How to add two numbers in jinja - django

I have a mark object which I want to list in table
{% for mark in marks %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{mark.subject.name}}</td>
<td>75</td>
<td>25</td>
<td>{{mark.mark}}</td>
<td>{{mark.mark_pr}}</td>
<td>{{ (mark.mark + mark.mark_pr) }}</td>
<td>A</td>
<td>70</td>
</tr>
{% endfor %}
I want to display the sum of mark.mark and mark.mark_pr into third last but when I try doing
(mark.mark + mark.mark_pr) it gives me error saying "Could not parse the remainder: '(mark.mark + mark.mark_pr)' from '(mark.mark + mark.mark_pr)" do anyone have any idea on how to do it?
Thanks in advance.

Related

#DJANGO - I need to display two (or more) rows of a table in my view, currently I can only display one of the rows

Hello people I am working with ticket creation and there is a 1-N relationship, (a ticket can have multiple messages)
I have a view that creates a ticket, in the creation process a message is added - All right here
I have a view that adds a new message to the ticket(s), thus 'activating' the 1-N - All right here
I have a ticket detail view view (code below) - Here starts my difficulty
def ticket_by_id(request, ticket_id, message_id):
mesTicket = MessageTicket.objects.get(pk=message_id)
ticket = Ticket.objects.get(pk=ticket_id)
return render(request, 'support/ticket_by_id.html', {'ticket': ticket, 'messageticket': mesTicket})
the code view above works when the ticket has only one message, but how can I display the multiple messages in this view?
For example in the image below there is my database, highlighting two lines that are linked to ticket 9
database, highlighted in ticket messages 9
Below is an image of my ticket detail view
my detail view of the ticket
How should I display in the view the two messages (or 3, or 4, anyway... more than one) that are related to the ticket, as I would show in my view (image 2) lines 9 and 12 (currently it is only displaying the first registered line linked to the ticket, in this case line 9 of the table) of the my table which are the ones that make up the 1-N with the ticket 9 (image 1)
my html page:
{% extends 'web/base.html' %}
{% block title %} Ticket #{{ticket.id}} {% endblock %}
{% block content %}
<div class="ticket">
<table class="styled-table">
<h2> Ticket #{{ticket.id}}</h2>
<style>
td {
padding: 15px;
}
</style>
<br>
<tbody>
<tr>
<td>ID: </td>
<td>{{ticket.id}}</td>
</tr>
<tr>
<td>Author: </td>
<td>{{messageticket.author_id}}</td>
</tr>
<tr >
<td>Status: </td>
<td>{{ticket.status}}</td>
</tr>
<tr>
<td>Content: </td>
<td>{{messageticket.content}}</td>
</tr>
<tr>
<td>Created At: </td>
<td>{{ticket.created_at}}</td>
</tr>
</tbody>
</table>
</div>
{% endblock %}
First of all, why does your message is not fetch with the id of your ticket, this would ease your work
Second to print multiple "messageticket" you should use something like:
{% for t in messageticket %}
<tr>
<td>Content: </td>
<td>{{ t.content }}</td>
</tr>
{% endfor %}
A loop is needed. As i don't know how messageTicket is done i can't really help you.
But if messageticket have a foreign_key to Ticket then you should be able to to acces it through ticket with ticket.messageticket_set (messageticket_set can be modified if "related_name=" is used in your foreign_key field)
https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_one/
and there you'll have all your messageticket
Hope it help =)

Django print loop index inside List

I am trying to print the list of values from a list. This code works:
<td id="b0">{{data.0.B}}</td>
<td id="b1">{{data.1.B}}</td>
<td id="b2">{{data.2.B}}</td>
<td id="b3">{{data.3.B}}</td>
I am trying to put this in a loop like so:
{% for i in data%}
<td id ='b{{forloop.counter0}}'>
Data is{{ data.forloop.counter0.B }}
</td>
{% endfor %}
This prints the td id as b0, b1 etc correctly. However it is unable to replace the forloop.counter0 inside data.forloop.counter0.B and is not printing it as data.0.B, data.1.B etc.
How do I change the code so that data.forloop.counter0.B is printed in the loop as data.0.B, data.1.B etc?
try following
{% for i in data%}
<td id ='b{{forloop.counter0}}'>
Data is{{ i.B }}
</td>
{% endfor %}

Get parent id in django-mptt

