user12425537
user12425537

Reputation:

How to change href in django based on the current page url?

As I said in the title, what would be the correct syntax to change the href of a link according to the current url the user is on?

I tried this, but it doesn't work.

{% if url == '' %} href='/cookies' {% else %} href='' {% endif %}

What would be the correct syntax to do this?

Upvotes: 0

Views: 332

Answers (2)

Cr4id3r
Cr4id3r

Reputation: 161

At Django 1.9 and above can use something like this

href='{% if not request.path %}/cookies{% endif %}'

Upvotes: 1

Erich
Erich

Reputation: 1848

You can check the current url with

href="{% if request.path == '' %}/cookies{% endif %}"

Upvotes: 0

Related Questions