NoReverseMatch error

any one who can briefly tell me about NoReverseMatch error with example please?

Start with django.urls utility functions | Django documentation | Django.

If you’ve got a specific question or issue, please post the details and we’ll try to resolve it here.

If this is a continuation of NoReverseMatch at/, please do not repost the same issue - continue the discussion there.

i got some specific problem i have a view that used to save the quiz form like this

def NewQuiz(request, course_id,module_id):
	user = request.user
	module = get_object_or_404(Module, id=module_id)
	if request.method == 'POST':
		form = NewQuizForm(request.POST)
		if form.is_valid():
			a = Quizzes()
			quiz = NewQuizForm.save(commit=False)
			quiz.module=module
			quiz.save() 
			module.save()
			return redirect('new-question', course_id=course_id,  quiz_id=quiz_id)  

			#course=models.Course.objects.get(id=request.POST.get('course_id'))
           
	else:
		form = NewQuizForm()

	context = {
		'form': form,
	}
	return render(request, 'quiz/newquiz.html', context)

and the error is "save() missing 1 required positional argument: ‘self’ "

Please provide the full traceback of the error.

Traceback (most recent call last):
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py”, line 34, in inner
response = get_response(request)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py”, line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py”, line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “C:\Users\user\Desktop\cv\e learning\learn\quiz\views.py”, line 19, in NewQuiz
quiz = NewQuizForm.save(commit=False)
TypeError: save() missing 1 required positional argument: ‘self’
[26/Dec/2021 22:15:41] “POST /quiz/1/modules/1/quiz/newquiz HTTP/1.1” 500 80644

Your instance of the form you are working with is named form (the first line of this excerpt).

That is what you want to save at the last line of this portion:
quiz = form.save(commit=False)

See Creating forms from models | Django documentation | Django for more details.

Tank you so much sir

may can i ask something else?

Absolutely! This is a “conversational” location, not an “ask one question, get one answer” type of site.

after i saved the quiz it redirect like this

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

but i got the error
Traceback (most recent call last):
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py”, line 34, in inner
response = get_response(request)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py”, line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py”, line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “C:\Users\user\Desktop\cv\e learning\learn\quiz\views.py”, line 25, in NewQuiz
return redirect(‘new-question’, course_id=course_id, module_id=module_id, quiz_id=quiz_id)
NameError: name ‘quiz_id’ is not defined
[26/Dec/2021 23:19:56] “POST /quiz/1/modules/1/quiz/newquiz HTTP/1.1” 500 80658
what can i do to fix this?

What is that error message telling you?

Or, to be more specific, what part of that line is it complaining about?

the message is

NameError at /quiz/1/modules/1/quiz/newquiz

name ‘quiz_id’ is not defined
the line is what i wrote in the last replay and the code is here

ef NewQuiz(request, course_id,module_id):
	user = request.user
	module = get_object_or_404(Module, id=module_id)
	if request.method == 'POST':
		form = NewQuizForm(request.POST)
		if form.is_valid():
			a = Quizzes()
			quiz = form.save(commit=False)
			quiz.module=module
			quiz.save() 
			#quiz.id=quiz_id
			module.save()

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

			#course=models.Course.objects.get(id=request.POST.get('course_id'))
           
	else:
		form = NewQuizForm()

	context = {
		'form': form,
	}
	return render(request, 'quiz/newquiz.html', context)


def NewQuestion(request, course_id,module_id,  quiz_id):
	user = request.user
	quiz = get_object_or_404(Quizzes, id=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,  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', course_id=course_id,  module_id=module_id, quiz_id=quiz.id)
	else:
		form = NewQuestionForm()

	context = {
		'form': form,
	}
	return render(request, 'quiz/newquestion.html', context)

Ok, this was a more targeted question.

You posted this line:

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

which is the line throwing the error.

What part of this line do you think the error is referring to?

And, the second question is:

What do you think the cause of this error would be?

i think quiz_id is not defined in NewQuiz function but i was think that when i save the quiz in quiz.save() method the quiz_id was also crated for that quiz…but my thought is not worked so when i think onther way of getting the quiz_id of the crating quiz ```
quiz.id=quiz_id

Correct. You have no variable defined named quiz_id. What you are trying to reference here is the id attribute of the quiz object, which is quiz.id, not quiz_id. Yes, there are a small number of places where you can use either one almost interchangably (within the ORM), but this is not one of them.

so what do you think to fix the problem

i mean when i tray like this

quiz_id = quiz.id

another probleme is come

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

Reverse for ‘new-question’ not found. ‘new-question’ is not a valid view function or pattern name.

Ok, so we’ve solved the quiz.id issue, now it’s a different problem.

Again, what do you think the cause of this error would be?

i think still the problem is that because in the Traceback i saw the previous line of code which where the error was coming
Traceback (most recent call last):
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py”, line 34, in inner
response = get_response(request)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py”, line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py”, line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “C:\Users\user\Desktop\cv\e learning\learn\quiz\views.py”, line 25, in NewQuiz
return redirect(‘new-question’, course_id=course_id, module_id=module_id, quiz_id=quiz.id)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py”, line 41, in redirect
return redirect_class(resolve_url(to, *args, **kwargs))
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py”, line 131, in resolve_url
return reverse(to, args=args, kwargs=kwargs)
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\base.py”, line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File “C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\resolvers.py”, line 677, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for ‘new-question’ not found. ‘new-question’ is not a valid view function or pattern name.
[26/Dec/2021 23:45:03] “POST /quiz/1/modules/1/quiz/newquiz HTTP/1.1” 500 99897

Yes, you are correct, this is the right error.

What do you think might cause this error?