传递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'),
尝试使用这个:-
{% for a in quiz %}
<a href="{% url 'single_quiz' a.id %}">{{a.job.quiz}}</a>
{% endfor %}
我在您的整个视图中没有看到任何名为
applied
的变量。确保您成功地传递了所有变量。