【问题标题】:Django i18n_patterns does not match links with different language codesDjango i18n_patterns 不匹配具有不同语言代码的链接
【发布时间】:2012-07-05 09:17:03
【问题描述】:

我已经花了几个小时试图了解我错过了什么。我在项目设置文件中定义了语言。

LANGUAGE_CODE = 'en-us'

LANGUAGES = (
    ('en', 'English'),
    ('fr', 'French'),
)

然后我的 MIDDLEWARE_CLASSES 中列出了 LocaleMiddleware。

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    ...
)

最后在我的 urls.py 中,我有以下内容

from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns

...
urlpatterns += i18n_patterns('',
    url(r'^about/$', 'about.view', {'template_name': {'en':'contact_en.html', 'fr':'contact_fr.html',},}, name='about'),
)

现在链接 /en/about 可以正常工作,但是 /fr/about 链接失败并出现以下错误:

Using the URLconf defined in XXX.urls, Django tried these URL patterns, in this order: 
...
2. ^en/

The current URL, fr/about/, didn't match any of these.

谁能帮我理解我错过了什么?

【问题讨论】:

    标签: django localization internationalization url-pattern


    【解决方案1】:

    最后我解决了这个问题。以上所有内容对于常规项目都是正确的。但是我忘记了我编写了一个中间件,该中间件可以动态修补request.urlconf 字段以替换为正确的URL conf。在查看LocaleMiddlware 代码后,我发现中间件使用的是settings.ROOT_URLCONF,因此没有查看正确的urls.py

    所以如果有人确实修补了urls.py,别忘了也修补ROOT_URLCONF

    【讨论】:

      猜你喜欢
      • 2013-11-04
      • 1970-01-01
      • 2017-08-04
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多