how to make sub pages url in django

1k views Asked by At

In my online store django project, I want to create a sub-page which shows the details of a product that is listed in a page.

in urls.py I created the urls for both pages like bellow:

path('wwqm', views.water_quality_sensor  , name='wwqm'),
path('wwqm/<str:water_sensor_title>', views.water_sensor_item, name='water_sensor_item'),

in this scenario, a bunch of items are being shown in wwqm. user click on a product and the water_sensor_item should load up.

I know its not important but here is views.py:

def water_quality_sensor(request):

    queryset_list = Water_quality_sensor.objects.order_by('-product_name').filter(is_published=True)

    context = {
                'water_quality_sensor': queryset_list,
            }

    return render(request, 'products/water_quality_sensor.html', context)

def water_sensor_item(request, water_sensor_title):

    water_sensors =  get_object_or_404(Water_quality_sensor, title=water_sensor_title)
    context = {
        'water_sensors': water_sensors
    }
    return render(request, 'products/water_sensor_item.html' , context)

I try to build the url for each item based on a parameter that is passed to its view(products title).

In my templates, I try to create a link like the following:

<a href="{% url 'water_sensor_item' w_q_sensor.title %}" class="card hoverable mb-4 text-dark" >

one my products' title is 3725. when I click on that product in the main page, I get the following error:

Django Version: 3.1.2  
Exception Type: NoReverseMatch  
Exception Value:     Reverse for 'water_quality_sensor' not found. 'water_quality_sensor' is not a valid view function or pattern name.

What am I doing wrong?

1

There are 1 answers

4
ha-neul On BEST ANSWER

in your urls.py

path('wwqm', views.water_quality_sensor  , name='wwqm'),

you used name wwqm. But it looks like somewhere in your template (most likely water_sensor_item.html), you have something similar to :

<a href="{% url 'water_quality_sensor' %}"

Change it to wwqm or change name in urls.py

UPDATE It is better to use <str:title><int:pk> in your urls, to avoid when you have the same name in two products. pk is unique.

in your urls.py

path('wwqm/<str:water_sensor_title><int:pk>', views.water_sensor_item, name='water_sensor_item'),          # add <int:pk>

in your template:

# since url is taking both title and pk arguments, you need to provide both of them. 
<a href="{% url 'water_sensor_item' title= w_q_sensor.title pk=w_q_sensor.pk %}" class="card hoverable mb-4 text-dark" >

in your view:

def water_sensor_item(request, water_sensor_title, pk): # added pk

    water_sensors =  get_object_or_404(Water_quality_sensor, pk=pk) # use pk to get the object
    context = {
        'water_sensors': water_sensors
    }
    return render(request, 'products/water_sensor_item.html' , context)