hello there …was crating a quiz app for my e learning project and i faced NoReverseMatch probelm my all code is here
models.py:
from django.db import models
from django.contrib.auth.models import User
from courses.models import Modules,Courses
class Answer(models.Model):
answer_text = models.CharField(max_length=900)
is_correct = models.BooleanField(default=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.answer_text
class Question(models.Model):
question_text = models.CharField(max_length=900)
answers = models.ManyToManyField(Answer)
points = models.PositiveIntegerField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.question_text
class Quizzes(models.Model):
module=models.ForeignKey(Modules,related_name='Quizzes',on_delete=models.CASCADE)
title = models.CharField(max_length=200)
description = models.CharField(max_length=200)
date = models.DateTimeField(auto_now_add=True)
due = models.DateField()
allowed_attempts = models.PositiveIntegerField()
time_limit_mins = models.PositiveIntegerField()
questions = models.ManyToManyField(Question)
def __str__(self):
return self.title
class Attempter(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
quiz = models.ForeignKey(Quizzes, on_delete=models.CASCADE)
score = models.PositiveIntegerField()
completed = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.user.username
class Attempt(models.Model):
quiz = models.ForeignKey(Quizzes, on_delete=models.CASCADE)
attempter = models.ForeignKey(Attempter, on_delete=models.CASCADE)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
answer = models.ForeignKey(Answer, on_delete=models.CASCADE)
def __str__(self):
return self.attempter.user.username + ' - ' + self.answer.answer_text
class Completion(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
course = models.ForeignKey(Courses, on_delete=models.CASCADE)
completed = models.DateTimeField(auto_now_add=True)
quiz = models.ForeignKey(Quizzes, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.user.username
class Completion(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
course = models.ForeignKey(Courses, on_delete=models.CASCADE)
completed = models.DateTimeField(auto_now_add=True)
quiz = models.ForeignKey(Quizzes, on_delete=models.CASCADE, blank=True, null=True)
#assignment = models.ForeignKey(Assignment, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.user.username
my view.py
from django.shortcuts import render, redirect, get_object_or_404,reverse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
from quiz.forms import NewQuizForm, NewQuestionForm
from quiz.models import Answer, Question, Quizzes, Attempter, Attempt,Completion
from django.contrib.auth.models import User
from courses.models import Modules,Courses
# Create your views here.
def NewQuiz(request, courses_id, modules_id):
user = request.user
module = get_object_or_404(Modules, id=modules_id)
new_quiz=None
if request.method == 'POST':
quiz_form = NewQuizForm(request.POST)
if quiz_form.is_valid():
new_quiz=quiz_form.save(commit=False)
new_quiz.module = module
new_quiz.save()
return reverse('quiz:new-question', courses_id=courses_id, modules_id=modules_id, new_quiz_id=new_quiz.id)
else:
quiz_form = NewQuizForm()
context = {
'quiz_form': quiz_form,
}
return render(request, 'quiz/newquiz.html', context)
def NewQuestion(request, course_ids, module_ids, new_quiz_id):
user = request.user
quiz = get_object_or_404(Quizzes, id=new_quiz_id)
if request.method == 'POST':
form = NewQuestionForm(request.POST)
if form.is_valid():
question_text = form.cleaned_data.get('question_text')
points = form.cleaned_data.get('points')
answer_text = request.POST.getlist('answer_text')
is_correct = request.POST.getlist('is_correct')
question = Question.objects.create(question_text=question_text, user=user, points=points)
for a, c in zip(answer_text, is_correct):
answer = Answer.objects.create(answer_text=a, is_correct=c, user=user)
question.answers.add(answer)
question.save()
quiz.questions.add(question)
quiz.save()
return redirect('new-question', courses_id=courses_id, modules_id=modules_id, new_quiz_id=new_quiz_id)
else:
form = NewQuestionForm()
context = {
'form': form,
}
return render(request, 'quiz/newquestion.html', context)
def QuizDetail(request, courses_id, modules_id, new_quiz_id):
user = request.user
quiz = get_object_or_404(Quizzes, id=quiz_id)
my_attempts = Attempter.objects.filter(quiz=quiz, user=user)
context = {
'quiz': quiz,
'my_attempts': my_attempts,
'courses_id': courses_id,
'modules_id': modules_id,
}
return render(request, 'quiz/quizdetail.html', context)
def TakeQuiz(request, courses_id, modules_id, new_quiz_id):
quiz = get_object_or_404(Quizzes, id=new_quiz_id)
context = {
'quiz': quiz,
'courses_id': courses_id,
'modules_id': modules_id,
}
return render(request, 'quiz/takequiz.html', context)
def QuizDetail(request, courses_id, modules_id, new_quiz_id):
user = request.user
quiz = get_object_or_404(Quizzes, id=new_quiz_id)
my_attempts = Attempter.objects.filter(quiz=quiz, user=user)
context = {
'quiz': quiz,
'my_attempts': my_attempts,
'courses_id': course_id,
'modules_id': module_id,
}
return render(request, 'quiz/quizdetail.html', context)
def SubmitAttempt(request, courses_id, modules_id, new_quiz_id):
user = request.user
quiz = get_object_or_404(Quizzes, id=new_quiz_id)
earned_points = 0
if request.method == 'POST':
questions = request.POST.getlist('question')
answers = request.POST.getlist('answer')
attempter = Attempter.objects.create(user=user, quiz=quiz, score=0)
for q, a in zip(questions, answers):
question = Question.objects.get(id=q)
answer = Answer.objects.get(id=a)
Attempt.objects.create(quiz=quiz, attempter=attempter, question=question, answer=answer)
Completion.objects.create(user=user, courses_id=courses_id, quiz=quiz)
if answer.is_correct == True:
earned_points += question.points
attempter.score += earned_points
attempter.save()
return redirect('index')
def AttemptDetail(request, courses_id, modules_id, new_quiz_id, attempt_id):
user = request.user
quiz = get_object_or_404(Quizzes, id=new_quiz_id)
attempts = Attempt.objects.filter(quiz=quiz, attempter__user=user)
context = {
'quiz': quiz,
'attempts': attempts,
'courses_id': courses_id,
'modules_id': modules_id,
}
return render(request, 'quiz/attemptdetail.html', context)
urls.py:
from django.urls import path
from quiz.views import NewQuiz, NewQuestion, QuizDetail, TakeQuiz, SubmitAttempt, AttemptDetail
app_name ='quiz'
urlpatterns = [
path('<courses_id>/modules/<modules_id>/quiz/newquiz/', NewQuiz, name='new-quiz'),
path('<courses_id>/modules/<modules_id>/quiz/<new_quiz_id>/detail/', QuizDetail, name='quiz-detail'),
path('<courses_id>/modules/<modules_id>/quiz/<new_quiz_id>/newquestion', NewQuestion, name='new-question'),
path('<courses_id>/modules/<modules_id>/quiz/<new_quiz_id>/take', TakeQuiz, name='take-quiz'),
path('<courses_id>/modules/<modules_id>/quiz/<new_quiz_id>/take/submit', SubmitAttempt, name='submit-quiz'),
path('<courses_id>/modules/<modules_id>/quiz/<new_quiz_id>/<attempt_id>/results', AttemptDetail, name='attempt-detail'),
]
in the templates
<div class="card pt-3 pb-3 text-center">
<a href="{% url 'quiz:new-quiz' courses.id modules.id %}"> Create Exam </a>
</div>
any one who can help me please