Search code examples
djangointernationalizationdjango-i18ndjango-modeltranslation

django modeltranslation 404


I'm trying to set up my app in two languages, however I'm getting 404 error on all app's urls, even though I've set up another app a while ago the exact same way.

models.py:

class New(models.Model):
    title = models.CharField(max_length=300)
    slug = models.SlugField(max_length=300, editable=False)
    pub_date = models.DateTimeField(auto_now_add=True)
    text = models.TextField()

    def __unicode__(self):
        return self.title

    def save(self, *args, **kwargs):
        if not self.id:
            # Newly created object, so set slug
            self.slug = slugify(self.title)

        super(New, self).save(*args, **kwargs)

translation.py:

class NewTranslationOptions(TranslationOptions):
    fields = ('title','text')

translator.register(New, NewTranslationOptions)

urls.py:

urlpatterns += i18n_patterns('',
    url(r'^categories/$', 'products.views.categories_index', name='categories_index'),
    url(r'^(?P<category_slug>[\w-]+)/$', 'products.views.specific_category', name='specific_category'),
    url(r'^(?P<category_slug>[\w-]+)/(?P<product_slug>[\w-]+)/$', 'products.views.specific_product', name='specific_product'),

    url(r'^news/$', 'news.views.news_index', name='news_index'),
    url(r'^news/(?P<news_slug>[\w-]+)/$', 'news.views.specific_new', name='specific_new'),
)

Here you can also see urls of my other app products, it works just fine. If you need anything else please let me know.


Solution

  • Your specific_category and specific_product url patterns are catching urls from news app:

    >>> re.match("(?P<category_slug>[\w-]+)", "news").groups()
    ('news',)
    

    Reorder your urls patterns:

    urlpatterns += i18n_patterns('',
        url(r'^categories/$', 'products.views.categories_index', name='categories_index'),
    
        url(r'^news/$', 'news.views.news_index', name='news_index'),
        url(r'^news/(?P<news_slug>[\w-]+)/$', 'news.views.specific_new', name='specific_new'),
    
        url(r'^(?P<category_slug>[\w-]+)/$', 'products.views.specific_category', name='specific_category'),
        url(r'^(?P<category_slug>[\w-]+)/(?P<product_slug>[\w-]+)/$', 'products.views.specific_product', name='specific_product'),
    )
    

    Or, consider adding category/ prefix to patterns from products app:

    url(r'^category/(?P<category_slug>[\w-]+)/$', 'products.views.specific_category', name='specific_category'),
    url(r'^category/(?P<category_slug>[\w-]+)/(?P<product_slug>[\w-]+)/$', 'products.views.specific_product', name='specific_product'),