Django urlresolvers:需要2个以上的值才能解压缩

时间:2014-04-10 21:17:10

标签: django

我正在尝试从Django 1.3升级到Django 1.4。我坚持这个错误:

  

Python版本:2.7.3
  Django版本:1.4.10
  异常类型:ValueError
  例外值:需要2个以上的值来解包

触发该错误的行是(在模板/var/www/proj/src_1.4/templates/fragments/header.html,第20行的错误):

    <a id="login" href="{% url login %}" rel="nofollow">{% trans "Login" %}</a>

它在Django 1.3中运行良好。

我尝试了以下内容:

python manage.py shell
>> from django.conf.urls import *
>> from django.core.urlresolvers import reverse
>> reverse('login')

然后出现此错误:

ValueError                                Traceback (most recent call last)
/var/www/proj/env_1.4/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 reverse('login')

/var/www/proj/env_1.4/local/lib/python2.7/site-packages/django/core/urlresolvers.pyc in reverse(viewname, urlconf, args, kwargs, prefix, current_app)
474             resolver = get_ns_resolver(ns_pattern, resolver)
475 
--> 476     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
477 
478 reverse_lazy = lazy(reverse, str)

/var/www/proj/env_1.4/local/lib/python2.7/site-packages/django/core/urlresolvers.pyc in _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs)
363         possibilities = self.reverse_dict.getlist(lookup_view)
364         prefix_norm, prefix_args = normalize(_prefix)[0]
--> 365         for possibility, pattern, defaults in possibilities:
366             for result, params in possibility:
367                 if args:

ValueError: need more than 2 values to unpack

如果我在尝试加载项目时查看Django显示的信息,“Local vars”会显示该信息:

self    <RegexURLResolver urls (None:None) ^/>
args    ()
_prefix u'/'
possibilities   [([(u'accounts/login/', [])], 'accounts/login/$')]
lookup_view u'login'
prefix_norm u'/'
prefix_args []
kwargs  {}

proj / urls.py中的代码

from django.conf.urls import *

urlpatterns += patterns('',
    url(r'^', include('home.urls')),                
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts', include('accounts.urls')),

apps / accounts / urls.py中的代码

from django.conf.urls import patterns, url

urlpatterns = patterns('',
url(r'^/register$',
    'accounts.views.register',
    name='register'),
url(r'^/login/$',
    'django.contrib.auth.views.login',
    {'template_name': 'accounts/login.html', 'authentication_form': AuthenticationForm},
    name='login'),

我会感激任何帮助。感谢。

最后我发现了错误。这是一个用于URL国际化的旧应用程序(i18nurls)。 Django 1.3使用外部应用程序,在Django&gt; 1.4国际化被纳入核心(Django: Internationalization: in URL patterns)。

感谢。

2 个答案:

答案 0 :(得分:0)

不确定这是否可以解决您的错误,但请尝试执行以下操作: 在您的主 url.py 替换

url(r'^accounts', include('accounts.urls')),

url(r'^accounts/', include('accounts.urls')), ### add the slash /

最好将slash /包含在外部urlpatterns中,而不是内部urlpatter中。

然后,在accounts内的 urls.py 替换

url(r'^/register$',...
url(r'^/login/$',....

url(r'^register/$',...   ### delete dhe leading slashed, because you added it in `accounts/`
url(r'^login/$',....     ### finish both regex with the slash

另一个建议是添加名称空间:

url(r'^accounts/', include('accounts.urls', namespace='accounts')),

现在使用"{% url 'accounts:login' %}"

在内部参考您的模板

通过这种方式,您可以更好地了解哪个网址来自哪个应用。检查这些提示是否修复了您的错误,如果没有,请告诉我

答案 1 :(得分:0)

如果这是您的完整urls.py代码

来自django.conf.urls导入模式,网址

urlpatterns = patterns('',
url(r'^/register$',
    'accounts.views.register',
    name='register'),
url(r'^/login/$',
    'django.contrib.auth.views.login',
    {'template_name': 'accounts/login.html', 'authentication_form': AuthenticationForm},
    name='login'),

检查你没有添加结束括号)

来自django.conf.urls导入模式,网址

urlpatterns = patterns('',
url(r'^/register$',
    'accounts.views.register',
    name='register'),
url(r'^/login/$',
    'django.contrib.auth.views.login',
    {'template_name': 'accounts/login.html', 'authentication_form': AuthenticationForm},
    name='login'),
)