Is there a better way to do the following? Perhaps something like if/elif/else ?
{% if avail.allows_free_streaming %}Free to Stream{% endif %}
{% if avail.requires_paid_subscription %}Subscription{% endif %}
{% if not avail.requires_paid_subscription and not avail.allow_free_streaming %}N/A{% endif %}
Yes, you can use the following approach:
{% if avail.allows_free_streaming %}
Free to Stream
{% elif avail.requires_paid_subscription %}
Subscription
{% else %}
N/A
{% endif %}
Related
The challenge is to reduce the construction and, if possible, speed up the processing of the code.
{% for tp1 in test.tab_tp1s %}
{{ tp1.name }}
{% endfor %}
{% for tp2 in test.tab_tp2s %}
{{ tp2.name }}
{% endfor %}
{% for tp3 in test.tab_tp3s %}
{{ tp3.name }}
{% endfor %}
{% for tab in test %}
{% for tp in test[tab]
{{ tp.name }}
{% endfor %}
{% endfor %}
Maybe, it's hard to tell because we don't know what is stored in test, I'm assuming test is a dictionary holding 3 keys which each hold a sub key called name?
So, I have a number of objects I wish to render in a loop. I.E. Render each of the 5 latest posts on the home page. Each of these posts will be displayed differently whether or not the user is logged in.
I have a question: How would I go about making this distinction? I imagine having a template like this
{% if user.is_logged_in %}
{% for post in latest_posts %}
post.render_long_form
{% endfor %}
{% else %}
{% for post in latest_posts %}
post.render_short_form
{% endfor %}
{% endif %}
How can I make the functions render_short_form and render_long_form return the appropriate HTML snippits? I would like them to call other templates for rendering under the hood.
Thanks!
Why don't not use {% include %} tag?
{% if user.is_logged_in %}
{% for post in latest_posts %}
{% include 'long_form.html' %}
{% endfor %}
{% else %}
{% for post in latest_posts %}
{% include 'short_form.html' %}
{% endfor %}
{% endif %}
Or, more DRY version:
{% for post in latest_posts %}
{% if user.is_logged_in %}
{% include 'long_form.html' %}
{% else %}
{% include 'short_form.html' %}
{% endif %}
{% endfor %}
I tried to make a state_template.html with
{% load inplace_edit %}
{% block extra_header %}
{% inplace_static %}
{% endblock %}
{% inplace_edit "action.action_state" %}
And tables.py has:
action_state = tables.TemplateColumn(template_name='django_tables2/state_template.html', verbose_name="State")
But since action is never being passed to the template, it is giving an error.
Does anyone have any ideas on how to make this work?
This is a very nice question :-)
I think if you update your template (state_template.html), with this should works:
{% load inplace_edit %}
{% block extra_header %}
{% inplace_static %}
{% endblock %}
{% inplace_edit "record.action_state" %}
And this is a recomendation, You should move to the template of the view that renderthe table, but this is only to efficiency:
{% block extra_header %}
{% inplace_static %}
{% endblock %}
I want to put break and continue in my code, but it doesn't work in Django template. How can I use continue and break using Django template for loop. Here is an example:
{% for i in i_range %}
{% for frequency in patient_meds.frequency %}
{% ifequal frequency i %}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}" checked/> {{ i }} AM</td>
{{ forloop.parentloop|continue }} ////// It doesn't work
{ continue } ////// It also doesn't work
{% endifequal %}
{% endfor%}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}"/> {{ i }} AM</td>
{% endfor %}
Django doesn't support it naturally.
You can implement forloop|continue and forloop|break with custom filters.
http://djangosnippets.org/snippets/2093/
For-loops in Django templates are different from plain Python for-loops, so continue and break will not work in them. See for yourself in the Django docs, there are no break or continue template tags. Given the overall position of Keep-It-Simple-Stupid in Django template syntax, you will probably have to find another way to accomplish what you need.
For most of cases there is no need for custom templatetags, it's easy:
continue:
{% for each in iterable %}
{% if conditions_for_continue %}
<!-- continue -->
{% else %}
... code ..
{% endif %}
{% endfor %}
break use the same idea, but with the wider scope:
{% set stop_loop="" %}
{% for each in iterable %}
{% if stop_loop %}{% else %}
... code ..
under some condition {% set stop_loop="true" %}
... code ..
{% endif %}
{% endfor %}
if you accept iterating more than needed.
If you want a continue/break after certain conditions, I use the following Simple Tag as follows with "Vanilla" Django 3.2.5:
#register.simple_tag
def define(val=None):
return val
Then you can use it as any variable in the template
{% define True as continue %}
{% for u in queryset %}
{% if continue %}
{% if u.status.description == 'Passed' %}
<td>Passed</td>
{% define False as continue %}
{% endif %}
{% endif %}
{% endfor %}
Extremely useful for any type of variable you want to re-use on template without using with statements.
I have the following code in my template:
{% for f in friendslist %}
{% if forloop.first %}
// display something
{% endif %}
// display stuff
{% if forloop.last %}
// display something
{% endif %}
{% endfor %}
It works as expected when there is more than one item in the friendslist. But if there is just 1 item, then the content inside the forloop.last
conditional does not display.
I guess this is because the loop in that case is the first, but I mean it's also the last right? So why dosn't both contents inside first and last
conditional show?
In my code they both execute if there is only one element in friendslist. Here is a test you can run in the shell where maybe you can figure out what isn't working:
$ ./manage.py shell
>>> from django import template
>>> t = template.Template("""{% for f in friendslist %}
{% if forloop.first %}
First of the loop
{% endif %}
{{ f }}
{% if forloop.last %}
Last of the loop
{% endif %}
{% endfor %}""")
>>> c = template.Context({'friendslist' : ['one element',]})
>>> t.render(c)
First of the loop
one element
Last of the loop
couldn't you just use an "or" tag like {% if forloop.last or friendlist.count == 1 %}
{% for f in friendslist %}
{% if forloop.first %}
// display something
{% endif %}
// display stuff
{% if forloop.last or friendlist.count == 1 %}
// display something
{% endif %}
{% endfor %}
You can also use ''not'' in template to solve this. It works even there is just an item in the list.
{% for friend in friendslist %}
{% if not forloop.last %}
// display something
{% endif %}
{% if forloop.last %}
// display something
{% endif %}
{% endfor %}