gpt4 book ai didi

python - 使用参数 'vote' ', '(' app1/(?P[0-9]+)/vote/$'] 反转 )' not found. 1 pattern(s) tried: ['

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:48 25 4
gpt4 key购买 nike

没有通用 View ,一切正常

NoReverseMatch at /app1/2/

Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['app1/(?P<question_id>[0-9]+)/vote/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/app1/2/
Django Version: 1.11.3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['app1/(?P<question_id>[0-9]+)/vote/$']
Exception Location: C:\Python36\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 497
Python Executable: C:\Python36\python.exe
Python Version: 3.6.1
Python Path:
['C:\\Users\\Tomasz\\Desktop\\GitHub\\DjangoTutorialProject',
'C:\\Python36',
'C:\\Python36\\python36.zip',
'C:\\Python36\\DLLs',
'C:\\Python36\\lib',
'C:\\Python36\\lib\\site-packages']
Server time: Thu, 24 Aug 2017 08:34:58 +0200

模板渲染时出错

In template C:\Users\Tomasz\Desktop\GitHub\DjangoTutorialProject\app1\templates\app1\detail.html, error at line 5

Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['app1/(?P<question_id>[0-9]+)/vote/$']

1 <h1>{{ question.question_text }}</h1>
2
3 {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
4
5 <form action="{% url 'app1:vote' question.id %}" method="post">
6 {% csrf_token %}
7 {% for choice in question.mychoice_set.all %}
8 <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
9 <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
10 {% endfor %}
11 <input type="submit" value="Vote" />
12 </form>
13

问题是这段代码:

{% url 'app1:vote' question.id %}

查看

class DetailView(generic.DetailView):
model = MyQuestion
template_name = 'app1/detail.html'

网址

from django.conf.urls import url

from . import views

app_name = 'app1'
urlpatterns = [
#url(r'^contact/$', views.contact_view, name="contact_view"),
#url(r'^appinfo/$', views.appinfo_view, name="appinfo_view"),

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<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

模板

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

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'app1:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.mychoice_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>

最佳答案

您的模型是 MyQuestion,因此详细信息 View 将对象作为 myquestion 添加到上下文实例中。您需要更改模板以使用myquestion:

{% url 'app1:vote' myquestion.id %}

或在 View 中设置context_object_name:

class DetailView(generic.DetailView):
model = MyQuestion
context_object_name = 'question'

关于python - 使用参数 'vote' ', '(' app1/(?P<question_id>[0-9]+)/vote/$'] 反转 )' not found. 1 pattern(s) tried: [',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855143/

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