say we have table in template
<table class="table">
<thead>
<tr>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
{% if {{student.academic_status}}=="promoted" %}
<td class=text-success>promoted</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
So is it possible using if statement in table in django-templates
Since you are already in a template tag, you should not use curly brackets ({{ … }}) for the variable, so:
{% if student.academic_status == "promoted" %}
…
{% endif %}
The correct way to add if statement in Django Templates
follow me !
if statement
{% if condition %}
{{here you add the key that you using in the views page exactly context}}
{% endif %}
Related
I need to put a counter in while loop in template. So I did:
<tbody>
{% with count=1 %}
{% while count <={{orders_count}}: %}
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{count}}</td>
<td>{{order.name}}</td>
</tr>
{% count+=1 %}
{% endfor %}
{% endwhile %}
{% endwith %}
</tbody>
But finaly I have the following error:
Invalid block tag on line 216: 'while', expected 'endwith'. Did you forget to register or load this tag?
You do not need a while loop here, you can simply make use of the |slice template filter [Django-doc]:
<tbody>
{% for order in orders|slice:orders_count %}
<tr>
<td style="width:5%;"></td>
<td>{{ forloop.counter }}</td>
<td>{{ order.name }}</td>
</tr>
{% endfor %}
</tbody>
But slicing, etc. does not really belong in the template. You normally do that in the view.
You can try this :
<tbody>
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{forloop.counter}}</td>
<td>{{order.name}}</td>
</tr>
{% endfor %}
</tbody>
I just started learning Python and Django.
Trying to output on HTML data from the database, I can do it if I set a id, but I wanna have a page where I list all the data from the database in a table.
This is my views:
from django.shortcuts import render
from django.http import HttpResponse
from .models import clientes, viagem
# Create your views here.
def index(request):
ls= clientes.objects.all()
context= {'ls': ls}
return render(request, "booking/home.html", context)
And this is my page:
{% extends 'bulma/base.html' %}
{% block title %}Travel{% endblock %}
{% for clientes in clientes %}
{% block content %}
<table class="table is-fullwidth is-hoverable">
<thead>
<tr>
<th><abbr title="ID">ID</abbr></th>
<th>Nome</th>
<th>Apelido</th>
<th>Morada</th>
<th>Telemóvel</th>
<th>NIF</th>
</tr>
</thead>
<tbody>
<tr>
<th>{{ls.id}}</th>
<td>{{ls.nome}}</td>
<td>{{ls.apelido}}</td>
<td>{{ls.morada}}</td>
<td>{{ls.tel}}</td>
<td>{{ls.nif}}</td>
</tr>
</tbody>
</table>
{% endblock content %}
{% endfor %}
Can someone point me in the right direction?
First thing: your {% for %} loop is not at the right place. When extending another template, only the code in {% block %} statements are executed. So you want to move your loop inside the {% block content %}.
Second point: in the context you pass to the template, your queryset is named ls, not clientes, so you want to change this name either in the view or the template - the point is that they have to match xD.
A possible correction:
def index(request):
queryset = clientes.objects.all()
context= {'clientes': queryset}
return render(request, "booking/home.html", context)
and
{% extends 'bulma/base.html' %}
{% block title %}Travel{% endblock %}
{% block content %}
{% for client in clientes %}
<table class="table is-fullwidth is-hoverable">
<thead>
<tr>
<th><abbr title="ID">ID</abbr></th>
<th>Nome</th>
<th>Apelido</th>
<th>Morada</th>
<th>Telemóvel</th>
<th>NIF</th>
</tr>
</thead>
<tbody>
<tr>
<th>{{client.id}}</th>
<td>{{client.nome}}</td>
<td>{{client.apelido}}</td>
<td>{{client.morada}}</td>
<td>{{client.tel}}</td>
<td>{{client.nif}}</td>
</tr>
</tbody>
</table>
{% endfor %}
{% endblock content %}
you can try this:
{% extends 'bulma/base.html' %}
{% block title %}Travel{% endblock %}
{% block content %}
<table class="table is-fullwidth is-hoverable">
<thead>
<tr>
<th><abbr title="ID">ID</abbr></th>
<th>Nome</th>
<th>Apelido</th>
<th>Morada</th>
<th>Telemóvel</th>
<th>NIF</th>
</tr>
</thead>
<tbody>
{% for client in ls %} # ls as you passed variable named ls
<tr>
<th>{{client.id}}</th>
<td>{{client.nome}}</td>
<td>{{client.apelido}}</td>
<td>{{client.morada}}</td>
<td>{{client.tel}}</td>
<td>{{client.nif}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
you must write this code in view.py
def product_detail_view(request):
obj = product.objects.all()
context = { 'allobject' : obj }
return render(request, "product/details.html", context)
you have to write this code in your html file
{% extends 'base.html' %}
{% block content %}
<h1> New about </h1>
<p>
{% for item in allobject %}
{{ item.title }}
{% endfor %}
</p>
{% endblock %}
{% block content %}
{% for x in y %}
{% autoescape off %}
{{x}}
{% endautoescape %}
{% endfor%}
This is quite simple to execute. Use the tools that django provides :)
Try it:
{% extends 'bulma/base.html' %}
{% block title %}Travel{% endblock %}
{% for clientes in ls%}
{% block content %}
<table class="table is-fullwidth is-hoverable">
<thead>
<tr>
<th><abbr title="ID">ID</abbr></th>
<th>Nome</th>
<th>Apelido</th>
<th>Morada</th>
<th>Telemóvel</th>
<th>NIF</th>
</tr>
</thead>
<tbody>
<tr>
<th>{{ls.id}}</th>
<td>{{ls.nome}}</td>
<td>{{ls.apelido}}</td>
<td>{{ls.morada}}</td>
<td>{{ls.tel}}</td>
<td>{{ls.nif}}</td>
</tr>
</tbody>
</table>
{% endblock content %}
{% endfor %}
You need to use the same name of the dict in your forloop.
I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.
Code I'm working with:
{% for row in all_rows %}
<tr>
{% for names in all_fields_names %}
<td> {% getattr(row, names) %} </td>
{% endfor %}
</tr>
{% endfor %}
I get error:
'getattr(row,', expected 'empty' or 'endfor'
I've tried:
{% for row, names in (all_rows, all_fields_names) %}
<tr>
<td> {% getattr(row, names) %} </td>
</tr>
{% endfor %}
With no success. Any ideas?
You can't call functions like that inside the template. You'd want to create a custom template tag.
Custom Template Tag:
#register.simple_tag
def getattr(row, field_name, default=None):
return getattr(row, field_name, default)
Call it inside your template like so:
{% for row, names in (all_rows, all_fields_names) %}
<tr>
<td> {% getattr row names %} </td>
</tr>
{% endfor %}
I would like to display data, three columns per row during my for. I would like my result to look like the following:
<table>
<tr><td>VALUE1</td><td>VALUE2</td><td>VALUE3</td></tr>
<tr><td>VALUE4</td><<td>VALUE5</td><td>VALUE6</td></tr>
</table>
Anyone know how to do it?
Syntax Error
TemplateSyntaxError at /
'for' statements should use the format 'for x in y': for i in range(0, len(all_products_list), 3)
There's a divisibleby tag.
So you can do something (ugly) like:
<table><tr>
{% for field in form %}
<td>{{ field }}</td>
{% if forloop.last %}
</tr>
{% else %}
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endif %}
{% endfor %}
</table>
Alternatively, you could give your form class a table_print method that returns a html string (wrapped in mark_safe).
<table>
{% for i in range(0, len(stuff), 3) %}
<tr>
{% for j in range(3) %}
<td>{{ stuff[i+j] }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Sorry misunderstood question.