0

我对 Django i18n 路由有疑问。我想在 URL 中设置语言但不起作用。我在项目中创建了 urls.py,并在应用程序中创建了另一个 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')),
)

个性化的.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: []

谢谢!!

4

1 回答 1

0

I guess you need to activate the language? Read this: https://docs.djangoproject.com/en/1.7/topics/i18n/translation/#language-prefix-in-url-patterns. Have you tried from the command line? Does it work?

于 2015-01-09T12:37:33.093 回答