Python Django-简单更改代码后的NoReverseMatch
因此,长话短说,我本周开始学习Django基础知识,并遵循一个简单的民意调查创建教程,该教程运行良好,但随后我开始尝试对代码进行一些更改,由于我在Django方面缺乏经验(老实说,也是在HTML方面),我最终遇到了这个NoReverseMatch错误。我所做的基本上是尝试更改一个类的名称及其在应用程序中的使用,以及它在项目中的对象(类TText)。 然后错误开始出现在localhost网页中的/polls/*(*分别是TText的id号)和/polls/*/结果中。 第一个错误出现在Python Django-简单更改代码后的NoReverseMatch,python,html,django,Python,Html,Django,因此,长话短说,我本周开始学习Django基础知识,并遵循一个简单的民意调查创建教程,该教程运行良好,但随后我开始尝试对代码进行一些更改,由于我在Django方面缺乏经验(老实说,也是在HTML方面),我最终遇到了这个NoReverseMatch错误。我所做的基本上是尝试更改一个类的名称及其在应用程序中的使用,以及它在项目中的对象(类TText)。 然后错误开始出现在localhost网页中的/polls/*(*分别是TText的id号)和/polls/*/结果中。 第一个错误出现在detail
detail.html
,第5行:
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<text_id>[0-9]+)/vote/$']
下面是我对django应用程序的设置:
/mysite
urls.py
/polls
models.py
urls.py
views.py
/templates
/polls
detail.html
index.html
results.html
其中:/mysite/url.py
是:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
另外-/polls/models.py
:
from __future__ import unicode_literals
import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
class TText(models.Model):
text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
text = models.ForeignKey(TText, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
另外-/polls/url.py
:
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<text_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
from django.shortcuts import get_object_or_404, render
# Create your views here.
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from .models import Choice, TText
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_text_list'
def get_queryset(self):
"""Return the last five published questions."""
return TText.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = TText
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = TText
template_name = 'polls/results.html'
def vote(request, text_id):
text = get_object_or_404(TText, pk=text_id)
try:
selected_choice = text.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'text': text,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(text.id,)))
另外-/polls/templates/polls/detail.html
:
<h1>{{ text.text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' text.id %}" method="post">
{% csrf_token %}
{% for choice in text.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>
另外-/polls/templates/polls/results.html
:
{% if latest_text_list %}
<ul>
{% for text in latest_text_list %}
<li><a href="{% url 'polls:detail' text.id %}">{{ text.text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
<h1>{{ text.text}}</h1>
<ul>
{% for choice in text.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' text.id %}">Vote again?</a>
{{text.text}
{text.choice\u set.all%中的选项为%s}
- {{choice.choice{u text}}--{{choice.voces}}投票{{choice.voces}复数化}
{%endfor%}
PS:这是第一次在Stack上发布问题,所以请随时指出任何错误。
哦,是的,我确实读过其他类似问题的帖子,但我无法将它们与我自己的问题相匹配。谢谢你抽出时间 传递到通用视图的上下文基于
模型
名称。模板中有两个选项,可以使用对象
或模型名称
。在您的例子中,这是ttext
。或者,您可以告诉通用视图在上下文中调用对象的内容:
class DetailView(generic.DetailView):
model = TText
template_name = 'polls/detail.html'
context_object_name = 'text'
class ResultsView(generic.DetailView):
model = TText
template_name = 'polls/results.html'
context_object_name = 'text'
然后,您可以使用当前在模板中执行的操作:
text.id
而不是ttext.id
或object.id
URL模板标记中存在问题。实际上,您是在告诉Django查找一个不存在的URL
据我所知,主要问题是您引用的是一个不存在的模板变量text
。它不存在,因为这些模板由Django的通用视图呈现,并且通用视图使用object
作为单个对象的标准模板变量名。如果您只是将这些模板文件中的文本
更改为对象
,您应该会得到更好的结果
另一个选项是在视图类上声明上下文对象名称,就像您在
IndexView
-context\u object\u name='latest\u text\u list'
中所做的那样,在哪个页面上会出现此错误?非常感谢!!多亏了那个小小的变化,它又漂亮地工作了。你的解释也帮我弄明白我做错了什么!谢谢你的解释,它真的帮助我理解了错误背后的概念!
class DetailView(generic.DetailView):
model = TText
template_name = 'polls/detail.html'
context_object_name = 'text'
class ResultsView(generic.DetailView):
model = TText
template_name = 'polls/results.html'
context_object_name = 'text'