I need to compare an url with if condition in Django html template, to make an options menu selected when I'm on this page. I have this but doesn't works well:
{% for vehiculo in vehiculos %}
{% if "/calendar/vehicle/{{ vehiculo.pk }}/" in request.path %}
<a >
{% else %}
<a >
{% endif %}
{% endfor %}
But doesn't works!!!
I don't find any solutions in Django docs or here in stackoverflow, github...
Any solution is worth, thanks!
CodePudding user response:
Rather than checking the URL to see if it contains the pk, if you are on a detail page for car, and on the car detail page you have a list of link to navigate to other car detail pages, you could quite easily add the vehicle object (or just the pk) to the context which is rendered with the template. Then it would be a matter of iterating your list of vehicles, and comparing the pk of the two objects, setting active class on the relevant link.
Added bonus, I would usually write this sort of if statement as a one-line like so:
{% for vehiculo in vehiculos %}
<a >
{% endfor %}

