Django - 代码中的简单更改后的NoReverseMatch

时间:2017-03-21 00:56:44

标签: python html django

所以,长话短说,我本周开始学习Django基础知识,然后进行了一个简单的民意调查创建教程,工作正常,但后来我开始尝试对代码进行一些更改,并且由于我在Django方面缺乏经验(并且说实话,也是在HTML中),我最终得到了这个NoReverseMatch错误。我基本上做的是尝试更改类的名称及其在应用程序中的用途,以及它在项目中的对象(类TText)。 然后错误开始出现在/ polls / *(*是TText的相应id号)和/ polls / * /结果在localhost网页中。 第一个在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/$']

第二行results.html,第9行:

Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<pk>[0-9]+)/$']

这是我的django应用程序的设置:

/mysite
    urls.py
/polls
    models.py
    urls.py
    views.py
    /templates
        /polls
            detail.html
            index.html
            results.html

其中:/mysite/urls.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/urls.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'),
]

也 - /polls/views.py

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/index.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 %}

也 - /polls/templates/polls/results.html

<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>

PS:这是第一次在Stack上发帖提问,所以请随时指出任何错误。 哦,是的,我确实阅读过类似问题的其他帖子,但我没有把它们与我自己的问题相匹配。谢谢你的时间!

2 个答案:

答案 0 :(得分:0)

传递到通用视图的上下文基于Model名称。模板中有两个选项,您可以使用objectname_of_model。在你的情况下,那是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.idobject.id

答案 1 :(得分:0)

问题出在您的网址模板标签中。基本上你是告诉Django找到一个不存在的URL。

据我所知,主要问题是你指的是一个不存在的模板变量text。它不存在,因为这些模板是由Django的通用视图呈现的,而通用视图使用object作为单个对象的标准模板变量名称。如果您只是将这些模板文件中的text更改为object,那么您应该会获得更好的结果。

另一个选项是在视图类上声明上下文对象名称,就像您在IndexView - context_object_name = 'latest_text_list'中所做的那样。