【问题标题】:NoReverseMatch at /student/test/ Reverse for 'single_quiz' with arguments '('',)' not found. 1 pattern(s) tried: ['quiz/(?P<quiz_id>[0-9]+)/$']NoReverseMatch at /student/test/ 没有找到带有参数 '('',)' 的“single_quiz”的反向。尝试了 1 种模式:['quiz/(?P<quiz_id>[0-9]+)/$']
【发布时间】:2021-05-10 10:59:51
【问题描述】:

传递 id 时发生严重匹配错误。但手动添加时工作正常。它显示 NoReverseMatch 错误。我无法弄清楚这是从哪里来的。我也应该添加回溯吗?

模板

    <div style="padding: 100px 270px;">
    {% for a in applied%}
    {{a.job.quiz.id}}

    <a href="{% url 'single_quiz' a.job.quiz.id %}">{{a.job.quiz}}</a>
    
    {%endfor%}
    </div>

views.py

def single_quiz(request, quiz_id):
     print(quiz_id)
     quiz = get_object_or_404(Quiz, pk=quiz_id)
     print(quiz)
     num_questions = len(quiz.question_set.all())
     try:

         unique = Result.objects.get(user=request.user,quiz=quiz)

     except Result.DoesNotExist:
          unique = False



      # deletes quiz and returns to home if no questions created
    if num_questions == 0:
         quiz.delete()
         all_quiz_list = Quiz.objects.all()
         context = {
         'all_quiz_list': all_quiz_list,
          }
          return render(request, 'quiz/index.html', context)

    quiz.num_questions = num_questions
    quiz.save()

    # resets accuracy info to 0
    request.session["num_correct"] = 0
    request.session["num_wrong"] = 0

    context = {
        'quiz': quiz,
        'num_questions': num_questions,
        'unique': unique,
    }

    return render(request, 'quiz/single_quiz.html', context)

def TestStudents(request):
     applied=Applicants.objects.filter(applicant=request.user)
     context={
        'applied':applied
      }
     return render(request,'student/test_list.html',context)

urls.py

path('<int:quiz_id>/', views.single_quiz, name='single_quiz'),

【问题讨论】:

  • 尝试发送命名参数。 &lt;a href="{% url 'single_quiz' quiz_id=a.job.quiz.id %}"&gt;{{a.job.quiz}}&lt;/a&gt; 正如 TEST 所述,确保您将正确的上下文从视图传递给模板。
  • 更改和上下文正确后仍然无法正常工作
  • 每个a 都有job 吗?每个job 都有quiz 吗?如果没有,则在 url 生成周围添加 if ... else
  • 不明白if num_questions == 0:下是否是everithing

标签: django django-models django-views django-templates


【解决方案1】:

尝试使用这个:-

    {% for a in quiz %}


    <a href="{% url 'single_quiz' a.id %}">{{a.job.quiz}}</a>
    
    {% endfor %}

在您的整个视图中,我没有看到任何名为 applied 的变量。确保您成功传递了所有变量。

【讨论】:

  • a.id 与测验的 id 不同。不工作
  • 好的,但是我没有在您的 view 上看到 quiz 变量。
猜你喜欢
  • 1970-01-01
  • 2019-09-07
  • 2023-01-16
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2017-11-05
  • 1970-01-01
相关资源
最近更新 更多