1

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

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.

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

4

1 回答 1

0

Finally I resolved the problem. Everything above is correct for regular project. However I forgot the fact that I wrote a middleware that patches the request.urlconf field dynamically to replace with the right URL conf. After looking into LocaleMiddlware code I found out that the middleware instead uses the settings.ROOT_URLCONF and thus was not looking into correct urls.py!

So if anybody does patch the urls.py don't forget to patch also the ROOT_URLCONF

于 2012-07-05T13:14:11.713 回答