[英]Reverse for 'ques_detail' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['ques_detail/(?P<pk>[0-9]+)/$']
I am getting the following error: 我收到以下错误:
Reverse for 'ques_detail' with keyword arguments '{'pk': ''}' not found.
找不到带有关键字参数'{'pk':''}'的'ques_detail'。 1 pattern(s) tried: ['ques_detail/(?P[0-9]+)/$']
尝试了1个模式:['ques_detail /(?P [0-9] +)/ $']
Does anyone know how to solve it? 有人知道如何解决吗?
I tried solutions posted on many sites but nothing worked. 我尝试过在许多网站上发布的解决方案,但没有任何效果。 Someone kindly help.
有人帮忙。
urls.py urls.py
from django.urls import path
from . import views
urlpatterns = [
path('logout', views.logout, name='test_logout'),
path('register', views.register, name = 'register'),
path('', views.welcome, name='welcome'),
path('instructions', views.instructions, name = 'instructions'),
path('ques_detail/<int:pk>/',views.ques_detail,name='ques_detail')
]
views.py views.py
def instructions(request):
return render(request,'events/instructions.html')
def ques_detail(request, pk):
ques = get_object_or_404(Questionm, pk=pk)
return render(request, 'events/ques_detail.html', {'ques': ques})
instructions.html Instructions.html
{% extends 'base.html' %}
{% block content %}
<div class="register">
<h1>Instructions</h1>
</div>
<br><br><hr><hr>
<ul class="list-group">
<li class="list-group-item">Lorem ipsum dolor sit amet, consectetur...</li>
</ul>
<div class="start">
<button type="button" class="btn btn-success" style="width: 350px;
height: 80px;font-size : 500px;"><a href="{% url 'ques_detail' pk=ques.pk %}">
<h4>Start Test</h4></a></button>
</div>
{% endblock %}
Something is missing in the view instructions
to call the Start Test page properly. 视图
instructions
缺少某些内容,无法正确调用“ 开始测试”页面。
In the template you define the button with an url to call a question : 在模板中,您定义带有URL的按钮以调用问题:
<button type="button" class="btn btn-success" style="width: 350px;
height: 80px;font-size : 500px;">
<a href="{% url 'ques_detail' pk=ques.pk %}">
<h4>Start Test</h4></a>
</button>
{% url 'ques_detail' pk=ques.pk %}
will call the view ques_detail
and try to pass the question ID with the parameter pk
, OK, but you never define ques.pk
here, that's why you got an empty string and the reverse error. {% url 'ques_detail' pk=ques.pk %}
将调用视图ques_detail
并尝试使用参数pk
传递问题ID,确定,但是您从未在此处定义ques.pk
,这就是为什么会有空字符串和反向错误。
When instructions.html is rendered, you have to define a ques
object in the context of the template, like you do with render(request, 'events/ques_detail.html', {'ques': ques})
. 当instructions.html渲染,你必须定义一个
ques
在模板的上下文对象,就像你做render(request, 'events/ques_detail.html', {'ques': ques})
So you could have a view like the following : 因此,您可以拥有如下视图:
def instructions(request):
ques = Questionm.objects.first()
context = {'ques': ques}
return render(request,'events/instructions.html', context)
(I query a question randomly ( Questionm.objects.first()
) you will have to replace this to query the question you need.) (我随机查询一个问题(
Questionm.objects.first()
),您将不得不替换它以查询所需的问题。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.