8 examples of 'if else in django template' in Python

Every line of 'if else in django template' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
34def test_else_elsif():
35 tpl = """
36 {% if customer.name == "kevin" %}
37 Hey Kevin!
38 {% elsif customer.name == "anonymous" %}
39 Hey Anonymous!
40 {% else %}
41 Hi Stranger!
42 {% endif %}
43 """
44 assert Liquid(tpl).render(
45 customer={"name": "anonymous"}
46 ).strip() == "Hey Anonymous!"
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

263def testElseInSet(self):
264 tpl, ev = evaluate('[set:range [test:foo evals true][else evals false]]')
265 self.assertEquals(ev, '')
572def test_if4(self):
573 'if-elif-else statements and nested statements'
574 self.assertEqual(mint.Template('#if False:\n'
575 ' if\n'
576 '#elif True:\n'
577 ' elif\n'
578 ' #if False:\n'
579 ' nested if\n'
580 ' #else:\n'
581 ' nested else\n'
582 '#else:\n'
583 ' else').render(), 'elif\nnested else\n')
23def test_default_template_tag(self):
24 tmpl = '{% load renderit_tags %}'\
25 '{% renderit obj %}'
26 self.assertEqual(
27 render(tmpl, {}), '{ RENDERIT DEFAULT TEMPLATE }')
100def test_when_if_statement_resolves_to_false_the_content_is_skipped(self):
101 template = quik.Template("Hello #if (@show_greeting)your name is @{name}#end Good to see you")
102 self.assertEqual("Hello Good to see you", template.merge({"name": "Steve", "show_greeting": False}))
16@register.tag
17def ifswitch(parser, token):
18 bits = token.split_contents()
19 if len(bits) < 2:
20 raise template.TemplateSyntaxError("%r tag requires an argument" % token.contents.split()[0])
21
22 name = bits[1]
23 instances = bits[2:]
24
25 nodelist_true = parser.parse(('else', 'endifswitch'))
26 token = parser.next_token()
27
28 if token.contents == 'else':
29 nodelist_false = parser.parse(('endifswitch',))
30 parser.delete_first_token()
31 else:
32 nodelist_false = template.NodeList()
33
34 return SwitchNode(nodelist_true, nodelist_false, name, instances)
311@setup({'if-tag-not08': '{% if foo and not bar %}yes{% else %}no{% endif %}'})
312def test_if_tag_not08(self):
313 output = self.engine.render_to_string('if-tag-not08', {'foo': True, 'bar': False})
314 self.assertEqual(output, 'yes')
12def render_in_page(request, template):
13 """return rendered template in standalone mode or ``False``
14 """
15 from leonardo.module.web.models import Page
16
17 page = request.leonardo_page if hasattr(
18 request, 'leonardo_page') else Page.objects.filter(parent=None).first()
19
20 if page:
21 try:
22 slug = request.path_info.split("/")[-2:-1][0]
23 except KeyError:
24 slug = None
25
26 try:
27 body = render_to_string(template, RequestContext(request, {
28 'request_path': request.path,
29 'feincms_page': page,
30 'slug': slug,
31 'standalone': True}))
32 response = http.HttpResponseNotFound(
33 body, content_type=CONTENT_TYPE)
34 except TemplateDoesNotExist:
35 response = False
36
37 return response
38
39 return False

Related snippets