django modeltranslation 404

时间:2013-09-15 19:30:47

标签: django internationalization django-i18n django-modeltranslation

我正在尝试使用两种语言设置我的应用程序,但是我在所有应用程序的网址上都出现了404错误,即使我刚刚以相同的方式设置了另一个应用程序。

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'),
)

在这里你还可以看到我的其他应用产品的网址,它工作得很好。如果您还有其他需要,请告诉我。

1 个答案:

答案 0 :(得分:1)

您的specific_categoryspecific_product网址模式正在抓取来自news app的网址:

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

重新排列网址格式:

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'),
)

或者,请考虑在category/ app:

的模式中添加products前缀
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'),