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
Related
{% 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 %}
I am trying display different text to user on an empty tag. I was able to implement this correctly in other template, but I do not clearly understand why it is not displaying correctly in another template. For example, if user A login, I want only user A to see 'Find people to follow' , while other users to see 'No users'.
def following_view(request, username):
p = FriendRequest.objects.filter(from_user__username=username)
all_profile_user=[]
button_status_list=[]
for user_obj in p:
u = user_obj.to_user
all_profile_user.append(u)
friend = Profile.objects.filter(user=request.user, friends__id=user_obj.id).exists()
button_status = 'none'
if not friends:
button_status = 'not_friend'
if len(FriendRequest.objects.filter(from_user=request.user).filter(to_user=u)) == 1:
button_status = 'cancel_request_sent'
button_status_list.append(button_status)
context={'profile_and_button_status':zip(p, button_status_list), 'u':all_profile_user, 'following':p,}
{% for data in profile_and_button_status %}
#Button codes here
{% empty %}
#why this doesn't display
{% if data.user != request.user %}
No users
{% endif %}
{% if data.user == request.user %}
Find people to follow
{% endif %}
{% endfor %}
You can try like this:
{% for friend_request, status in profile_and_button_status %}
#Button codes here
#why this doesn't display
{% if friend_request.to_user != user %}
No users
{% else %}
Find people to follow
{% empty %}
No users
{% endfor %}
But I would suggest a better approach, which is to calculate these in views instead of template. Like this:
from django.db.models import Case, When, Value, CharField, F
def following_view(request, username):
friend_requests = FriendRequest.objects.filter(
from_user__username=username
).annotate(
button_status=Case(
When(to_user__in=request.user.profile.friends.all(), then=Value('none')),
When(
from_user=request.user, to_user=F('to_user'),
then=Value('cancel_request_sent')
),
default=Value('not_friend'),
output_field=CharField()
)
)
return render(request, 'template.html',context={'friend_requests':friend_requests})
# template
{% for friend_request in friend_requests %}
{{ friend_request.button_status }}
{% empty %}
No users
{% endfor %}
I am using conditional expression here.
Update(based on comments)
It should be simple. You should send data if request.user.username and username is same. For example:
# view
context={'checking_own_friend_list': request.user.username == username,... # rest of the contexts
# template
{% for data in profile_and_button_status %}
{% empty %}
{% if checking_own_friend_list %}
Find people to follow
{% else %}
No user
{% endfor %}
You are using a for loop instead of an if statement. It should be
{% for data in profile_and_button_status %}
# Button codes here
{% empty %}
# why this doesn't display
{% if data.user != request.user %}
No users
{% endif %}
{% if data.user == request.user %}
Find people to follow
{% endif%}
{% endfor %}
I am trying to apply if condition inside {% block content %} by using the following code:
{% for item in execution_log %}
{% if item.description == 'Set On' %}
#Condition
{% elseif item.description == 'Set Off' %}
#Condition
{% elseif item.description == 'FFMPEG error' %}
#Condition
{% endif %}
{% endfor %}
But I am not getting any output. Is the syntax for the if condition correct?
You should use elif instead of elseif
Hint: before the {% endif %}, add these lines and test again. This should drive you to the solution, since you didn't show the code that pushes the execution_log in the template context.
{% else %}
#UNKNOWN {{ item.description }}
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.
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.