I need some help.
I want to list all user avoid the login user.
if I print user= juan
if I print usuario.username=juan
There have the same string
{% if usuario.username == user %}
Nothing
{% else %}
<tr><td>
{{usuario.username}}
<img src="{% static 'assets/img/icono_writeMSJ.png' %}" height="30px">
</td>
</tr>
{% endif %}
if i change for example
{% if usuario.username == 'juan' %}
Nothing
{% else %}
<tr><td>
{{usuario.username}}
<img src="{% static 'assets/img/icono_writeMSJ.png' %}" height="30px">
</td>
</tr>
{% endif %}
this work fine, why?? :(
user is an object. Stringify it. That should work
{% if usuario.username == user|stringformat:"s" %}
======== excluded current logged-in user ========
========= in HTML ==============
<h3>All Users list</h3>
{% if user.is_authenticated %}
<ul>
{% for i in all_users %}
<li>{{i.username}}</li>
{% endfor %}
</ul>
{% else %}
<h4>Please login first</h4>
======= in views.py ===========
def HomeView(request):
all_users = User.objects.all().exclude(id=request.user.id).values('username')
return render(request,'index.html',{'all_users':all_users})
Finally, the problem was that user is an objet. For that reason a can't compare from that way.
{% if usuario.username == user %}
the correct way is
{% if usuario.username == user.username %}
Related
Trying to use nested block and for loop in a Jinja template block setup.
{% block main %}
<table>
<tr>
<td>user id</td>
<td>user sec level</td>
</tr>
{% block main_nested_b scoped %}
{%
for user in list_users:
t_id_user = str(user[0][0])
t_sec_level = str(user[2][0])
%}
<tr>
<td>
<a href='/usersEdit?id_user={{ t_id_user }}' class='onwhite'>edit</a>
</td>
</tr>
{% endfor %}
{% endblock main_nested_b %}
{% endblock main %}
</table>
Error message:
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 't_id_user'
Help?
You can't treat Jinja syntax as Python syntax. It's not the same thing. Keep your for tag separate from assignment (set) tags:
{% for user in list_users %}
{% set t_id_user = user[0][0] %}
{% set t_sec_level = user[2][0] %}
Note that there isn't even a : at the end of the for ... in ... syntax! Also you don't need to call str() here, leave that to Jinja to convert to strings for you; anywhere you use {{ t_id_user }} or {{ t_sec_level }} the value will be converted to a string anyway.
Here is the complete template:
<table>
{% block main %}
{% block main_nested_b scoped %}
{% for user in list_users %}
{% set t_id_user = user[0][0] %}
{% set t_sec_level = user[2][0] %}
<tr>
<td>
<a href='/usersEdit?id_user={{ t_id_user }}' class='onwhite'>edit</a>
</td>
</tr>
{% endfor %}
{% endblock main_nested_b %}
{% endblock main %}
</table>
It seems logic that any author of an article, can delete his own post. But I don't really know how to check if a user is the current author of a post.
Here is my code :
{% extends 'base.html' %}
{% block title %} Details | {{article.title}} {% endblock title %}
{% block content %}
<div class="starter-template" style="text-align: center; margin: 2% 0">
<h1>{{object.title.capitalize}}</h1>
<p>{{object.body}}</p>
<p style="font-style: italic">{{object.author}}</p>
<p>{{object.date}}</p>
{% if object.author == user.username %}
<p>
Delete
Edit
</p>
{% endif %}
</div>
{% endblock content %}
"object.author == user.username " is returning False. Why is that ?
Thanks :)
You can check like below. Please add your models to view relations.
{% object.author.user == user %}
{% if object.author == user %}
I'm passing 2 variables (3 including a simple one) to some Jinja from a python script.
On certain runs I want to generate a list with links.
<html>
<body>
<h1>{{ title }}</h1>
<table>
{% if packageURLs is defined %}
{% for x in packageURLs: %}
<a href="{{ x }}">
{% endfor %}
{% for i in packagesList: %}
<tr><td>{{ i }}</td></tr>
{% endfor %}
{" else "}
{% for i in packagesList: %}
<tr><td>{{ i }}</td></tr>
{% endfor %}
{% endif %}
</a>
</table>
</body>
for each 'packageURLs' I want to add a tag then some table tags for 'packages' List. What's happening though is its generating HTML but completing each individual for loop first. So I end up with half a page full of links then another with table rows. How can I have it iterate through both initially?
I found a solution:
<html>
<body>
<h1>{{ title }}</h1>
<table>
{% if packageURLs is defined %}
{% for i,x in packagesList %}
<a href="{{ x }}">
<tr><td>{{ i }}</td></tr>
</a>
{% endfor %}
{" else "}
{% for i in packagesList: %}
<tr><td>{{ i }}</td></tr>
{% endfor %}
{% endif %}
</table>
</body>
Which is populated using zip() in the python script:
f = t.render(title=dataType,packagesList=zip(Info,packageURLs),packageURLs=packageURLs)
I kept the last variable out of laziness as a check since I still want to only execute those different chunks sometimes.
i'm creating menu with forloop and i need to add active class after click.
{% for menu in TopMenu %}
<li>{{menu.title}}</li>
{% endfor %}
i tried to use django template inheritance but it didn't work. any solutions?
{% for menu in TopMenu %}
<li {%if activeflag == '{{menu.slug_link}}' %} class="active" {%endif%} >{{menu.title}}</li>
{% endfor %}
You don't need {{ }} when using the if tag.
Try:
{% if activeflag == menu.slug_link %} class="active" {% endif %}
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.