gpt4 book ai didi

python - 我想解决 Django NoReverseMatch

转载 作者:行者123 更新时间:2023-12-01 01:17:34 26 4
gpt4 key购买 nike

这里有问题。我不知道如何解决这个问题。请帮我。错误enter image description here

源代码

结果.html

<h1>{{ question.question_text }}</h1>

<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="{% url 'polls:detail' question_id %}">Vote again?</a>

views.py-polls

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from polls.models import Question, Choice

# Create your views here.
def index(request):
latest_question_list = Question.objects.all().order_by('-pub_date')[:5]
context = {'latest_question_list':latest_question_list}
return render(request, 'polls/index.html', context)

def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})

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

def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})

这里似乎有一个错误。我不认为民意调查结果相反。当然这只是推测。

urls.py-fistsite

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

urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
path('', views.index, name='index'),
]

urls.py-民意调查

from django.urls import path
from . import views


app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote')
]

最佳答案

您尚未将变量 question_id 传递到模板上下文中。

在您的模板中,您可以使用问题对象来代替

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

关于python - 我想解决 Django NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54177476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com