我想使用django-modeltranslation翻译URL前缀和URL slug,其中slug保存在数据库表中。切换语言后,我希望保持在同一页面上,只需更改语言即可。我正在使用此处所述的表单语言切换器:
http://docs.djangoproject.com/en/dev/topics/i18n/translation/#the-set-language-redirect-view
问题是该语言只是在主页上切换。其他页面只是刷新而没有语言和URL更改。
有什么办法可以用其他语言获取当前网址吗?
在根项目urls.py中,我有以下内容:
urlpatterns = patterns('',
# Examples:
(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^$', 'portfolio.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(_(r'^projects/'), include('projects.urls', namespace='projects')),
)
在应用程序中称为项目我有网址:
urlpatterns = patterns('',
url(r'^$', all_projects, name='projects'),
url(r'^(?P<slug>[\w-]+)/$', project_detail, name='project_detail'),
)
答案 0 :(得分:1)
如果这不是复制粘贴问题,则您在主url
中缺少urls.py
函数名称。将上面提供的代码的第3行更改为:
urlpatterns = patterns('',
...
# The following line need to be changed from
# (r'^i18n/', include('django.conf.urls.i18n')),
# to
url(r'^i18n/', include('django.conf.urls.i18n')),
...
)