2013-02-14 67 views
0

我創建我的第一個Django應用程序使用本教程https://docs.djangoproject.com/en/dev/intro/tutorial04/。我到最後的任務,現在我有這個錯誤,我不知道如何解決。我要去向您展示我的整個計劃,並且我會盡力指出我在哪裏做錯了投票。這個應用就像投票。它顯示一個民意調查和一些選擇,你必須投票。django錯誤NoReverseMatch,而我渲染

這是我的錯誤

TemplateSyntaxError at /polls/1/ 
Caught NoReverseMatch while rendering: u'myapp' is not a registered namespaceRequest Method: GET 
Request URL: http://cat.pythonanywhere.com/polls/1/ 
Django Version: 1.3.5 
Exception Type: TemplateSyntaxError 
Exception Value: Caught NoReverseMatch while rendering: u'myapp' is not a registered namespace 
Exception Location: /usr/local/lib/python2.7/site-packages/django/template/defaulttags.py in render, line 450 
Python Executable: /usr/local/bin/uwsgi 

Template error 
In template /home/cat/mysite/myapp/templates/myapp/detail.html, error at line 5 

Caught NoReverseMatch while rendering: u'myapp' is not a registered namespace 
1 <h1>{{ poll.question }}</h1> 

2 
3 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 

4 
5 <form action="{% url myapp:vote poll.id %}" method="post"> 

6 {% csrf_token %} 

7 {% for choice in poll.choice_set.all %} 

8  <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> 

9  <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br /> 

10 {% endfor %} 

11 <input type="submit" value="Vote" /> 

12 </form> 

我認爲錯誤在我detail.html隱藏

<h1>{{ poll.question }}</h1> 

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} 

<form action="{% url myapp:vote poll.id %}" method="post"> 
{% csrf_token %} 
{% for choice in poll.choice_set.all %} 
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> 
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br /> 
{% endfor %} 
<input type="submit" value="Vote" /> 
</form> 

我Urls.py

from django.conf.urls.defaults import * 
from mysite.myapp import views 

urlpatterns = patterns('', 

    url(r'^$', views.index, name='index'), 
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'), 
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'), 
    url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'), 
) 

我希望有人能幫助我因爲我不知道如何解決這個錯誤

+0

您不應該在/en/1.3/intro/tutorial04/而不是/ en/dev/intro/tutorial04 /中使用教程嗎? – Nick 2013-02-14 04:36:11

+0

@nick你可能是對的。 – supersheep1 2013-02-14 04:53:02

+0

當您遇到命名空間錯誤時,問題出在您的主要網址 – catherine 2013-02-14 04:56:11

回答

2

在你的主網址在您的管理URL保存,你的主要的URLconf調查必須是這樣的,以註冊該命名空間:

主要urls.py

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

然後在孩子urls.py

urlpatterns = patterns('poll.views', 
    url(r'^$', index, name='index'), 
    url(r'^(?P<poll_id>\d+)/$', 'detail', name='detail'), 
    url(r'^(?P<poll_id>\d+)/results/$', 'results', name='results'), 
    url(r'^(?P<poll_id>\d+)/vote/$', 'vote', name='vote'), 
) 
+0

,感謝您的幫助 – supersheep1 2013-02-14 05:02:51

0

嘗試改變{% url myapp:vote poll.id %}{% url vote poll.id %}在你的模板

0

如果你的主urls.py包含

url (r'^poll/', include('poll.urls')) 

然後換

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

這爲我工作。