sailesh
sailesh

Reputation: 1

Got an error in Django view : "NoReverseMatch at /view/"

Here are my urls :

root urls.py

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

]

url.py

    urlpatterns = [
    path('',views.store,name='store'),
    path('cart/',views.cart,name='cart'),
    path('checkout/',views.checkout,name='checkout'),
    path('update_item/',views.updateItem,name='update-item'),
    path('process_order/',views.processOrder,name='process-order'),
    path('view/<int:pk>/',views.view,name='view-detail'),
]

Here is the code for the view :

views.py

    def view(request,pk):
        product = Product.objects.get(id=pk)
        return render(request,'store/view.html',{'product':product} )

Here is my template : template

    <a class="btn btn-outline-success" href="{% url 'view-detail' product.id%}">View</a>

This output error :

NoReverseMatch at /view/1/ Reverse for 'store' with arguments '(1,)' not found. 1 pattern(s) tried: ['$']

How to resolve this error?

Upvotes: 0

Views: 105

Answers (1)

Riyas Ac
Riyas Ac

Reputation: 1613

template

{% load templatehelpers %}
<a class="btn btn-outline-success" href="{% abs_url 'app_name:view-detail' request pk=product.id %}">View</a>

Here we use a new simple tag namely abs_url (absolute url) in templatehelpers.py (Template tag). And app_name is the name of the app.

templatehelpers.py

from django import template
from django.urls import reverse
register = template.Library()

@register.simple_tag
def abs_url(value, request, **kwargs):
    return reverse(value,kwargs=kwargs)

Upvotes: 1

Related Questions