NoReverseMatch at /

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

Django version: 3.2.7

Python Version: 3.9.5

SO i am scratching my head on my this is not working… Please Help

My Views.py


def Index(request):

R = Retailers.objects.all()

A = []

for a in R: `` `

`A.append(a.Color)``

C =Retailers.objects.filter(Color = random.choice(A))

context = {
“C”: C }

return render(request,"Prods/base.html",context)

class PostDetailView(DetailView):

model = Retailers

template_name = 'Prods/Detail.html'

My Urls.py:

from django.urls import path

from . import views

from .views import PostDetailView, SearchResultsView

urlpatterns = [

path("Products/<int:pk>/", PostDetailView.as_view(), name = 'Products-Page'),

path('search/', SearchResultsView.as_view(), name='search_results'),

path("",views.Index, name = "Home"),

path("Color/<str:colors>/", views.home, name='blog-home'),

]

My Html Template FIle (Prods/Base.html)

{% block content %}

{% for c in C %}

<div class="pa2 mb3 striped--near-white">

<div class="pl2">

<a href = {{ c.Links }}> <p class="mb2"><b>{{ c.Title }}</b> </a>

</p>

<img src = "data:image/png;base64,{{c.Images}}" >

<a href="{% url 'Products-Page' Products.id %}"> <b> {{ c.Title}}</b> </a>

<br>

<br>

</div>

</div>

{% endfor %}

{% endblock %}

What is the path the full path of My Urls.py:?
Given that it contains a from .views import PostDetailView, SearchResultsView it looks like it is within an apps urls.py, and not the projects urls.py.

In the project urls you have to tell django to include these urls, e.g.

from django.urls import include, path

urlpatterns = [
    path('', include('Prods.urls')), 
]

PS you should read PEP8, I recommend following standard python style guides and naming conventions.

hi, i have included that in the main urls.py where the settings.py file resides

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Prods.urls'))
]

it works when i go to a singular product page,

localhost/Products/1

this works perfectly,

but when i include it on html part, it gives the error

I think if you had posted the complete traceback it would probably be more clear.

You have this line in your template:
<a href="{% url 'Products-Page' Products.id %}"> <b> {{ c.Title}}</b> </a>

But, I don’t see anything in your view where you’re supplying something called Products in your context.

(For your future reference, when asking about an error message you receive, it really is important to post the complete error.)

i am sorry, but why should we have to render products ?

because i am passing the url pattern right ?

I followed this video here and they do not render anything. Sorry if i am wrong

My full Error:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 3.2.7
Python Version: 3.9.5
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Prods']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/firaki/Desktop/images/blog/Prods/templates/Prods/base.html, error at line 66
   Reverse for 'Detail-Page' with arguments '('',)' not found. 1 pattern(s) tried: ['Products/(?P<pk>[0-9]+)/$']
   56 : 
   57 :             <div class="pa2 mb3 striped--near-white">
   58 : 
   59 :                 <div class="pl2"> 
   60 : 
   61 :                     <a href = {{ c.Links }}> <p class="mb2"><b>{{ c.Title }}</b> </a>
   62 :                     </p>
   63 : 
   64 :                     <img src = "data:image/png;base64,{{c.Images}}">
   65 : 
   66 :                     <a href=" {% url 'Detail-Page' Products.id %} "> Click here to go to Product Page</a>
   67 :                   
   68 : 
   69 :                     <br>
   70 : 
   71 :                     <br>
   72 : 
   73 :                 </div>
   74 : 
   75 :             </div>
   76 : 


Traceback (most recent call last):
  File "/home/firaki/.local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/firaki/Desktop/images/blog/Prods/views.py", line 32, in Index
    return render(request,"Prods/base.html",context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/loader_tags.py", line 53, in render
    result = self.nodelist.render(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/template/defaulttags.py", line 446, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/urls/base.py", line 86, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "/home/firaki/.local/lib/python3.9/site-packages/django/urls/resolvers.py", line 694, in _reverse_with_prefix
    raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'Detail-Page' with arguments '('',)' not found. 1 pattern(s) tried: ['Products/(?P<pk>[0-9]+)/$']

Any data that you render in your template must be supplied to the template through the context.

Note the complete error:

Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'Detail-Page' with arguments '('',)' not found. 1 pattern(s) tried: ['Products/(?P<pk>[0-9]+)/$']

It’s not saying that there’s no reverse for “Detail-Page”.

It’s saying that there’s no reverse for ‘Detail-Page’ with no arguments.

(Also, this error output is not the same as what you reported originally. At the top you were referring to a “Products-Page”, now you’re referring to a “Details-Page”. Are we talking about the same issue here?)

i am so sorry, and i finally fixed it,

Instead of Products.id i had to do c.id

Phew, finally Thanks you :slight_smile:

Also, I’d be extremely wary about watching a 3 year old tutorial. I’m not going to spend any time watching it myself, but I’ll guarantee it’s significantly out-of-date and likely obsolete.