URL 国际化后呈现为 home_page
URL rendered as home_page after internationalization
我有一个要国际化的 Django 网站。到目前为止它看起来像这样:
首页:
www.myhomepage.com
另一页:
www.myhomepage.com/content/cities
现在我想把它变成这样:
首页:
www.myhomepage.com/en
www.myhomepage.com/de
另一页:
www.myhomepage.com/en/content/cities
www.myhomepage.com/de/content/cities
在 this and this 之后,我设法让主页正常工作,所以 www.myhomepage.com/en
我看到英文主页,www.myhomepage.com/de
我看到德文主页。
当我想转到任何其他页面时,问题就来了,比如 www.myhomepage.com/en/content/cities
。那么,渲染出来的页面还是首页。在更改任何设置以使其国际化之前,www.myhomepage.com/content/cities
已正确显示。
我的猜测是视图渲染或 url 有问题,但我没能成功。
请注意,www.myhomepage.com
的视图属于一个应用,content/cities
的视图属于另一个应用。
这是我的代码:
settings.py
MIDDLEWARE_CLASSES = [
...
'django.middleware.locale.LocaleMiddleware',
...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.i18n',
],
},
},
]
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en-us'
USE_I18N = True
主应用程序:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += i18n_patterns('',
url(r'^content/', include('content.urls', namespace='content')),
)
views.py
from django.shortcuts import render
def home_view(request):
...
context = {
...
}
#print('request home', request)
return render(request, 'home_template.html', context)
通过激活 print 语句并加载 www.myhomepage.com/en/content/cities
,以下内容将打印在控制台中:request home: <WSGIRequest: GET '/en/content/cities/'>
,即使此视图属于 home_page.
内容应用:
urls.py
from .views import countries_and_cities
urlpatterns = [
...
url(r'^cities/$', countries_and_cities),
...
]
views.py
from django.shortcuts import render
def countries_and_cities(request):
...
context = {
...
}
return render(request, 'cities_template.html', context)
我也尝试了docs中的建议,但没有成功。
urls.py
来自主应用程序:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
from content import views as content_views
content_patterns = ([
url(r'^cities/$', content_views.countries_and_cities, name='cities'),
], 'content')
urlpatterns += i18n_patterns('',
url(r'^content/', include(content_patterns, namespace='content')),
)
我做错了什么?
终于找到问题了
还有一个 url 定义为:
urlpatterns += i18n_patterns(
...
url(r'', include('main.urls')),
...
)
这是导致问题的原因,即使在进行国际化之前它工作正常。
我刚改成:
urlpatterns += i18n_patterns(
...
url(r'^$', include('main.urls')),
...
)
它工作正常。
我有一个要国际化的 Django 网站。到目前为止它看起来像这样:
首页:
www.myhomepage.com
另一页:
www.myhomepage.com/content/cities
现在我想把它变成这样:
首页:
www.myhomepage.com/en
www.myhomepage.com/de
另一页:
www.myhomepage.com/en/content/cities
www.myhomepage.com/de/content/cities
在 this and this 之后,我设法让主页正常工作,所以 www.myhomepage.com/en
我看到英文主页,www.myhomepage.com/de
我看到德文主页。
当我想转到任何其他页面时,问题就来了,比如 www.myhomepage.com/en/content/cities
。那么,渲染出来的页面还是首页。在更改任何设置以使其国际化之前,www.myhomepage.com/content/cities
已正确显示。
我的猜测是视图渲染或 url 有问题,但我没能成功。
请注意,www.myhomepage.com
的视图属于一个应用,content/cities
的视图属于另一个应用。
这是我的代码:
settings.py
MIDDLEWARE_CLASSES = [
...
'django.middleware.locale.LocaleMiddleware',
...
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.i18n',
],
},
},
]
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
LANGUAGE_CODE = 'en-us'
USE_I18N = True
主应用程序:
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += i18n_patterns('',
url(r'^content/', include('content.urls', namespace='content')),
)
views.py
from django.shortcuts import render
def home_view(request):
...
context = {
...
}
#print('request home', request)
return render(request, 'home_template.html', context)
通过激活 print 语句并加载 www.myhomepage.com/en/content/cities
,以下内容将打印在控制台中:request home: <WSGIRequest: GET '/en/content/cities/'>
,即使此视图属于 home_page.
内容应用:
urls.py
from .views import countries_and_cities
urlpatterns = [
...
url(r'^cities/$', countries_and_cities),
...
]
views.py
from django.shortcuts import render
def countries_and_cities(request):
...
context = {
...
}
return render(request, 'cities_template.html', context)
我也尝试了docs中的建议,但没有成功。
urls.py
来自主应用程序:
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
from content import views as content_views
content_patterns = ([
url(r'^cities/$', content_views.countries_and_cities, name='cities'),
], 'content')
urlpatterns += i18n_patterns('',
url(r'^content/', include(content_patterns, namespace='content')),
)
我做错了什么?
终于找到问题了
还有一个 url 定义为:
urlpatterns += i18n_patterns(
...
url(r'', include('main.urls')),
...
)
这是导致问题的原因,即使在进行国际化之前它工作正常。
我刚改成:
urlpatterns += i18n_patterns(
...
url(r'^$', include('main.urls')),
...
)
它工作正常。