Just wondering what the correct syntax is for checking if the current path is equal to some url:
{% if request.path == url "app_namespace:route_name" %}
Above doesn't work - but hoping someone knows a way or method for doing this lookup...
You can use this syntax to save the url path in a template variable:
{% url 'app_namespace:route_name' as url_path %}
which you can later use within your if condition
{% if request.path == url_path %}...{% endif %}
Note that you may also find this syntax useful when you need to use the output of a url function within a blocktrans block:
{% blocktrans %}
text to translate
{% endblocktrans %}
Related
for example, I can reverse a url in this way:
{% url 'home:index' %}
but if I need to compare a url in a if sentence, like this:
{% if request.path == url %}
and I want to replace the url with a reverse one
but I can't do this:
{% if request.path == {% url 'home:index' %} %}
So is there another way can solve this?
Thanks a lot !
The url tag takes an argument as <var> that saves the result of the reverse to a variable. You can then use this variable in your comparison
{% url 'home:index' as home_url %}
{% if request.path == home_url %}
I can't figure out how to get value of {% url "name" %} inside another {% %} tag. In my case its include
{% include "file" with next={% url "name" %} %}
This doesn't work. Do you know what to do? I would surround the {% include.. by {% with but it would cause the same problem.
Quick answer for django templates:
{% url "name" as my_url_context_var %}
{% include "file" with next=my_url_context_var %}
Lengthier explanation:
You cannot nest {% … %} or {{ … }}. The moment you open {{ … }}, you can only use context variables inside, optionally combined with template filters. Inside a {% … %}, you can only use whatever the tag you are using recognizes, but in general, that will be just a few arguments, some of which are static words (such as the as in the url above, or the with in the include), and often you can also access template variables and filters just like inside {{ … }}.
I'm not that familiar with jinja, but it seems that its include doesn't support the same kind of variable passing as django's include does, so here's an idea of what it might look like with jinja:
{% set next=url('name') %}
{% include "file" %}
So I have this in a Django template.
{% if request.get_full_path = "/blog/" %}
do something
{% endif %}
Which does exactly what I want when I go to http://somesite.com/blog/
What I want to do is also include all subdirectories.
So something like
{% if request.get_full_path = "/blog/*" %}
do something
{% endif %}
Unfortunately as far as I can tell Django doesn't do wildcards in templates. So how do I do this?
Try with this:
{% if request.get_full_path|slice:'0:6' == '/blog/' %}
I'm new to Django and puzzled. Using Django 1.4. Inside one of my templates, this code works:
{% for element0, element1 in menu.elements %}
<li class='menu_{{ name }}'>{{ element0 }}</li>
{% endfor %}
... but this code throws a "NoReverseMatch" error:
{% for element0, element1 in menu.elements %}
<li class='menu_{{ name }}'>{{ element0 }}</li>
{% endfor %}
... despite the fact that the "element1" variable holds 'users.views.home'. I'm thinking/hoping that the solution to this is really simple... that I've missed something obvious about variable handling inside Django templates?
I've consulted the documentation for the url built-in function to no avail. Any help will be appreciated.
I think you need to add this to your template:
{% load url from future %}
and change the first call to
{% url 'users.views.home' %}
see the forwards compatibility note in the docs you linked to
That's bad idea to write like this {% url 'users.views.home' %}, better use named url - {% url 'users_home' %}, it will be easy to maintain in the future. For example if you decide to move your def home(request) from users.views to account.views you will need to replace all urls occurrences in all your templates. But if you use named urls, you just need to change urls.py
I am not sure what is the difference between {% url url_name %} and {% url 'url_name' %}. Why is there such a difference. When to use what? Need some clarification on that... Thanks..
As of django 1.5 {% url url_name %} would treat the argument as a context variable, whereas {% url 'url_name' %} would give you the url named 'url_name'. In prior versions, the latter syntax is wrong, and won't work.
So if you are using django 1.4 and will migrate to newer versions later use this syntax {% url 'url_name' %} to get named url.
Note: you will have to add {% load url from future %} in each of your template where you will use above syntax.