I'm using jinja2 to render string. I have a filter named 'get_user_by_id' and other similar filters. I want to rename the filter expression value with a simple variable name. The follow is what I want:
{% rename 1|get_user_by_id, 'user'%}
{{user.name}} {{user.email}} ...
{% rename 5|get_book_by_id, 'book'%}
{{book.name}} {{book.price}} ...
How can I write the "rename" tag extension with jinja2?
As I understand is that you try to set a new variable in jinja...
You can do it with set command.
For example:
{% set book = get_user_by_id %}
Related
I want to create a list and append values to it throughout the template. Before having to create a custom filter or something like that I'd like to know how this is done with vanilla Jinja.
This is the only way I could get this to work:
{%- set mylist = [] -%}
{# create manipulate variables... #}
{% set mylist = mylist.append(some_var) %}
{# create manipulate variables... #}
{% set mylist = mylist.append(some_other_var) %}
Do I really need to use set and reassign like this? I'd prefer to just to {% mylist.append(some_other_var) %}, but it looks like that isn't possible?
Also, how can I do multiple appends more concisely (if I don't have another list to loop over). I'd like to do something like this (which does not work):
{%
mylist.append(some_var)
mylist.append(some_other_var)
mylist.append(sdfsdfs)
%}
I'm completely newbie in Django-phython.
I'm trying to populate the results of a search inside an html form input tag. with the register that the search returns.
I have a view that renders an html code, passing a dictionary. And I would like to display that dictionary, containing the search results, inside the html form fields that corresponds to that dictionary.
The question is if it's possible to do such a thing.
You will get all the information at django template documentation
Specifically, if you want to iterate through the dictionary in your template, you need the for loop.
From the documentation:
This can also be useful if you need to access the items in a
dictionary. For example, if your context contained a dictionary data,
the following would display the keys and values of the dictionary:
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
You'll have to use ajax and a typehead pulgin for that.
In a django template, is it possible to have two (or more) dots after a variable? For example, say I have a list of objects that I first want to use a list-index lookup for and then once I have the object, I want to call its method for getting the absolute url, should that work?
For example:
{% for entry in myList %}
{{ entry.0.get_absolute_url }}
{% endfor %}
So the 0 is asking for the first item in the list which is an object, then I want to get the absolute url. It doesn't work when I try it but it doesn't return an error either. Is there a better way to accomplish what I'm trying to do?
To clarify it, what's strange is that:
This works:
{{ singleObject.get_absolute_url }}
In that case if I just try {{ singleObject }}, I get the unicode value of that object so something like: John Smith
This doesn't work:
{% for object in objectList %}
{{ object.get_absolute_url }}
{% endfor %}
But in this case, if I put in {{ object }}, I no longer get the unicode value. I get: [<Name: John Smith>] (name being the name of the model)
Basically, the method works when it's outside of a loop. Could there be any reason for that?
more than one dot absolutely works.
based on your comment, there is no entry.0 because entry IS the first item in the list cause you are already looping through `myList'
just use entry.get_absolute_url instead
but if you only want to print out the url for the first entry, forgo the for loop and just use myList.0.get_absolute_url
UPDATE:
there's a tip from 'the pragmatic programmer' that says:
``select’’ Isn’t Broken
It is rare to find a bug in the OS or the
compiler, or even a third-party product or library. The bug is most
likely in the application.
i think you assumed that django templates were behaving weird, when the truth is you were not building your list correctly. don't be afraid to show some of your actual code, by abstracting the problem for us, you removed the part that included the problem
I got it. I had brackets around each item in my list like so:
objectList = [['John Smith'], ['Jim Jones'], ['Bill White']]
Silly me! Thanks so much for your all your input
What you are doing is perfectly acceptable in Django templates. There is no better way to accomplish what you're trying to do.
Hi all not sure how to explain this clearly but here goes....
I need to use two variables like so:
{% for client in clients %}
{% if user.client.username %}
I need {% if user.username %} but the value of username is in client.username from the loop.
is there a way to do this?
If I understand correctly, user is a dict, and you want to lookup the value indexed by client in each iteration of the loop - eg, user[client].username in Python.
This (deliberately) isn't possible in Django templates - the language is limited, to force you to do pre-processing in code.
Instead, you should zip your two lists/dicts together before passing them to the template.
Are you trying to do something if the value of client.username is equal to the value of user.client.username? If so, you want:
{% if client.username == user.client.username %} # Works in Django 1.2 and above
{% ifequal client.username user.client.username %} # Works everywhere
Is there an easy way to use python string formatting from within a django template? That is, I'd like to be able to do something like this in a template
{{ variable|%.3f }}
I know in this case, one can just use
{{ variable|floatformat:3 }}
But I'd really like to be able to generically use any python string format on a django variable. In my system it's inconvenient to have to deal with two different ways to format output (python vs django), so I'd like to standardize. I could write a custom template tag like
{% pyformat variable format="%.3f" %}
or maybe a custom template filter like
{{ variable|pyformat:"%.3f" }}
Do either of these already exist? Will the customer filter work with a string passed in like that?
{{ variable|stringformat:".3f" }}
Source: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#stringformat
stringformat
I had omit the "%":
{{ variable|stringformat:".3f" }}