Why do I keep getting this error all the time? NoReverseMatch at /

don’t give up – Django is an excellent framework.

we get this error now:

Reverse for 'article_view_url' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['article/(?P<pk>[0-9]+)/\\Z']

here is our models.py file in case it helps:

class BlogArticle(models.Model):
	title = models.CharField(max_length=300)
	headline = models.CharField(max_length=300)
	short_description = models.TextField(max_length=3000)
	article_content = models.TextField(max_length=3000)
	author = models.ForeignKey(User, on_delete=models.CASCADE,null=True)

	def __str__(self):
		return self.title or  ''

maybe there is something amss here

actually:

{% for blog_article in blog_articles %}
{% url 'article_view_url' pk=article.pk %}
{% endfor %}

should be:

{% for blog_article in blog_articles %}
{% url 'article_view_url' pk=blog_article.pk %}
{% endfor %}

wrong variable name there: from article to blog_article Try this first.

it worked, thank you :grinning:

I’m sorry to bother you again.

In one app its working fine, in the new app still its not.

navbar.html


  {% if user.is_authenticated %}

      
        <ul class="navbar-nav ml-auto mb-2 mb-lg-0">

        <li class="nav-item"><a class="nav-link" href="{% url 'logout' %}">logout</a> </li>

        <li class="nav-item"><a class="nav-link" href="{% url 'show_user_profile_view_url' pk=user.profile.pk %}">Signed in as ({{user.username}})</a></li>


{% else %}

views.py

class ShowProfilePageView(generic.DetailView):
	model = Profile 
	template_name = 'registration/user_profile.html'

	def get_context_data(self, *args,**kwargs):
		users = Profile.objects.all()
		context = super(ShowProfilePageView, self).get_context_data(*args,**kwargs)
		page_user = get_object_or_404(Profile,id=self.kwargs['pk'])
		context['users']=users
		context['page_user']=page_user
		return context

we get the same error: could this have something to do with the fact that it’s inside an if statement?


Request Method:	GET
Request URL:	http://90skidcoder.pythonanywhere.com/
Django Version:	4.0.3
Exception Type:	NoReverseMatch
Exception Value:	
Reverse for 'show_user_profile_view_url' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['members/(?P<pk>[0-9]+)/user_profile/\\Z']
Exception Location:	/home/90skidcoder/.virtualenvs/blog2-virtualenv/lib/python3.8/site-packages/django/urls/resolvers.py, line 802, in _reverse_with_prefix
Python Executable:	/usr/local/bin/uwsgi
Python Version:	3.8.5
Python Path:	
['/home/90skidcoder/blog2/blog2',
 '/var/www',
 '.',
 '',
 '/var/www',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/home/90skidcoder/.virtualenvs/blog2-virtualenv/lib/python3.8/site-packages']
Server time:	Wed, 06 Apr 2022 02:08:29 +0000
Error during template rendering
In template /home/90skidcoder/blog2/blog2/blog2_app/templates/navbar.html, error at line 38

Reverse for 'show_user_profile_view_url' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['members/(?P<pk>[0-9]+)/user_profile/\\Z']
28	      <form class="d-flex">
29	        <input class="search_form" type="search" placeholder="Search" aria-label="Search"> &nbsp
30	        <button class="search_btn" type="submit">Search</button>
31	      </form>
32	
33	            <ul class="navbar-nav ml-auto mb-2 mb-lg-0">
34	
35	
36	 <li class="nav-item"><a class="nav-link" href="{% url 'logout' %}">logout</a> </li>
37	
38	 <li class="nav-item"><a class="nav-link" href="{% url 'show_user_profile_view_url' pk=page_user.pk %}">Signed in as ({{user.username}})</a></li>
39	
40	{% else %}
41	
42	
43	
44	
45	       <li class="nav-item"><a class="nav-link" href="{% url 'login' %}">login</a> </li>
46	        <li class="nav-item"><a class="nav-link" href="{% url 'register_view_url' %}">create account</a> </li>
47	{% endif %}
48	

please help!!

If you have not yet worked your way through the official Django tutorial, you probably should take a step back and do that at this time.

If you have, then you should review Step 3 to remind yourself how templates, views, and urls work together to generate your pages and the references to them.

i read this already, why do you keep sending me these links?

again, check your variable names…

page_user versus user.

Just “reading” the tutorials isn’t enough. To really learn what it’s telling you, you need to work your way through the tutorials to really understand what it’s telling you. There’s something in the act of physically typing in the code shown - and seeing the results - that enhances the learning process and improves retention and comprehension of the material.

You have not displayed that you understand what the context is when rendering a template, or how that relates to the url tag in your template. The tutorial will teach you that.

Please explain what you mean by variable names, please

what is wrng with this code

class UserProfile(generic.DetailView):
	model = Profile
	template_name = 'registration/user_profile.html'

	def get_context_data(self, *args,**kwargs):
		users = Profile.objects.all()
		context = super(UserProfile, self).get_context_data(**args,**kwargs)
		page_user = get_object_or_404(Profile, id=self.kwargs['pk'])

		context['users']=users
		context['page_user']=page_user
		
		return context
	<li><a href="{% url 'user_profile' pk=page_user.pk %}">view profile</a></li>

I get a different error now,

Page not found (404)
No profile found matching the query
Request Method:	GET
Request URL:	http://127.0.0.1:8000/members/user_profile/2
Raised by:	members.views.UserProfile

please help

I get this error now after fixing the profile page

Not Found: /members/user_profile/1
[08/Apr/2022 07:05:07] "GET /members/user_profile/1 HTTP/1.1" 404 5011

Have you done some coding before? No offense intended, this is an actual question.

As for the error … it looks normal:

page_user = get_object_or_404(Profile, id=self.kwargs['pk'])

the clue is in get_object_or_404. Do you know what this does? Or have you been “coding” these lines via copy and paste?

I followed the tutorial, i have no idea what im doing wrong.

what is wrong wit this line?

page_user = get_object_or_404(Profile, id=self.kwargs['pk'])

what is the clue??? just tell me…teach meeee
DO I REALLY NEED TO GIVE UP BECAUSE OF THIS???

can you just explain instead of asking questions? Seriously, this is the only thing thats in my way, everything else is working perfectly until this. How do i resolve this?

How can it not work when i followed the tutorial step by step??? What did i type wrong?

Ive been trying to make it work for a month, ive had it already

whats the clue in this line?

page_user = get_object_or_404(Profile, id=self.kwargs['pk'])

does that mean that the page is missing or the use us missing?
before I quit ets give it another chance.

can you please point that out for me?

Have you read the docs for get_object_or_404? Can you state in your own words what that function does?

yes. I know what this means, the model is there though why can’t it find it? Its the Profile model

get_object_or_404()

Calls get() on a given model manager, but it raises Http404 instead of the model’s DoesNotExist exception.

THE model is there though, its the Profile model, so what can’t it find???

No, apparently, you do not have an instance of the Profile model with that primary key.