How do i pass a template variable to djangos url resolver as a parameter?
Something along these lines:
{% for group in groups %}
{{ group.name }}
MEMBER
<br>
{% endfor %}
What makes you think you need variable tags inside another tag? You don't: as the documentation clearly shows, once you're inside a tag, you have direct access to the context variables.
{% url 'group-view' group_code=group.group_code %}
Related
I need to hard set a list in django template.
I know that I have to pass variables to the template, instead of creating them in the template, but I only have access to the template file. I'm using sendinblue with a custom template, and the only way to use custom params injected to the template is to use their api. I only need to hardcode some content in a list, and the content will dynamically appear depending on contact, I think that using an api only for this is overkill.
{% with tt='123'|make_list %}
{{ tt }}
<hr>
{% for x in tt %}
{{ x }}<br>
{% endfor %}
{% endwith %}
try this
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" %}
Is it possible for the django template {% load %} tag to take a variable. I'm trying to pass the name of the tag set from my view to the template html and using it like this: {% load {{filter}} %} where filter is the name of the variable with the tag set name string. However, doing so is giving me the error: {{' is not a registered tag library. Must be one of:
I want to load some tag sets dynamically since this template file is part of a reusable app and the tag set will depend on which application is using this app. Thanks in advance!
In your template
{% if condition_from_view_1 %}
{% upload tag_library1 %}
{% elif condition_from_view_2 %}
{% upload tag_library2 %}
{% endif %}
Similarly, you can take the check down to the tag level.
I was using the count method on a queryset context variable more than once in a template, so I decided to store it in a reusable variable:
{% with album.photograph_set.count as numPhotos %}
<title>My title with {{ numPhotos }} in it</title>
<span>I use {{ numPhotos }} here, too</span>
{% endwith %}
The numPhotos variable always seems to be blank, though replacing it with album.photograph_set.count inline still returns the appropriate value. I also tried using the {% with numPhotos=album.photograph_set.count %} syntax but it exhibits the same behavior. I use the {% with ... as ... %} syntax elsewhere in my code and it works as expected.
Any help is appreciated.
If photograph_set is the reverse relationship of a ForeignKeyField or if it's a ManyToManyField, you'll need to do
{% with album.photograph_set.all.count as numPhotos %}
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