data doesn't show on django template - python

My Views
My Template
My Result
My Data
How can I solve this issue?
The data on the django template don't show but when I change them, it is a number it changed.

You can simply iterate through the loop in template like this:
{% for item in leave_type %}
{{ item.id }}
{{ item.title }}
{{ item.amount_leave }}
{% endfor %}

Related

How to render the DateField on a page in django

Hello I just started learning django and I am having problems rending the content the way I want to. Ideally I want to manage a system of tickets and have the ticket number, start-date, end-date and status appear on the page. Similar to the below format
{% extends 'base.html' %}
{% block content %}
<h1>test</h1>
{% for cr in crs %}
<p>{{ cr }}</p>
{% endfor %}
{% endblock content %}
From what I have in my views file the only thing being displayed when i view it in my web browser is only the ticket number.
Any help would be appreciated.
You should render the attributes .start_date and .end_date in your template as well, so:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date }} {{ cr.end_date }} {{ cr.get_status_display }}
{% endfor %}
You can specify the date format with the |date template filter [Django-doc]:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date|date:"Y-m-d" }} {{ cr.end_date|date:"Y-m-d" }} {{ cr.get_status_display }}
{% endfor %}

How to count the number of resuts of an if on Django built in templates

I have the following situation:
{% for subject in subjects %}
{% if subject.media < 60 %}
{{ subjects|length }}
{% endif %}
{% endfor %}
The result of that is "161616", because I have 16 subjects on my database, but I want to show the number of subjects that are below the media, like "3".
You can't do logic like that in the template. Make the query in the view
def my_view(request):
num_under_60 = Subject.objects.filter(media__lt=60).count()
return render(request, 'my_template.html', {'num_under_60': num_under_60})
then use {{ num_under_60 }} in the template.
I think that you should write {{ subject|length }} instead of {{ subjects|length }}.

how to make the change_password template in flask-user to extend a different template based on user-roles

i have this code for change_password.html in flask-user:
{% extends 'admin.html' %}
{% block content %}
{% from "flask_user/_macros.html" import render_field, render_submit_field %}
<h1>{%trans%}Change password{%endtrans%}</h1>
<form action="" method="POST" class="form" role="form">
{{ form.hidden_tag() }}
{{ render_field(form.old_password, tabindex=10) }}
{{ render_field(form.new_password, tabindex=20) }}
{% if user_manager.enable_retype_password %}
{{ render_field(form.retype_password, tabindex=30) }}
{% endif %}
{{ render_submit_field(form.submit, tabindex=90) }}
</form>
{% endblock %}
I would like the template to extend two different templates namely employee.html and admin.html based on user roles. that is if the user is an employee, then the template extends employee.html and admin.html is extended if the user is an admin. how do i achieve this?
According to the Jinja's documentation there must be only one extends statement, that should be placed on the top of the file. To overcome this limitation you may join your two templates into the one, and switch between them using if statement inside the new template.

Loop through a Python list in Django template with for loop

I need to simplify this Django template,
{{ var.1 }}
{{ var.2 }}
{{ var.3 }}
{{ var.4 }}
{{ var.5 }}
var is Python list passed as context to the template
how do you convert the above template using a for tag construct. I tried this but does not work.
{% for i in var|length %}
{{ var.i }}
{% endfor %}
You can just do
{% for x in var %}
{{x}}
{% endfor %}

Jinja loop.index does not print

When running the following jinja code, I only get "Column info" printed. Why the index does not appears ?
{% for field in columns_form %}
{% if 'title_' in field.name %}
<td>Column {{ loop.index }} info</td>
{% endif %}
{% endfor %}
It sounds like the template is being treated as a Django template, not a Jinja template.
Using {{ loop.index }} should work in a Jinja template, but wouldn't work in a Django template, where you would use {{ forloop.counter }} instead.
In case {{ loop.index }} does not work in the latest version, a workaround is to zip the columns_form and range(0, len(columns_form)+1) in the python file as
columns_form_idx = zip(columns_form, range(0, len(columns_form)+1))
In the template file,
{% for field, idx in columns_form_idx %}
{% if 'title_' in field.name %}
<td>Column {{ idx }} info</td>
{% endif %}
{% endfor %}

Categories