简体   繁体   English

找不到带有关键字参数&#39;{&#39;pk&#39;:&#39;&#39;}&#39;的&#39;ques_detail&#39;。 尝试了1个模式:[&#39;ques_detail /(?P <pk> [0-9] +)/ $&#39;]

[英]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.

相关问题 未找到参数为 &#39;(&#39;&#39;,)&#39; 的 &#39;post-detail&#39; 的 Django Reverse。 尝试了 1 个模式:[&#39;post\\\\/(?P<pk> [0-9]+)\\\\/$&#39;] - Django Reverse for 'post-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post\\/(?P<pk>[0-9]+)\\/$'] 找不到带有参数“(&#39;&#39;,)”的“ news_detail”。 尝试了1个模式:[&#39;news \\\\-(?P <news_pk> [0-9] +)$&#39;] - Reverse for 'news_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['news\\-(?P<news_pk>[0-9]+)$'] .NoReverseMatch:未找到参数为 &#39;(&#39;&#39;,)&#39; 的 &#39;author-detail&#39; 反转。 尝试了 1 个模式:[&#39;目录/作者/(?P<pk> [0-9]+)$&#39;] - .NoReverseMatch: Reverse for 'author-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/author/(?P<pk>[0-9]+)$'] 未找到 arguments '('',)' 的 'book-detail' 反向。 尝试了 1 种模式:['catalog/book/(?P<pk> [0-9]+)$']</pk> - Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['catalog/book/(?P<pk>[0-9]+)$'] 未找到 arguments '('',)' 的 'article_detail' 反向。 尝试了 1 种模式:['article/(?P<pk> [0-9]+)$']</pk> - Reverse for 'article_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['article/(?P<pk>[0-9]+)$'] 找不到带有关键字参数&#39;{&#39;pk&#39;:&#39;&#39;}&#39;&#39;的&#39;details&#39;。 尝试了1个模式:[&#39;entry \\\\ /(?P <pk> [0-9] +)$&#39;] - Reverse for 'details' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['entry\\/(?P<pk>[0-9]+)$'] 找不到/ answer /反向的NoReverseMatch,反向搜索带有关键字参数&#39;{&#39;pk&#39;:1}&#39;的&#39;detail&#39;。 尝试了1种模式:[&#39;answer / $(?P <pk> \\\\ d +)/ $&#39;] - NoReverseMatch at /answer/ Reverse for 'detail' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['answer/$(?P<pk>\\d+)/$'] 找不到带有关键字 arguments '{'pk': 1}' 的“project_detail”的反向。 尝试了 1 种模式:['projects//int:pk/$'] - Reverse for 'project_detail' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['projects//int:pk/$'] DRF-找不到带有参数&#39;()&#39;和关键字参数&#39;{u&#39;pk&#39;:&#39;&#39;}&#39;的&#39;widget-detail&#39;。 尝试了0个模式:[] - DRF - Reverse for 'widget-detail' with arguments '()' and keyword arguments '{u'pk': ''}' not found. 0 pattern(s) tried: [] 使用参数 &#39;(&#39;&#39;,)&#39; 和关键字参数 &#39;{}&#39; 未找到的 &#39;post_detail&#39; 反转。 尝试了 1 个模式:[&#39;posts/(?P[0-9]+)/$&#39;] - Reverse for 'post_detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['posts/(?P[0-9]+)/$']
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM