Url repeating each time causing no template found error

Viewed 19

everything works fine on first click url is repeated again causing no template errorstrong text

on second click only id should be passed like first screenshot instead of repeating the route again

 <div class="dropdown">
                        <a class="btn btn-secondary dropdown-toggle"  role="button" data-bs-toggle="dropdown" aria-expanded="false">
                    category
                        </a>
            
                        <ul class="dropdown-menu">
                            {% for j in catism %}
                                <li><a class="dropdown-item" href="viewalls/{{j.id}}">{{j.cat_name}}</a></li>
                            {% endfor %}
                        </ul>
                    </div>

urls.py

from django.urls import path,include from .import views

urlpatterns=[ path('',views.home,name='home'),

path('viewalls/<int:id>',views.view_all,name='views'),
path('viewall',views.view_all,name='view_all'),


path('addproduct',views.add_product,name='add_product'),
path('remove_product',views.remove_product,name='remove_product'),
path('remove/<int:id>',views.remove_product,name='remove_product'),
# path('filter_product',views.filter_product)

# path('login/',include('django.contrib.auth.urls'))

]

views.py

`def view_all(request,id=0): pros=product.objects.all() cats=category.objects.all() if id: cat=category.objects.get(id=id) pros=pros.filter(cat_id__cat_name=cat) return render(request,'prohome.html',{'context':pros,'catism':cats})

return render(request,'prohome.html',{'context':pros,'catism':cats})`
1 Answers

This part of the code:

href="viewalls/{{j.id}}"

Should be changed in:

href="/viewalls/{{j.id}}"

Because with a /, you are redirecting relative to the root.

But without a /, you are redirecting relative to the current endpoint.

Related