Search code examples
htmldjangoanchor

django-URL inside anchor tag becomes relative to the current page django


urls.py

    path('add_a_product_to_store/<store_id>',views.add_a_product_to_store,name='add_a_product_to_store'),
    path('show_a_store/<store_id>', views.show_a_store,name='show_a_store')

show_a_store.html

<a href="add_a_product_to_store/5">Add a product</a>

When user enters ip:port/show_a_store/5 . . . . show_a_store.html is shown. But the link inside anchor tag points to http://127.0.0.1:8000/show_a_store/add_a_product_to_store/5 instead of http://127.0.0.1:8000/add_a_product_to_store/5

How to make it point to actual url irrespective of current page?


Solution

  • add slash as shown below

    path('add_a_product_to_store/<store_id>/',views.add_a_product_to_store,name='add_a_product_to_store'),
    path('show_a_store/<store_id>/', views.show_a_store,name='show_a_store')
    

    and template

    <a href="{% url 'add_a_product_to_store' store_id=object.id %}">Add a product</a>