我想學習如何使用Django模板的URL標記使我的代碼更通用,但我有一些異常被提出。Django 1.8模板的URL標記TypeError
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):
我一直在尋找這幾個小時,我似乎遵守所有的指引,使這項工作,當然一定出事了。
您能否顯示堆棧跟蹤的其餘部分,以便我們知道這個異常不會在'views.detail'中引發? –
不相關,但是您忘記了主控urls.py中的'namespace =「polls」'。再次查看[教程](https://docs.djangoproject.com/en/1.8/intro/tutorial03/#namespacing-url-names)。 –