Django i18n路由无法正常工作

时间:2015-01-09 07:36:19

标签: routing django-i18n

我遇到了Django i18n路由问题。我想在URL中设置语言但不起作用。我在项目中创建了urls.py,在app中创建了另一个urls.py.代码如下所示:

个人/ urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from personalasier import views

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'personal.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
)

urlpatterns += i18n_patterns(
    url(r'^cv/$',include('personalasier.urls')),
)

personalasier.py

from django.conf.urls import patterns, url

from personalasier import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
)

当我访问/或/ cv返回404并向我显示时:

Using the URLconf defined in personal.urls, Django tried these URL patterns, in this order:
   ^admin/
   ^es/
The current URL, , didn't match any of these.

如果我访问/ es /,/ es / cv /或/ es / cv会给出另一个404结果:

Using the URLconf defined in personal.urls, Django tried these URL patterns, in this order:
     ^admin/
The current URL, es/, didn't match any of these.

最后我试图反转URL并抛出异常:

>>>from django.core.urlresolvers import reverse
>>> from django.utils.translation import activate
>>> activate("es")
>>> reverse("index")
Traceback (most recent call last):
    file "<console>", line 1, in <module>
    File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 551, in reverse
        return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
    File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 468, in _reverse_with_prefix(lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

谢谢!

1 个答案:

答案 0 :(得分:0)

我想你需要激活语言?阅读:https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#language-prefix-in-url-patterns。你试过命令行了吗?它有效吗?