Display table data django - django

I want to display data from a table in a table; to retrieve the object I used the foreign key to retrieve the object (person depending on the project)
I have two tables: person & project.
class person (models.Model)
project =models.ForeignKey('plat_project.Project', blank=True,related_name='project')
pourcentage = models.IntegerField()
sum = models.IntegerField()
class project
...
in the views :
in the display function :
PersonneProject=PersonneProjet.objects.all(projet=project)
response['Listeperson']=PersonneProject
And the template :
{% for p in Listeperson %}
<tr>
<td style="text-align:center">{{ i }} </td>
<td style="text-align:center">{{ p.pourcentage}}% </td>
<td style="text-align:center">{{ p.sum }}€ </td>
</tr>
{% endfor %}
I have two problems:
I can not display the data on my chart
How to increment the i 1

You'll need to provide more information about your view function to fix the first problem (how and what are you returning from this function? Note also your typo in projet=project), but you get the loop counter with forloop.counter:
<td style="text-align:center">{{ forloop.counter }} </td>
(or forloop.counter0 if you want zero-based indexes).

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 %}

Make a form from an array of objects gotten by my_model.objects.all() (for a bulk edit)

Really a list of forms for each instance of the model the form is based on.... currently:
I have a list of data, currently, in the view I do a:
def get_booking_list(request):
object_list = HousingRequests.objects.all()
return render(request, 'template.html', {'housing_list':object_list})
With this I can then in my template have a nice table built:
<table>
<tr>
<th> User </th>
<th> Check in Date </th>
<th> Check out Date</th>
<th> Room </th>
</tr>
{% for hou in housing_list %}
<tr>
<td> {{ hou.user.first_name }} {{ hou.user.last_name}} </td>
<td> {{ hou.checkin_date}} </td>
<td> {{ hou.checkout_Date }} </td>
<td> {{ hou.room }} </td>
</tr>
{% endfor %}
</table>
This makes sense to me, I see a list of all my objects found that needed updating (I can even filter this list further in the view). The problem is:
When a user books a room, the admin has to select which room to put them in so all the rooms say 'none', I want from the list the admin to have access to all the rooms (a list of just about 9 or so) and select which room to put each person in.
So I almost want to do a bulk edit.
I thought I could do this by also passing to this template the list of rooms in a room list, and by hand creating an option for that field in the table that has all the rooms (and a ---- if none is there) and then some ajax to see if that field was selected and to ping back to the server in a view which record had the room changed to which and do a save/update on that.
I am guessing this is the preferred way, though creating the option by hand in html using the django template tags seems laborous and I was wondering if there was a way to do this with forms somehow, most the examples I see is of the tone:
Here is your one instance of your model, here is a form for that model. It doesn't say oh here is a form that is repeated for each instance of your model, which I guess is what I want. I don't see any tutorials that show how this might be structured. Or if I should just bite the bullet and roll the code to do it all by hand option boxes in html with ajax (and probably some jquery)

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 generate dynamic name for a <td> and pass the name of it to server

I have the following code :
{% for assessments in list_assessments%}
<form action="/test/" method="post">{%csrf_token%}
<tr>
<td>{{assessments.assessment_id}}</td>
<td>{{assessments.name}}</td>
<td>{{assessments.assessment_begin_date}}</td>
<td>{{assessments.assessment_end_date}}</td>
<td>{{assessments.is_active}}</td>
<td>{{assessments.is_complete}}</td>
<td>{{assessments.created_at}}</td>
<td>{{assessments.updated_at}}<br></td>
<td><input type="submit" value="Edit Assessment" /></td>
</tr>
{%endfor%}
</form>
All the data here are dynamically coming.
In this following code, i need to assign an name to assessments.name dynamically, something like
<td name="dynamic_name">{{assessment.name}}</td>.
And on clicking the button "Edit Assessment", i want the dynamic_name to be passed and received my the view.
The idea is each assessment has its own set of parameters. I want to display only the parameters related to the name. So if i could pass the value i would be able to do it.
Any help appreciated.
Your ending **</form>** tag should be before for loop.
{% for assessments in list_assessments%}
<form action="/test/" method="post" name="form-{{ assessments.counter }}">{%csrf_token%}
<tr>
<td>{{assessments.assessment_id}}</td>
<td>{{assessments.name}}</td>
<td>{{assessments.assessment_begin_date}}</td>
<td>{{assessments.assessment_end_date}}</td>
<td>{{assessments.is_active}}</td>
<td>{{assessments.is_complete}}</td>
<td>{{assessments.created_at}}</td>
<td>{{assessments.updated_at}}<br></td>
<td><input type="submit" value="Edit Assessment" /></td>
</tr>
</form>
{%endfor%}
Now, You can get specific block values by form name ( see above code ) in javascript as well as in python.
In Javascript,
form = document.getElementByTagName("form")
elems = form.children("td")
elems will give you all td elements.