NoReverseMatch Issue with Referenced Urls

Hey so I was having an issue with the url pulling a no reverse match every time I try and open the page and the only way the page will open is if i replace product.id in the index.html with a constant such as 1(represents the product id) I want it to be essentially a variable so for each product added to the admin panel it pulls up a new link that has descriptions for each product. I’ve tried a lot of things so my code might be a little hard to read but thank you for any help!

Error Traceback: Environment:

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

Django Version: 4.0.4
Python Version: 3.10.4
Installed Applications:
[‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘playground.apps.PlaygroundConfig’]
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 C:\Users\Zoro\Desktop\storefront\storefront\playground\templates\products\index.html, error at line 17
Reverse for ‘information’ with arguments ‘(’‘,)’ not found. 1 pattern(s) tried: [‘playground/(?P<product_id>[0-9]+)\Z’]
7 :
8 : Work
9 : Type of Work
10 : Hours needed
11 :
12 :
13 :
14 : {% for context2 in context%}
15 :
16 :
17 : <a href= {%url ‘trog:information’ product.id%} >{{context2.work}}
18 :
19 : {{context2.genre}}
20 : {{context2.hoursWorked}}
21 :
22 : {% endfor %}
23 :
24 :
25 :
26 :
27 : {% endblock %}

Traceback (most recent call last):
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “C:\Users\Zoro\Desktop\storefront\storefront\playground\views.py”, line 11, in hello
return render(request, ‘products/index.html’, context2)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\shortcuts.py”, line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader.py”, line 62, in render_to_string
return template.render(context, request)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py”, line 62, in render
return self.template.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 175, in render
return self._render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 167, in _render
return self.nodelist.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 1000, in render
return SafeString(“”.join([node.render_annotated(context) for node in self]))
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 1000, in
return SafeString(“”.join([node.render_annotated(context) for node in self]))
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 958, in render_annotated
return self.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader_tags.py”, line 157, in render
return compiled_parent._render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 167, in _render
return self.nodelist.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 1000, in render
return SafeString(“”.join([node.render_annotated(context) for node in self]))
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 1000, in
return SafeString(“”.join([node.render_annotated(context) for node in self]))
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 958, in render_annotated
return self.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader_tags.py”, line 63, in render
result = block.nodelist.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 1000, in render
return SafeString(“”.join([node.render_annotated(context) for node in self]))
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 1000, in
return SafeString(“”.join([node.render_annotated(context) for node in self]))
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 958, in render_annotated
return self.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py”, line 238, in render
nodelist.append(node.render_annotated(context))
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py”, line 958, in render_annotated
return self.render(context)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py”, line 472, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\base.py”, line 88, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File “C:\Users\Zoro\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py”, line 802, in _reverse_with_prefix
raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /playground/
Exception Value: Reverse for ‘information’ with arguments ‘(’‘,)’ not found. 1 pattern(s) tried: [‘playground/(?P<product_id>[0-9]+)\Z’]

Please do not post images of code or error messages here. They cannot be quoted for context, they cannot be searched when looking for information, and are unreadable on many devices.

Please copy/paste the relevant code and the text of the traceback message from the console as text within your posting.

When posting code, enclose each section (file, traceback, etc) between lines of three backtick - ` characters. You’ll have a line of ```, then the code (or message, etc), then another line of ```. This forces the software to keep the code properly formatted, which is critical with Python. Also make sure you use the backtick character - ` and not the apostrophe - '. Thanks!

Relevant Code:

Urls.py:

from re import X
from django.urls import path
from . import views


app_name = 'trog'

#products/1
urlpatterns = [
    path('', views.hello, name='hello'),
    path('<int:product_id>', views.detail, name='information')
]

views.py

from django.http import Http404, HttpResponse
from .models import products, typeWork


def hello(request):
    context = typeWork.objects.all()
    context2 = {'context':context}
    #info1 = typeWork.objects.get(id=product_id)
    #info2 = {'info2':info1}
    return render(request, 'products/index.html', context2)

def detail(request, product_id):
    context3 = get_object_or_404(typeWork, id=product_id)
    context4 = {'context4':context3}
    return render(request, 'products/detail.html', context4)```

index.html
{% extends 'base.html' %}


{% block content %}
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Work</th>
                <th>Type of Work</th>
                <th>Hours needed</th>
            </tr>
        </thead>
        <tbody>
            {% for context2 in context%}
                <tr>
                    <td>
                        <a href={%url 'trog:information' product.id%}>{{context2.work}}</a>
                        </td>
                    <td>{{context2.genre}}</td>
                    <td>{{context2.hoursWorked}}</td>
                </tr>
            {% endfor %}
            
        </tbody>
        
    </table>
{% endblock  %}

I fixed it thank you for pointing out how to do it I appreciate your help!

You’re trying to reference the id attribute of a variable named product, but you don’t have anything named product in the context that you are supplying to your template.

I thought i supplied the product_id variable in the detail function in the views file? What would be the correct way to call it into the template?

Templates are rendered with the data supplied in the context. If something isn’t in the context, it’s not available to the template.

I’m sorry i’m new to django so I might not be understanding you correctly but how would I do that since if I add product_id to the hello class parameter(in the views.py file) it would need that secondary parameter and that would be another issue. So how would I call product_id to the template from the detail class? I’m sorry if I’m asking a stupid question I’m just new to this I really appreciate your help!

The context is the dictionary that you pass as a parameter to the render function.

You describe yourself as being new to Django. Have you worked your way through either the Official Django Tutorial or the Django Girls Tutorial? If you haven’t, those really are the best place to start.

If you have, then you might want to review Writing your first Django app, part 3 | Django documentation | Django

Also, you might want to read Templates | Django documentation | Django and The Django template language: for Python programmers | Django documentation | Django

I’ve reviewed those tutorials and I still don’t seem to see the solution to fix this issue. It seems like its taking the parameters from the hello class and not the detail class even though I’ve specified the path to the detail class through the named path of information. I have also attempted to add product_id to the hello class in order to pass it through in context but it either gives me a type error for not providing a second parameter, but nothing seems to work I’m just confused why it’s not calling from the detail class

Generally speaking, just “reviewing” the tutorials isn’t enough. To get the most out of them, you need to work your way through them - and that includes typing all to code and examples to gain an understanding of what every line of code is doing. By the time you’re done, you should be able to look at every line of code in those tutorials and understand what each line does.

Sorry figured it out was using the wrong variable i should have been using context2.id instead of product_id that’ll teach me to use very similar variables

1 Like