2015-08-14 115 views
4

我想學習如何使用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): 

我一直在尋找這幾個小時,我似乎遵守所有的指引,使這項工作,當然一定出事了。

+0

您能否顯示堆棧跟蹤的其餘部分,以便我們知道這個異常不會在'views.detail'中引發? –

+3

不相關,但是您忘記了主控urls.py中的'namespace =「polls」'。再次查看[教程](https://docs.djangoproject.com/en/1.8/intro/tutorial03/#namespacing-url-names)。 –

回答

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') 
] 
+0

哦真的。我對Django真的很陌生,但我沒有看到這一點。 非常感謝你,因爲我可以從字面上搜索AGES。 – soueuls

+1

@SwannPolydor不客氣。回顧一下我的早期問題(也是Django相關的),並且你會感覺好多了。 – orokusaki

+1

這不是'dict',它是'set'。 '{0:1,2:3}'會給你一個'dict',但像'{0,2}'一樣使用單個值會給你一個集合。 – Matthias