我渲染时django错误NoReverseMatch
我正在使用本教程创建我的第一个django应用程序。我已经完成了最后一个任务,现在我有一个错误,我不知道如何修复。我将向您展示我的整个程序,我将尝试确定我认为我在进行此投票时出错的地方。此应用程序类似于投票。它显示一个投票和一些选择,您必须对其进行投票 这是我的错误我渲染时django错误NoReverseMatch,django,Django,我正在使用本教程创建我的第一个django应用程序。我已经完成了最后一个任务,现在我有一个错误,我不知道如何修复。我将向您展示我的整个程序,我将尝试确定我认为我在进行此投票时出错的地方。此应用程序类似于投票。它显示一个投票和一些选择,您必须对其进行投票 这是我的错误 TemplateSyntaxError at /polls/1/ Caught NoReverseMatch while rendering: u'myapp' is not a registered namespaceReques
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>
模板语法错误位于/polls/1/
呈现时捕获到NoReverseMatch:u'myapp'不是注册的命名空间请求方法:GET
请求URL:http://cat.pythonanywhere.com/polls/1/
Django版本:1.3.5
异常类型:TemplateSyntaxError
异常值:呈现时捕获到NoReverseMatch:u'myapp'不是注册的命名空间
异常位置:/usr/local/lib/python2.7/site-packages/django/template/defaulttags.py在渲染中,第450行
Python可执行文件:/usr/local/bin/uwsgi
模板错误
在template/home/cat/mysite/myapp/templates/myapp/detail.html中,第5行出现错误
呈现时捕获到NoReverseMatch:u'myapp'不是注册的命名空间
1{{poll.question}
2.
3{%if error\u message%}{{error\u message}{%endif%}
4.
5.
6{%csrf_令牌%}
7{poll.choice_set.all%中的选择百分比}
8.
9{{choice.choice_text}}
10{%endfor%}
11
12
我认为错误隐藏在my 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>
{{poll.question}
{%if error\u message%}{{{error\u message}{%endif%}
{%csrf_令牌%}
{poll.choice_set.all%}
{{choice.choice_text}}
{%endfor%}
我的URL.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'),
)
从django.conf.url.defaults导入*
从mysite.myapp导入视图
urlpatterns=模式(“”,
url(r'^$',views.index,name='index'),
url(r'^(?P\d+/$),views.detail,name='detail'),
url(r'^(?P\d+)/results/$',views.results,name='results'),
url(r'^(?P\d+)/vote/$',views.vote,name='vote'),
)
我希望有人能帮助我,因为我不知道如何修复此错误尝试将模板中的
{%url myapp:vote poll.id%}
更改为{%url vote poll.id%}
在管理员url保存的主url中,您的主urlconf轮询必须如下以注册该命名空间:
mainurl.py
url (r'^poll/', include('poll.urls', namespace='poll')),
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'),
)
然后在子url.py中
url (r'^poll/', include('poll.urls', namespace='poll')),
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'),
)
urlpatterns=patterns('poll.views',
url(r'^$',索引,name='index'),
url(r'^(?P\d+/$),'detail',name='detail'),
url(r'^(?P\d+)/results/$,'results',name='results'),
url(r'^(?P\d+)/vote/$,'vote',name='vote'),
)
如果主URL.py包含
url (r'^poll/', include('poll.urls'))
然后换成
url (r'^poll/', include('poll.urls', namespace='poll'))
这对我很有用。你不应该使用/en/1.3/intro/tutorial04/而不是/en/dev/intro/tutorial04/?@尼克你可能是对的。当你遇到名称空间错误时,问题出在你的主url中