I am using django-mptt and jquery-treetable.
I am printing my objects with:
<table>
{% for node in nodes %}
<tr>
<td>{{ node }}</td>
</tr>
{% endfor %}
</table>
In jquery-treetable the <tr> element should have some attributes to identify which rows are children of which rows.
It needs to have the following setup
<table>
<tr data-tt-id="1">
<td>Parent</td>
</tr>
<tr data-tt-id="2" data-tt-parent-id="1">
<td>Child</td>
</tr>
</table>
but I can't seem to find the right template variables to identify the children correctly. I have only found node.id, node.tree_id, node.level, node.lft, and node.rght.
If your nodes are MPTTModels then you should have a 'parent' relationship to 'self'. Assuming that is the case, you should be able to get the parent id by doing:
node.parent.id

How to make a buffer between rows using start time and end time?

I'm trying to print out free spaces as a buffer zone between each row in a table. Is there a way for me to do this within the django template?
This is the current code I have.
<table id="upcomingtable" border="1" style="border-collapse:collapse;">
<tr>
<th>Course</th>
<th>Title</th>
<th>Professor</th>
<th>Start</th>
<th>End</th>
</tr>
{% for next in upcoming %}
<tr>
<td>{{ next.course }}</td>
<td>{{ next.title }}</td>
<td>{{ next.prof }}</td>
<td>{{ next.stime }}</td>
<td>{{ next.etime }}</td>
</tr>
<tr><td></tr> <!--Buffer here-->
{% endfor %}
</table>
This is my current upcoming query:
upcoming = Class.objects.filter(building__exact=b, floor__exact=f, room__exact=r, days__icontains=dayletter(day), etime__gt=datetime.datetime.now().time()).distinct().order_by('stime')
Basically I want to take the endtime (etime) and find the difference between it with the next start time that is iterated with the next start time and make a buffer in between these rows.
For example the end time now is 3:00pm and the next start time is 3:15pm I want to make a buffer of rows before it prints out the actual row.
Ex.
3:00 info info info
blank
blank
blank
3:15 info info info
for looping within views.py
for i, item in enumerate(range(len(upcoming)-1)):
s1 = upcoming[i].etime
s2 = upcoming[i+1].stime
d2 = s2.hour*60 + s2.minute
d1 = s1.hour*60 + s1.minute
d = d2 - d1
if (d>0):
upcoming[i].span = d/5
Thanks guys, I appreciate all your help!
If I understand you correctly, the number of blank rows depends on the time difference between records, right? If so, you could try something like this:
upcoming = list(upcoming)
for i, item in enumerate(upcoming[:-1]):
item.span = range(1, upcoming[i+1].sdate - item.edate) #actually, get the time difference in minutes and divide by some coefficient - how many minutes should pass for the new blank row to be inserted
And then in template:
{% for row in next.span %}
<tr><td colspan="5"> </td></tr>
{% endfor %}

using django template lanuage to realize greater and less

papis
i have a dict here
dict = {1: [1,2,3,4,5,6,7,8,9],
2: [2,4,5,6,7,8,9,0],
3: [5,2,4,6,12,3,7,6]}
i want show it on my page
so i using template as follows:
{%for item in dict.items%}
<tr>
<td>{{item.0}}</td>
{%for v in item.1%}
here ,i dont know how to handle
if last column and v >5
<td color = 'red'>{{v}}</td>
else
<td>{{v}}</td>
{%endfor%}
</tr>
{%endfor%}
as you see,i want the last column turn red text if its value is greater than 5
how can i realize this ,i google and found nothing
thanks all bro.
when i did as Daniel told:
{%for item in dict.items%}
<tr>
<td>{{item.0}}</td>
{%for v in item.1%}
{%if forloop.last and v > 5%}
<td color = 'red'>{{v}}</td>
{%else%}
<td >{{v}}</td>
{%endif%}
else
<td>{{v}}</td>
{%endfor%}
</tr>
{%endfor%}
it told me the errors:
Could not parse the remainder: '>5' from '>5'
Request Method: GET
Request URL: http://10.64.41.134:8000/monthlyinfo/
Django Version: 1.3
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '>5' from '>5'
what is the problem?
thanks again
crafet
{% if forloop.last and v > 5 %}
Edit You need some spaces. Do it exactly as I have above, and it parses fine.
Generally, your code would be better - and more readable - if you followed PEP8 style for spaces around operators.