Django教程中缺少参数

p1tboqfb  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(185)

我现在已经开始学习Django了,当我在做教程的时候,我得到了一个错误。在像教程第4部分那样更改视图后,我开始得到一个错误,即缺少一些参数。
Reverse for 'votes' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/votes/\\Z']
我在谷歌上搜索了这个问题,其他人也有同样的问题,但这就像拼写正确的问题。在我的问题中,我在details.html中丢失了一个名为question的参数。我尝试了for循环,它帮助了我,但我的答案没有显示出来。
views.py:

from django.shortcuts import render, get_object_or_404
from django.template import loader
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from .models import Questions, Choice

class IndexView(generic.ListView):
    template_name = "polls/index.html"
    context_object_name = "latest_question_list"

    def get_queryset(self):
        "Return latest 5 questions"
        return Questions.objects.order_by("-pub_date")[:5]

class DetailsView(generic.DetailView):
    model = Questions
    template_name = "polls/details.html"

class ResponseView(generic.DetailView):
    model = Questions
    template_name = "polls/response.html"

def votes(request, question_id):
    question = get_object_or_404(Questions, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST["choice"])
    except (KeyError, Choice.DoesNotExist):
        return render(
            request,
            "polls/details.html",
            {"question": question, "error_message": "You didn't select a choice"},
        )
    else:
        selected_choice.votes += 1
        selected_choice.save()
    return HttpResponseRedirect(reverse("polls:response", args=(question.id,)))

urls.py:

from django.contrib import admin
from django.urls import path, include
from . import views

app_name = "polls"
urlpatterns = [
    path("<int:question_id>/votes/", views.votes, name="votes"),
    path("<int:pk>/", views.DetailsView.as_view(), name="details"),
    path("<int:pk>/response/", views.ResponseView.as_view(), name="response"),
    path("", views.IndexView.as_view(), name="index"),
]

details.html:

{% for question in latest_question_list %}
    <h1>{{ question.question_text }}</h1>
    <form action="{% url 'polls:votes' question.id %}" method="post">
    {% csrf_token %}
    <fieldset>
        <legend><h1>{{ question.question_text }}</h1></legend>
            {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
            {% for choice in question.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 %}
        </fieldset>
    <input type="submit" value="Vote">
    </form>
{% endfor %}

就像我说的,我尝试添加for循环来定义我的参数,但它并不完美。我将非常感谢你的帮助。
谢谢<3

jjjwad0x

jjjwad0x1#

错误消息.... with arguments '('',)' not found. ...只是告诉您,参数在URL标记中为空,如question.id

{% url 'polls:votes' question.id %}

在您的示例中,问题为空,因为模型的默认“context-object-name”在基于泛型类的视图的模板中为object。所以object.id应该可以工作。
现在,如果你想在details.html中使用latest_question_list,就添加了for循环

{% for question in latest_question_list %}

您需要在视图中添加:

class DetailsView(generic.DetailView):
    model = Questions
    template_name = "polls/details.html"
    context_object_name = "latest_question_list"

但我认为这不是目标,因为它是详细视图,您只是想解决上面缺少参数的问题。

相关问题