我从[THIS] [1]教程学习Django。一切都很顺利,直到这一步:
现在更改您的民意调查/ index.html模板:
<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>
指向命名空间详细信息视图:
<li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
你可以看到整段[HERE] [2]。
这就是我改变了细节之后发生的事情&#39;进入&#39;民意调查:详细信息&#39;:
NoReverseMatch at /polls/
'polls' is not a registered namespace
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.6.4
Exception Type: NoReverseMatch
Exception Value:
'polls' is not a registered namespace
Exception Location: D:\Programy\Python 3.4\lib\site-packages\django\core\urlresolvers.py in reverse, line 528
Python Executable: D:\Programy\Python 3.4\python.exe
Python Version: 3.4.0
Python Path:
['D:\\dziango\\strona1',
'D:\\Programy\\Python 3.4\\python34.zip',
'D:\\Programy\\Python 3.4\\DLLs',
'D:\\Programy\\Python 3.4\\lib',
'D:\\Programy\\Python 3.4',
'D:\\Programy\\Python 3.4\\lib\\site-packages']
Server time: Wed, 21 May 2014 16:36:13 +0200
这是我的追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.6.4
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Template error:
In template D:\dziango\strona1\polls\templates\polls\index.html, error at line 1
'polls' is not a registered namespace
1 : {% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li><a href=" {% url 'polls:detail' poll.id %} ">{{ poll.question }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>Nie ma ankiet.</p>
{% endif %}
Traceback:
File "D:\Programy\Python 3.4\lib\site-packages\django\core\handlers\base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\dziango\strona1\polls\views.py" in index
11. return render(request, 'polls/index.html', context)
File "D:\Programy\Python 3.4\lib\site-packages\django\shortcuts\__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "D:\Programy\Python 3.4\lib\site-packages\django\template\loader.py" in render_to_string
169. return t.render(context_instance)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in render
140. return self._render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
305. return nodelist.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
196. nodelist.append(node.render(context))
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
447. six.reraise(*exc_info)
File "D:\Programy\Python 3.4\lib\site-packages\django\utils\six.py" in reraise
550. raise value
File "D:\Programy\Python 3.4\lib\site-packages\django\template\defaulttags.py" in render
433. url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "D:\Programy\Python 3.4\lib\site-packages\django\core\urlresolvers.py" in reverse
528. key)
Exception Type: NoReverseMatch at /polls/
Exception Value: 'polls' is not a registered namespace
我尝试了[THIS] [3]类似主题的每一个解决方案,但它们都不适合我。 我不会在这里粘贴我的代码,因为我从该教程中编写了所有代码并且它完全相同。我所做的一切都是按照它一步一步完成的。 但是如果你还需要我的代码,请告诉我,我会把它放在这里。
抱歉我的英文。
==编辑== 这是我的根urls.py文件:
from django.conf.urls import patterns, url, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
)
这是polls / views.py:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from polls.models import Choice, Poll
# Create your views here.
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list }
return render(request, 'polls/index.html', context)
def detail(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, 'polls/detail.html', {'poll':poll})
def results(request, poll_id):
return HttpResponse("Wyniki ankiety %s!" %poll_id)
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form
return render(request, 'polls/detail.html', {
'poll':p,
'error_message':"Nie wybrałeś żadnej opcji.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponse(reverse('polls:results', args=(p.id,)))
轮询/ urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
更新: 好吧,我将urls.py放在错误的目录中犯了一个愚蠢的错误..抱歉
答案 0 :(得分:25)
我认为您在包含民意调查网址时忘记设置名称空间。
在根urlconf文件中
更改
url(r'^polls/', include('polls.urls')),
到
url(r'^polls/', include('polls.urls', namespace="polls")),
答案 1 :(得分:2)
您应该像在polls/urls.py
中一样添加应用名称,我遇到了同样的问题,但是通过添加以下内容解决了该问题:
app_name = 'polls'
答案 2 :(得分:0)
是的,我也遇到名称空间问题-上面的回复对我有所帮助!
在浏览器上输入服务器链接时,确保您位于/ polls而非/ admin / polls非常重要。
此外,这也适用于路径。
对于mysite / polls / urls-请添加以下代码行
app_name = 'polls'
,然后您的mysite / mysite / url也应如下所示:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls', namespace="polls")),
path('admin/', admin.site.urls),
]