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

I followed a tutorial on django blog creation and I keep getting this error

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

I have two pages from which I want to link to the blog details page, the home page and index page.

It does link from the home view like so

		<a class="links" href="{% url 'article_details_view_url' post.pk %}">{{post.title}}</a>
class HomeView(ListView):
	model = Post
	template_name = 'home.html'
	ordering=['-date_posted']
	#ordering=['-id']

	def get_context_data(self, *args,**kwargs):
		cat_menu = Category.objects.all()
		context = super(HomeView, self).get_context_data(*args,**kwargs)
		context['cat_menu']=cat_menu
		return context

but not from the index view

 	<a class="links" href="{% url 'article_details_view_url' post.id %}">{{post.title}}</a>
def index(request):
	latest_posts = Post.objects.all() 
	return render(request,'index.html',{
			'latest_posts':latest_posts,
		})

why is this happening? Can someone direct me to where I need to look to resolve this? I’m new to this and the documentation is too overwhelming.

NEVERMIND: Got it to work.

I created a for loop on the index page like so–dang it

 		{% for post in latest_posts %}
    	<a class="links" href="{% url 'article_details_view_url' post.pk %}">{{post.title}}</a>
      	<p>{{post.body|slice:100|safe}}</p>
 		{% endfor %}

what does you urls.py file look like?

In any case, you might need to do:

{% url 'article_details_view_url' pk=post.pk %}

or

{% url 'article_details_view_url' post_id=post.pk %}

or something similar.

Alternatively (and probably more human/seo friendly, also would not expose your primary keys to the outside world) would be smth like this:

urls.py

from django.urls import path
from .views import ArticleView

app_name = 'blog'

urlpatterns = [
    <snip>
    path(
        '<slug:slug>',
        ArticleView.as_view(),
        name='article_detail
    ),
    <snip>
]

and call from the templates:

<a href="{% url 'blog:article_detail' slug=post.slug %}">{{ post.title }}</a>

**kwargs, not *args.

The above would require your model to include a slug field of course. Model field reference | Django documentation | Django

1 Like

Your solution worked like a charm

<a href="{% url 'show_user_profile_view_url' **pk=user.profile.pk** %}">view profile</a>

thank you

Hi, would you know why this pk=user.profile.pk works on my local machine but no when I upload the website to pythonanywhere.com?

I get the reverse match error again and I haven’t touched the code since.

Please help

I have no idea.
Do you need to restart, gracefully or not for changes to be applied?
Do you have a console/error output somewhere so you can look at it?

No error log, just the usual NoReverseMatch error. It only happens on live server, but works perfectly in local machine, bummers :confused:

nothing in uwsgi/nginx/whatever you are using and the logs they do generate?

something like:

$ tail -f /var/log/uwsgi/my_site.log

and press F5 ?

Also, do you see that yellow Django error page on your live server? If so, you want to avoid that at all costs, as it will expose stuff you do not want to be exposed out there.
Set DEBUG = False in your settings for prod, at the very least?

my bad, you’re on pythonanywhere… duh :slight_smile:

what’s the exact error? The whole trace?

Hello,

Here is the error

NoReverseMatch at /

Reverse for ‘show_user_profile_view_url’ with keyword arguments ‘{‘pk’: ‘’}’ not found. 1 pattern(s) tried: [‘members/(?P[0-9]+)/user_profile/\Z’]

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[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: Mon, 04 Apr 2022 07:53:50 +0000

Thank you

Yes, we do see the yellow error page,

Here is the error log

  File "/home/90skidcoder/.virtualenvs/blog2-virtualenv/lib/python3.8/site-packages/django/urls/resolvers.py", line 802, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: 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']

well it says what it says :slight_smile:

'show_user_profile_view_url' with keyword arguments '{'pk': ''}'

It means you are trying to call that named url with an (kw)argument that is an empty string. Whatever that argument is, it does not seem to be there. Do you properly expose your user? does you user have a profile indeed?
Print the user as a dict in the console, or somewhere, or find a way to inspect it (and its profile) to find out why this is not working.

no idea what you’re talking about.

i followed the tutorial on youtube precisely, for him it worked and for me it doesn’t.

1 Like

This means that in this:

{% url 'show_user_profile_view_url' pk=user.profile.pk %}

either:

  1. user is not defined or exposed to the template
  2. user.profile is not defined or exposed to the template
  3. or that somehow user.profile does not have any pk attribute.

Dump you user object in the logs somewhere, so for example in you views file:

print(user.__dict__)

and check that the user object does indeed have what your template needs it to have…

what do you mean by “exposed”? Exist you mean?

the user exist, its the admin user the superuser that i use to test the app, I dont understand what exposed mean

do I have to return the user pk in the dictionary like so?

Again we have the same issue with another app, why is this happening?

def article(request,pk):
	article = BlogArticle.objects.get(pk=pk)
	return render(request,'article.html', {
			'article':article,
	

		})

{% for blog_article in blog_articles %}
			 	<div class="col">

				    <div class="card h-100">
				    	
					    <img src="..." class="card-img-top" alt="...">

						    <div class="card-body">

							    <h5 class="card-title"><a href="{% url 'article_view_url' article.pk %}">{{blog_article.title}}</a></h5>
							    <p class="card-text">{{blog_article.headline}}</p>

						    </div>

						    <div class="card-footer">

						    	<small class="text-muted">Last updated 3 mins ago</small>

						    </div>
				    	
				    </div>
			  	</div>
			  	{% endfor %}
urlpatterns = [
    path('', views.index, name='index_view_url'),
    path('article/<int:pk>/', views.article, name='article_view_url'),
    
]

please help

what is wrong with this code?

by “exposed” I mean taken by the view and exposed to the template. Via the context dictionary, usually.

In your latest reply, shouldn’t

{% url 'article_view_url' article.pk %}

be:

{% url 'article_view_url' pk=article.pk %}

?

its still not working after changing to

{% url 'article_view_url' pk=article.pk %}

did we wrong django or insult in any way? i mean, seriosuly, literally every solution on stackoverflow on reddit and where not and still not working.

should we just give up and move on? I dont know what else to do.

2 Likes

maybe its something in the settings.py?

I dont know what else to thing of