Switch Case in Django Template - python

I'm trying to put different template for different category based on category ID. I'm using Django 1.3. Switch case is not working with Django 1.3, I get this error:
Invalid block tag: 'switch', expected 'endblock' or 'endblock content'
but switch case had been correctly closed.
Here is my code:
{% switch property.category.id %}
{% case 0 %}
<h4>'agriculture'</h4>
{% case 1 %}
<h4>'Residential'</h4>
{% case 2 %}
<h4>'commiercial'</h4>
{% case 3 %}
<h4>'mixed use'</h4>
{% case 4 %}
<h4>'Industrial'</h4>
{% else %}
<h4>'retail'</h4>
{% endswitch %}
What is the error in this code?

There is no {% switch %} tag in Django template language. To solve your problem you can
either use this Django snippet, that adds the functionality,
or re-write your code to a series of {% if %}s.
The second option in code:
{% if property.category.id == 0 %}
<h4>'agriculture'</h4>
{% elif property.category.id == 1 %}
<h4>'Residential'</h4>
{% elif property.category.id == 2 %}
<h4>'commiercial'</h4>
{% elif property.category.id == 3 %}
<h4>'mixed use'</h4>
{% elif property.category.id == 4 %}
<h4>'Industrial'</h4>
{% else %}
<h4>'retail'</h4>
{% endif %}
As Alasdair correctly mentioned in his comment, the {% elif %} tag was introduced in Django 1.4. To use the above code in an older version you need to upgrade your Django version or you can use a modified version:
{% if property.category.id == 0 %}
<h4>'agriculture'</h4>
{% endif %}
{% if property.category.id == 1 %}
<h4>'Residential'</h4>
{% endif %}
{% if property.category.id == 2 %}
<h4>'commiercial'</h4>
{% endif %}
{% if property.category.id == 3 %}
<h4>'mixed use'</h4>
{% endif %}
{% if property.category.id == 4 %}
<h4>'Industrial'</h4>
{% endif %}
{% if property.category.id < 0 or property.category.id > 4 %}
<h4>'retail'</h4>
{% endif %}
This modification is safe** (but inefficient) here since the ID can't be equal to two different integers at the same time.
** as long as you only use integers for the IDs which is probable
However I would strongly recommend upgrading to a newer Django version. Not only because of the missing {% elif %} tag but mainly for security reasons.

Related

how to use Django template if condition inside Django Template?

{% if len(verifiedJobs) > 0 %}
{% for v in verifiedJobs %}
companyname:::{{v.companyname}}<br>
jobDescription::::{{v.jobDescription}}<br>
salary:::{{v.salary}}<br>
{% endfor %}
{% endif %}
this is my code and it is not working it is giving errors about my if condition.
Django templates don't understand the length function so that's why its giving you an error.
suppose the length of verifiedjob is 0 it will be considered as false and the if condition will not work and if its value is anything different than 0 it will work
{% if verifiedJobs %}
{% for v in verifiedJobs %}
companyname:::{{v.companyname}}<br>
jobDescription:::{{v.jobDescription}}<br>
salary:::{{v.salary}}<br>
{% endfor %}
{% endif %}

If else condition error in django template

I write a simple django condition but it not working
{% if request.user == "sami" %}
sami
{% else %}
khan
{% endif %}
Requset.user is an object.
You need to write
{% if request.user.username == "sami" %}
or
{% if request.user.name == "sami" %} whatever is in your model

Django Template For Loop - reverese last two iterations

I am using Django 1.10 and Python 3.5.
I have been given some code that consists of a for loop that returns 3 iterations of value in alphabetical order.
For example the for loop returns: Chronological, Combination, Functional.
However I need the for loop to return: Chronological, Functional, Combination (the last two iterations/values are reversed). I am forced to keep the for loop.
Is this at all possible with a for loop?
I have tried combining {% if forloop.last %} with {% if forloop.first%} and also setting the value with {% if forloop.counter == 2 %}...set value to Functional...{% endif %} and {% if forloop.counter == 3 %}...set value to Combination...{% endif %} but I cannot get this to achieve what I want.
Here is my code:
{% for resume_format_image in resume_format_images %}
<div class="col-md-4">
{% with "resumes/resume_format_description_"|add:resume_format_image.style.lower|add:".html" as template_name %}
{% include template_name %}
{% endwith %}
</div>
{% endfor %}
I just figured this out:
{% for resume_format_image in resume_format_images %}
<div class="col-md-4">
{% if forloop.first%}
{% include "resumes/resume_format_description_chronological.html" %}
{% elif forloop.last%}
{% include "resumes/resume_format_description_combination.html" %}
{% else %}
{% include "resumes/resume_format_description_functional.html" %}
{% endif %}
</div>
{% endfor %}
I hope this helps someone.

How to count authors in Pelican Blogs?

I was trying to make my blog show "Authors" instead of "Author" when there are more than 1 authors for the same article.
The solution (thanks to #Avaris) is to set i = article.authors|count and that can be used as follows
<div align="right">
{% set i = article.authors|count %}
{% if i != 1 %}
Authors:<br>
{% endif %}
{% if i == 1 %}
Author:<br>
{% endif %}
{% if article.authors %}
{% for author in article.authors %}
{{ author }}<br>
{% endfor %}
{% endif %}
</div>
This is different to Set variable in jinja because in Pelican I cannot find a command like lenght(authors) to obtain a number to define a condition.

Django if statement not working

{% extends "base.html" %}
{% block content %}
<h1>{{ page }}</h1>
{% for category in categories %}
{% if category.page == page %}
<h2>{{ category.title }}!</h2>
{% for item in categoryitems %}
{{ category.title }} {{ item.category }}
{% if item.category == category.title %}
<h3>{{ item.title }}</h3>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endblock %}
The first statement if category.page == page is working fine, but the other one - if item.category == category.title - doesn't, for the sake of checking if everything is fine I've also added those {{category.title}} and {{item.category}} outputs and they are identical, so why doesn't the loop show me my item.title ? Without the if, it works, but, of course, shows every element in the list, which is smthn I don't need.
Do not depend on the page output to tell you what is correct, since the output is dependent on the code. Instead, do it correctly in the first place.
{% if item.category == category %}

Categories

Resources