1

我正在尝试以两种语言设置我的应用程序,但是我在所有应用程序的 url 上都收到 404 错误,即使我前一段时间以完全相同的方式设置了另一个应用程序。

模型.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)

翻译.py:

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

translator.register(New, NewTranslationOptions)

网址.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'),
)

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

4

1 回答 1

1

specific_categoryspecific_producturl 模式正在从news应用程序中捕获 url:

>>> 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/为应用程序中的模式添加前缀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'),
于 2013-09-15T19:33:43.693 回答