NoReverseMatch error

i don’t know this one what do you think ? by the way i am so happy to get the person like you …thank you so much for your time sir!!

Read the referenced docs and then tell me what you think.

i think it is about using the key word args to pass the arguments in the Redirect so i think the above line of code should be like this

return redirect('new-question', args=[ course_id, module_id,quiz_id])

First, I made a mistake. You’re using redirect(... and I’ve been reading it as reverse(.... I pointed you to the wrong section of the docs - not that it makes a huge difference in this case.

You made a really good guess - but that’s not what the error message is telling you:

From the error message:

In the function call:

What is “new-question” supposed to be?
I’m looking for a general answer, not specific to this example.
The other way to phrase this is to answer the question, What is the first parameter of the redirect function call a reference to?

From the correct docs at Django shortcut functions | Django documentation | Django

  • A model: the model’s get_absolute_url() function will be called.
  • A view name, possibly with arguments: reverse() will be used to reverse-resolve the name.
  • An absolute or relative URL, which will be used as-is for the redirect location.

So for this specific case, which of these three items is “new-question”?

in my case ‘new-question’ is used in the quiz urls.py file to call the NewQuestion view

path('<course_id>/quiz/<quiz_id>/newquestion', views.NewQuestion, name='new-question'),
  • Is this in an app named quiz?
    • Do you have an apps.py file for that app?
      • If so, what is the name attribute in that file?

If you have this in an app named quiz, you may need to reference that named url as quiz:new-question.
See URL dispatcher | Django documentation | Django

yes it is an app and app.py file says like

class QuizConfig(AppConfig):
    name = 'quiz'

and i fixed that when i added quiz:new-question but here is another problem

NoReverseMatch at /quiz/1/modules/1/quiz/newquiz

Reverse for ‘new-question’ with keyword arguments ‘{‘course_id’: ‘1’, ‘module_id’: ‘1’, ‘quiz_id’: 13}’ not found. 1 pattern(s) tried: [‘quiz/(?P<course_id>[^/]+)/quiz/(?P<quiz_id>[^/]+)/newquestion$’]

i don’t know the reason

What does your function call look like at this point?

How many parameters are you passing?

How many parameters is your url expecting?

it worked sir…tank you so much for your time , but i want to talk with you in private can i?

I’m sorry, but no, I don’t do private consulations.

i don’t want private consultations…just i want you to talk some other stuff but it OK if you don’t want i will be back when i get stack.

how can i use the timer in my quiz app ?

This is a new and different topic - I suggest you open up a new subject for it, including some specifics as to what you’re trying to achieve. This includes defining what you’re trying to time and what actions you want to perform based upon timing.

For example, are you looking to track the total time it takes to complete the quiz? That’s going to get a slightly different answer than if you wanted to track the time taken for each question.

I want to track the total time it takes to complete the quiz? and after the time is completed the quiz page is hidden and see the result

i got ValueError at /quiz/None/newquestion

Field ‘id’ expected a number but got ‘None’.
from the code

def NewQuiz(request):
    quizForm=forms.QuizForm()
    if request.method=='POST':
        quizForm=forms.QuizForm(request.POST)
        if quizForm.is_valid():        
           quiz= quizForm.save(commit=False)
           quiz_id = quiz.id
        else:
            print("form is invalid")
        return redirect('quiz:new-question',quiz_id=quiz_id)
    return render(request,'quiz/createquiz.html',{'quizForm':quizForm})

#create new question
def NewQuestion(request,quiz_id):
	user = request.user
	quiz = get_object_or_404(Quizzes, id=quiz_id)
	questionForm=forms.QuestionForm()
	if request.method=='POST':
		questionForm=forms.QuestionForm(request.POST)
		if questionForm.is_valid():
			question=questionForm.save(commit=False)
			quiz=models.quiz.objects.get(id=request.POST.get('quizID'))
			question.quiz=quiz
			question.save() 
		else:
			print("form is invalid")
		return redirect('quiz:new-question',quiz_id=quiz_id)
	return render(request,'quiz/createqusetion.html',{'questionForm':questionForm})

what do you the this error is comes from?

Which line is giving you that error?

What is the url being invoked that takes you to the view that is throwing the error?

the error is coming from this line

quiz = get_object_or_404(Quizzes, id=quiz_id)

and the url is

 path('quiz/<quiz_id>/newquestion', views.NewQuestion, name='new-question'),

Ahhh, I see the issue now.

You are on page /quiz/None/newquestion. Your url pattern is quiz/<quiz_id>/newquestion, so the string None is being applied to the quiz_id parameter.

Whatever is generating that url is what’s causing the problem. It’s sending you to an improper url.

yes that is exactly what i am getting…so what can i do to fix this problem?

You need to fix whatever it is that is sending you to that url.

What link / page / menu entry / whatever are you clicking on to send you to that location? That’s the page that needs to be fixed. It’s building a bad url and sending you to a url that’s not going to work.