How to go to a Django url regardless of the path we are on?

Asked

Viewed 442 times

0

I created a menu in a base file that is visible regardless of the page we are,

        <p> Tela de perfil <a href="perfil/">Perfil</a></p>

The problem is that when we are on the same page and click on the same link, the "Profile" and duplicated giving an error,

How can I put to go to page that is independent whether or not I am already in it, or in a different way ?

2 answers

1


What Puam Dias answered is correct. Just remember that to use this tag you need to name the route in the urls.py file

urlpatterns = [
    url(r'^perfil/$', perfil_function, name='perfil'),
]

It is the attribute name='profile' that enables you to do so in the template:

<a href="{% url 'perfil' %}">Perfil</a>

0

It is not recommended that you use urls this way, because this can lead to silent errors in your application. If one day you end up changing your url to 'profile-something', when accessing this template, it will continue to be rendered normally without accusing the error. Only when you click the link, will the error be displayed.

Try calling urls with Django tags:

<p>Tela de perfil <a href="{% url 'perfil' %}">Perfil</a></p>

or

<p>Tela de perfil <a href="{% url 'namespace:perfil' %}">Perfil</a></p>

if your url is inside an app with namespace.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.