0

我正在使用本教程https://docs.djangoproject.com/en/dev/intro/tutorial04/创建我的第一个 django 应用程序。我完成了最后的任务,现在我遇到了这个我不知道的错误如何修复。我将向您展示我的整个程序,并尝试指出我认为我在进行此投票时出错的地方。这个应用程序就像投票一样。它显示民意调查和一些选择,您必须对其进行投票。

这是我的错误

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>

我的网址.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'),
)

我希望有人可以帮助我,因为我不知道如何解决这个错误

4

3 回答 3

2

在您保存管理员 url 的主 url 中,您的主 urlconf 投票必须像这样才能注册该命名空间:

主要网址.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'),
)
于 2013-02-14T04:53:59.493 回答
0

尝试在您的模板中更改{% url myapp:vote poll.id %}{% url vote poll.id %}

于 2013-02-14T04:50:33.623 回答
0

如果您的主要 urls.py 包含

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

然后更改为

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

这对我有用。

于 2014-09-08T18:05:04.813 回答