NoReverseMatch Debugging
In this lesson, you’ll review what you just saw and practice some more debugging by looking at error messages in Django and get some tips on how to debug a NoReverseMatch
error. If you get a NoReverseMatch
error, there are a few files you’ll want to check:
-
urls.py
: Make sure you have an app name defined as well as a name for your path so you can build valid namespaces. -
your_template.html
: If you’re using URL linking, make sure that the string that you’re using is a valid Django namespace with the name of the app as well as the path. Also make sure that you have an argument that you’re passing correctly, with a space right after the namespace.
Note: The code snippet in the slide around 1:45 should read {'project': project}
. You want to pass the Project object under the singular key so that you’re able to access it via project
in your template.
00:00
Let’s go over what we just talked about and practice some more debugging by looking at error messages in Django, by looking at some tips on how to debug a NoReverseMatch
error. Okay.
00:12
Get ready for debugging. If you get a NoReverseMatch
error, there’s a couple of files and a couple of places you want to check. The first one to go to is urls.py
.
00:23
Make sure that you have an app_name
defined—that should be the name of your app—and make sure that you have a name
for your path()
.
00:31
Whichever path()
you’re linking to, they have to have this name
defined and you have to have the app_name
so that it can build valid namespaces. Next, you’re going to check inside of whichever template you’re linking from.
00:45
In there, make sure that if you use this url
linking, that the string that you’re using in here is a valid Django namespace, which means it consists of the app_name
that we just looked at before, as well as the name
of the path()
. So, sum up number one: the first debugging step that you take is you make sure that Django is able to identify the correct view. For this, it needs two things.
01:11
It needs the app_name
and the name
of the path()
, which are both defined in urls.py
, and then inside of your template, they need to be correctly arranged in the URL template tag.
01:24 The next thing you can go and check—if this doesn’t solve your problem—you want to look inside of your template again and make sure that there’s an argument that you’re passing forward correctly.
01:35 This happens with a space just after the namespace.
01:40 And just to revisit, this is going to be what gets put into the URL at the browser, which eventually gets resolved back into the view and then passed to the view.
01:52
The second thing to check is: are you passing the arguments correctly? For this, again, we have to check in two places. Again, inside of urls.py
, you want to see that if this part exists—if there’s something with angular brackets—then you also need to be passing some arguments inside of your template.
02:11 All right, so let’s get ready for the summary that has all of these things together, so it’s going to look a little messy.
02:21
Here’s our debugging summary. Two main things to check. Make sure that Django is able to identify the correct view. It needs an app_name
and it needs a name
for the path()
, and those have to be put in correctly in the template. And second, in case you’re passing arguments—and you see that with the angled brackets—then you also need to put these inside of your url
tag. And that’s it for our quick revisit, including another debugging step and some tips for handling NoReverseMatch
errors. See you in the next video, where we are going to take a dive into templates.
Martie on May 13, 2020
I got a bit lost earlier on with the views.py code. On the video at about 1:45 you show the “def project-detail()” function.
Everywhere else the .html template is named the same as the function but you have used project_detail()
as the function and details.html
as the template :-). Obviously not wrong, but a challenge to spot.
Is there in fact though an error in the code in the return render()
line where at 1:45 your code shows
{'projects': project})
as opposed to
{'project': project})
Martin Breuss RP Team on May 14, 2020
Hi @Martie! Yes, thanks for noting that. This is indeed a typo. I believe it pops up in another slide too and we’ve added a note there, but missed it here! I’ll look to it that we get an alert box up in the video description to avoid this from causing confusion. Thanks again! :D
bryonwausau on Jan. 24, 2022
I love the approach of working the system from errors I have been trying to get a Django site working and i was ready to pull my hair out.. This has been so helpful.
Mercy Orangi on Feb. 3, 2024
Is this pattern {% url 'appName:pathName' var %}
used only in the achor tag?
Martin Breuss RP Team on Feb. 5, 2024
@Mercy Orangi not necessarily. What the pattern does is to reverse URL mappings, meaning that it generates URLs by using the name of the view or a named URL pattern.
So you’d use it anywhere where you need Django to generate a URL for you. Most often that’ll be inside an anchor tag, but other options could be:
- Form actions
- Redirects
- JavaScript code within a Django template
Become a Member to join the conversation.
reblark on Nov. 13, 2019
Things above are things that I did a “million” times. When I finally found the problem, it was a typo in PyCharm, my typo. I could not see it because of the layout of the frame in PyCharm. Trust me, I went over my code character by character. I finally found it by looking at the error messages many times and they simply told me that I wasn’t passing the right address, but from my perspective I was passing a hard code number or project.pk…Looking more deeply into the error message, I found a display of my code. When I saw it, I thought “that isn’t my code, that’s crazy.” Of course, it was my code. So, I went back to PyCharm and scrolled up and down, left and right. I found it. I couldn’t believe it. It was a result of not paying attention to auto completion and winding up with two extra }}s. That’s all it was and it took me 2 hours of work and two or three messages to you. I would like to make a suggestion. This video is good but only as far as it goes. I did everything this video suggest several times and did not find the error. That’s simply because there is more to debugging than this. As I have mentioned previously, I really like your pressing the point that error messages are your friends. However, one needs to read the messages thoroughly. Just knowing that it’s a “NoReverseMatch” error is incomplete. I would emphasize always, always, looking for typo errors in your code. I just happen to be a very smart guy and an excellent typists. Throughout this course I have been stunned and amazed at the number of either careless, sloppy or stupid mistakes I have made, not in the coding, but in the typing. Dan even sent me a note about one. I have learned a lot from this course about how bad my coding is, not from a theory and rules standpoint. As I improve my typing/coding I will get better and better. I think there may be others who have this particular problem and it might be good to emphasize that as part of your excellent focus on “errors are your friends” message. Cheers, Martin. and, thanks again.