Django 1.8模板的URL标记TypeError

时间:2015-08-14 00:01:05

标签: python django django-templates

我正在尝试学习如何使用Django模板的URL标记来使我的代码更通用,但我提出了一些异常。

Exception Type:     TypeError
Exception Value:    argument to reversed() must be a sequence

这是我的全球urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^polls/', include('polls.urls'))
]

这是我的应用urls.py

from django.conf.urls import url
from . import views

urlpatterns = {
    url(r'^$', views.index, name='index'),
    url(r'^(?P<id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<id>[0-9]+)/results/$', views.results, name='results'),
    url(r'^(?P<id>[0-9]+)/vote/$', views.vote, name='vote')
}

这是我尝试使用该功能的一个模板。

{% if latest_question_list %}
  <ul>
    {% for question in latest_question_list %}
      <li><a href="{% url 'polls:detail' question.id %}">{{ question.content }}</a></li>
    {% endfor %}
  </ul>
{% else %}
  <p>No polls are available.</p>
{% endif %}

回溯:

File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/polydo_s/Projects/Modeling/app/polls/views.py" in index
  11.     return render(request, 'polls/index.html', context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
  99.         return template.render(context, request)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/backends/django.py" in render
  74.         return self.template.render(context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/base.py" in render
  209.                     return self._render(context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/base.py" in _render
  201.         return self.nodelist.render(context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/base.py" in render
  903.                 bit = self.render_node(node, context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/defaulttags.py" in render
  329.                 return nodelist.render(context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/base.py" in render
  903.                 bit = self.render_node(node, context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/defaulttags.py" in render
  217.                             nodelist.append(node.render(context))
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/template/defaulttags.py" in render
  493.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/core/urlresolvers.py" in reverse
  579.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  433.             self._populate()
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/core/urlresolvers.py" in _populate
  308.                     for name in pattern.reverse_dict:
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/core/urlresolvers.py" in reverse_dict
  338.             self._populate()
File "/home/polydo_s/Projects/Modeling/venv/lib/python3.4/site-packages/django/core/urlresolvers.py" in _populate
  285.         for pattern in reversed(self.url_patterns):

我一直在寻找这个问题几个小时,而且我似乎遵循所有指导方针来完成这项工作,但当然有些事情必定是错误的。

1 个答案:

答案 0 :(得分:2)

您已将urlpatterns定义为set - {}。您需要list - []

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<id>[0-9]+)/results/$', views.results, name='results'),
    url(r'^(?P<id>[0-9]+)/vote/$', views.vote, name='vote')
]