Next field django forms [duplicate] - python

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.

Related

How do i specify a variable in side of django template file

So I am trying to declare a variable inside of my django templates file
{% with object = "{{object.id
}}" %}
{% for detail in details %}
{% if detail.post.id == {{object}} %}
{{detail.name}}
{% endif %}
{% endfor %}
{% endwith %}
I know that with is used to this job, but when i run this code it shows me this error: 'with' expected at least one variable assignment
Please help me. Thank You
The post_id is likely an int so you should specify object as {% with object=2 %}, you furthermore should not use double curly brackets in a template tag:
{% with object=2 %}
{% for detail in details %}
{% if detail.post_id == object %}
{{ detail.name }}
{% endif %}
{% endfor %}
{% endwith %}
It is however not a good idea to filter in the template, since this is not efficient, and requires more memory, normally you filter in the view.

How to pass template variable to slice filter in Django templates

I'm trying to slice loop in django template with variable
USUAL WAY
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":2" %}
{{ article.article_title }}
{% endfor %}
WHAT NEEDS
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":module.module_article_key.module_article_count" %}
{{ article.article_title }}
{% endfor %}
so we have working variable {{ module.module_article_key.module_article_count }}
normaly this variable gives integer value stored for this module, however wen i use it to slice loop - nothing happens
You need to cast module_article_count to string first then making articleSlice via nested {% with %} and use the resulting template variable in slice filter as follow:
{% with articleCount=module.module_article_key.module_article_count|stringformat:"s" %}
{% with articleSlice=":"|add:articleCount %}
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:articleSlice %}
{{ article.article_title }}
{% endfor %}
{% endwith %}
{% endwith %}

How to check whether a number is divisible by another in Jinja Template (Django Framework)

I am trying to check a simple condition in Jinja Template inside for loop in a web page whether a number is divisible by three or not.
I have referred the following link
http://jinja.pocoo.org/docs/dev/templates/
(Note loop.index doesnt work for me forloop.counter does)
The code is
{% extends "header.html" %}
{% block content %}
<h1>List of all Reference Ids</h1>
<table class="table table-striped">
{% for master in object_list %}
{% if forloop.counter divisibleby 3 %}
Do something
{%endif%}
<td> {{ master.reference_id }} </td>
{% endfor %}
</table>
{% endblock %}
Tried various combinations such as below
{% if forloop.counter divisibleby 3 %}
{%endif%}
{% if forloop.counter divisibleby(3 %}
{%endif%}
{% if divisibleby(forloop.counter,3) %}
{%endif%}
{% if divisibleby forloop.counter 3 %}
{%endif%}
{% if forloop.counter%3==0 %}
{%endif%}
But nothing works. I dont know where I am making the mistake. Please help me out guys I am stuck into this problem for quite long.
template_string = """
{% for i in [1,2,3,4,5,6,7] %}
{% if loop.index %3 == 0%}3{%else%}0{%endif%}\n
{% endfor %}
"""
from jinja2 import Template
print Template(template_string).render()
although it sounds like you are using django Template not jinja ...
if this is DjangoTemplateLanguage then
{% if forloop.counter0|divisibleby:3 %}
should work (I think ... ) so in full here is the djangoTemplate equivelent that is runnable standalone
from django.template import Template, Context
from django.template.engine import Engine
from django.conf import settings
settings.configure(DEBUG=False)
template_string = """
{% for i in the_list %}
{% if forloop.counter|divisibleby:3 %}3{%else%}0{%endif%}\n
{% endfor %}
"""
print Template(template_string).render(Context({"the_list":[1,2,3,4,5,6,7]}))

Django assignment_tag conditional

I'm trying to show partials based on a simple condition. My condition is whether an assignment_tag is True or False.
Templatetag:
from django import template
register = template.Library()
#register.assignment_tag
def partner():
return False
Template:
{% load partner_check %}
{% if partner %}
{% block header %}
{% include 'includes/partner_header.djhtml' %}
{% endblock header %}
{% block footer %}
{% include 'includes/partner_footer.djhtml' %}
{% endblock footer %}
{% endif %}
No matter what I set partner to, the blocks still appear. What am I missing?
Firstly, that's not how assignment tags work. You have never actually called the tag; if partner refers to a (non-existent) template variable named "partner". You call an assignment tag by using it on its own along with a variable to assign it to:
{% partner as partner_value %}
{% if partner_value %}...{% endif %}
Secondly, that's not how blocks work either. You can't dynamically define blocks; they are part of the basic structure of a template, not something that is assigned during evaluation.
I accomplished this by using a context_processor (https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS)
Context Processor:
def partners(context):
return {
'partner': False
}
Template:
{% block header %}
{% if partner %}
{% include 'includes/partner_header.djhtml' %}
{% else %}
{{ block.super }}
{% endif %}
{% endblock header %}
{% block footer %}
{% if partner %}
{% include 'includes/partner_footer.djhtml' %}
{% else %}
{{ block.super }}
{% endif %}
{% endblock footer %}

Django template comparing string

I'm new with django. I'm stuck with the problem of comparing string in the template.
I have use ifnotequal tag to compare string. But it is not working.
I have try to output the variable:
{{ request.user.username }}
{{ article.creator }}
Here I compare:
{% ifnotequal request.user.username article.creator %}
{# output something #}
{% endifnotequal %}
But when I do the hardcode: It works.
{% ifnotequal "justin" "mckoy" %}
{# output something #}
{% endifnotequal %}
what is the problem? The article.creator is coming from the database and the user.username is from the request.
Can anyone help me with this issue?
For string compare in template use
{% if name == "someone" %}
............
............
{% endif %}
and for not equal
{% if name != "someone" %}
............
............
{% endif %}
Try this:
{% ifnotequal article.creator|stringformat:"s" request.user.username %}
article.creator is a User and request.user.username is a string. Try comparing request.user instead.
{% ifequal material.unit 'U' %}
<p>are equals!<p/>
{% endifequal %}
Note that if you do not put spaces before and after ==, Django could not parse the expression.
{% if MyProd.Status == "Processing" %}
<button class="btn btn-outline-warning">{{MyProd.Status}}</button>
{% else %}
<button class="btn btn-outline-success">{{MyProd.Status}}</button>
{% endif %}

Categories