django模型翻譯404 (django modeltranslation 404)


問題描述

django模型翻譯404 (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.


參考解法

方法 1:

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

(by Marijusalecxe)

參考文件

  1. django modeltranslation 404 (CC BY‑SA 3.0/4.0)

#internationalization #django-modeltranslation #django-i18n #Django






相關問題

志願者翻譯對 Delphi 2009 應用程序進行本地化的過程? (Process for localization of Delphi 2009 app by volunteer translators?)

Netbeans 菜單和 i18n (Netbeans menu and i18n)

Menggunakan Perpustakaan Penutupan pada aplikasi Phonegap (Android) (Using Closure Library on Phonegap(Android) application)

支持 SDK 但不支持 PhoneGap 的印度語語言 (Indic Language Support for SDK but not PhoneGap)

翻譯 symfony2 路由錯誤 (translation symfony2 routing error)

Django 沒有正確翻譯網站 (Django not translating the site properly)

django模型翻譯404 (django modeltranslation 404)

您是否知道用於編輯/翻譯資源(.rc)文件的好的程序? (Do you know of a good program for editing/translating resource (.rc) files?)

Struts 2 動作調用丟失 xwork i18n 語言設置 (Struts 2 action call loses the xwork i18n language setting)

移動應用測試 (Mobile application testing)

nuxt-i18n 的延遲加載語言環境 (Lazy load locales for nuxt-i18n)

你能在 next.config 中獲取數據嗎 (Can you fetch data in next.config)







留言討論