Omar Shokeir
Omar Shokeir

Reputation: 11

Django - Going back to a directory at a upper level from the template with href in django

trying to keep the question simple!

I am currently in the directory which has a template cart.html:

8000/shop/cart/6/M/user

and I want to go to the directory:

8000/shop/shirts

So I just wrote in the cart.html:

<a href="{% url 'shop' 'shirts' %}">

Which gives me an error:

Reverse for 'shop' not found. 'shop' is not a valid view function or pattern name.

So, I believe that its because I am trying to go from a lower directory back into a higher directory in the hierarchy so is there a way I can edit the href in cart.html in order to go to the shirts directory?

Thanks in advance,

Upvotes: 1

Views: 1167

Answers (2)

Paolo Vezzola
Paolo Vezzola

Reputation: 125

When you define the urls. you can chose a name to use whenever into a django template, to reffer to this url

path('shirts/',views.shirts,name='shirts'),

now, inside a template

<a href="{% url 'shirts' %}">Shirts</a>

The name after the url within the '' is the NAME you define into your urls file

Upvotes: 2

Ali Aref
Ali Aref

Reputation: 2412

you can go to parent directory by .. so <a href='../../../shirts'> would be what you want, or without changing the directory <a href='/shop/shirts'>.

but it's better to use {% url 'url-name' %}

Upvotes: 0

Related Questions