Interval in Django template - django

I have an interval type field that contains hours and minutes in this format: "28:00".
But in my template, it is translated to "1 day, 4:00:00"
How can I keep this "28:00" in my template?
My template:
<table class="table table-bordered ">
<thead align="center">
<th class="text-center" scope="col">User</th>
<th class="text-center" scope="col">Week number</th>
<th class="text-center" scope="col">Total H</th>
</thead>
<tbody>
{% for tp in resume_h %}
<tr scope="row">
<td class="text-center col-md-1">{{ tp.user_name }}</td>
<td class="text-center col-md-1">{{ tp.num_semaine }}</td>>
<td class="text-center col-md-1">{{ tp.hours_total|linebreaksbr }}</td>
</tr>
{% endfor %}
</tbody>
</table>
My model:
class hours(models.Model):
user_name = models.CharField(max_length=50, blank=True, null=True)
num_semaine = models.IntegerField(("No:"),blank=True, null=True)
hours_total = models.TimeField(("H"),blank=True, null=True)
def __str__(self):
return str(self.user_name)
Thank you

Related

How to get foreign key detailed values based on id in html template in django

In Models.py
class Interview(models.Model):
Current_Date = models.DateField(auto_now_add=True )
User = models.ForeignKey(User,on_delete=models.CASCADE)
Recuirement = models.ForeignKey(Client_Requirement,on_delete=models.CASCADE)
Candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
Interviewer = models.ForeignKey(Interviewer, on_delete=models.CASCADE)
Interview_Mode = models.ForeignKey(Interview_Mode, on_delete=models.CASCADE)
Date = models.DateField()
Time = models.TimeField()
Interview_status = models.ForeignKey(Interview_status, on_delete=models.CASCADE)
Interview_Location = models.CharField(max_length=30)
Comments = models.TextField(max_length=1000)
Update_Date = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.Interviewer)
in Views.py
def BD(request):
Interview_data = Interview.objects.all()
context={'Interview_data':Interview_data}
return render(request,'Sub/BD.html', context)
How i can get all the data in Interview table and also individual details of Recuirement, Candidate table in modal bootstrap 5
In for loop tag in django
<div class="row">
<div class="row">
<h1>Graphs</h1>
</div>
<div class="row">
<div class="col-lg-12" style="padding: 20px 60px 0 60px;">
<table class="table table-hover table-bordered">
<thead>
<tr class="table-dark">
<th scope="col">Id</th>
<th scope="col">Created_on</th>
<th scope="col">Recruiter</th>
<th scope="col">Requirement</th>
<th scope="col">Candidate</th>
<th scope="col">Interview_Mode</th>
<th scope="col">Data&Time_Interview</th>
<th scope="col">Interview_Status</th>
<th scope="col">Interview_Location</th>
<th scope="col">Comments</th>
<th scope="col">Last_updated</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
{% for interview in Interview_data %}
<tr>
<th scope="row">{{interview.id}}</th>
<th scope="row">{{interview.Current_Date}}</th>
<th scope="row">{{interview.User}}</th>
<td>
{{interview.Recuirement}}
<div class="modal fade" id="Requirement" tabindex="-1" aria-labelledby="requirement" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="requirement">Requirement Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<table class="table">
<tbody>
<tr>
<th scope="row">ID</th>
<td>{{ interview.Recuirement.id }}</td>
</tr>
<tr>
<th scope="row">Company Name</th>
<td>{{ interview.Recuirement.Client_Info }}</td>
</tr>
<tr>
<th scope="row">Position</th>
<td>{{ interview.Recuirement.Position }}</td>
</tr>
<tr>
<th scope="row">Department</th>
<td>{{ interview.Recuirement.Department }}</td>
</tr>
<tr>
<th scope="row">Experience</th>
<td>{{ interview.Recuirement.From_Year }} Years {{ interview.Recuirement.To_Year }} Months</td>
</tr>
<tr>
<th scope="row">Notice Period</th>
<td>{{ interview.Recuirement.NoticePeriod }}</td>
</tr>
<tr>
<th scope="row">Salary Package</th>
<td>{{ interview.Recuirement.Salary_package}}</td>
</tr>
<tr>
<th scope="row">Location</th>
<td>{{ interview.Recuirement.Location }}</td>
</tr>
<tr>
<th scope="row">No of Openings</th>
<td>{{ interview.Recuirement.No_Of_Openings }}</td>
</tr>
<tr>
<th scope="row">Job Description</th>
<td>{{ interview.Recuirement.Job_Description }}</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</td>
<td><button type="button" style="border-radius: 30px;" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#Candidate">
{{interview.Candidate}}
</button>
<div class="modal fade" id="Candidate" tabindex="-1" aria-labelledby="candidate" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="candidate">Candidate Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<table class="table">
<tbody>
<tr>
<th scope="row">id</th>
<td>{{ interview.Candidate.id }}</td>
</tr>
<tr>
<th scope="row">Current_Date</th>
<td>{{ interview.Candidate.Current_Date }}</td>
</tr>
<tr>
<th scope="row">Candidate_Name</th>
<td>{{ interview.Candidate.Candidate_name }}</td>
</tr>
<tr>
<th scope="row">Client</th>
<td>{{ interview.Candidate.Client_Info }}</td>
</tr>
<tr>
<th scope="row">Qualification</th>
<td>{{ interview.Candidate.Qualification }}</td>
</tr>
<tr>
<th scope="row">Experience</th>
<td>{{ interview.Candidate.Year }} Years {{ interview.Candidate.Months }} Months</td>
</tr>
<tr>
<th scope="row">Current_Salary</th>
<td>{{ interview.Candidate.Current_Salary }}</td>
</tr>
<tr>
<th scope="row">Current_Location</th>
<td>{{ interview.Candidate.Current_Location}}</td>
</tr>
<tr>
<th scope="row">Expected_Salary</th>
<td>{{ interview.Candidate.Expected_Salary }}</td>
</tr>
<tr>
<th scope="row">Preferred_Location</th>
<td>{{ interview.Candidate.Preferred_Location }}</td>
</tr>
<tr>
<th scope="row">Languages_Known</th>
<td>{{ interview.Candidate.Languages_Known }}</td>
</tr>
<tr>
<th scope="row">Marital_Status</th>
<td>{{ interview.Candidate.Marital_Status }}</td>
</tr>
<tr>
<th scope="row">Candidate_Notice_Period</th>
<td>{{ interview.Candidate.Candidate_Notice_Period }}</td>
</tr>
<tr>
<th scope="row">Email</th>
<td>{{ interview.Candidate.Email }}</td>
</tr>
<tr>
<th scope="row">Primary_Contact_No</th>
<td>{{ interview.Candidate.Primary_Contact_No }}</td>
</tr>
<tr>
<th scope="row">Secondary_Contact_No</th>
<td>{{ interview.Candidate.Secondary_Contact_No }}</td>
</tr>
<tr>
<th scope="row">Comments</th>
<td>{{ interview.Candidate.Comments }}</td>
</tr>
<tr>
<th scope="row">Update_Date</th>
<td>{{ interview.Candidate.Update_Date }}</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div></td>
<td>{{interview.Interview_Mode}}</td>
<td>{{interview.Date}}-{{interview.Time}}</td>
<span class="badge rounded-pill bg-primary">Primary</span>
<td>{{interview.Interview_status}}</td>
<td>{{i.Interview_Location}}</td>
<td>{{interview.Comments}}</td>
<td>{{interview.Update_Date}}</td>
<td>Edit</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>

Update data in database based on order id in Django

I am new in Django.
I want to update the value in the database based on the order id. Therefore, every order id has different updates. But, i only can update the last item that i add to the order. And every previous orders that i have, will directly follow the update from the last item.
models.py
class OrderItem(models.Model):
Table_No = models.IntegerField(blank=False)
FoodId = models.TextField()
Item = models.TextField()
Qty = models.DecimalField(max_digits=5, decimal_places=0)
Price = models.DecimalField(max_digits=10, decimal_places=2)
TotalPrice = models.TextField()
Note = models.TextField(max_length=100, null=True)
OrderId = models.TextField(max_length=5, null=True)
FoodStatus = (
('1', 'Has been ordered'),
('2', 'cooked'),
('3', 'ready to be served'),
('4', 'done'),
)
food_status = models.CharField(max_length=50, choices=FoodStatus)
views.py
def kitchen_view(request):
chef_view = OrderItem.objects.all()
if request.method == "POST":
order_id = request.POST.get("OrderId")
status = OrderItem.objects.filter(OrderId=request.POST.get("OrderId"))
status.status1 = OrderItem.objects.update(food_status=request.POST.get("food_status"))
return render(request, 'restaurants/kitchen_page.html', {'chef_view': chef_view})
kitchen_page.html
<form action="#" method="post">
<style>
table, th, td {
border: 1px solid black;
}
</style>
{% csrf_token %}
{% for order in chef_view %}
<table width="800">
<tr>
<th width="800">Table Number</th>
<th width="800">Item</th>
<th width="800">Quantity</th>
<th width="800">Price</th>
<th width="800">Note</th>
<th width="800">Order Id</th>
<th width="800">Status</th>
</tr>
<tr>
<td width="800">{{ order.Table_No }}</td>
<td width="800">{{ order.Item }}</td>
<td width="800">{{ order.Qty }}</td>
<td width="800">{{ order.Price }}</td>
<td width="800">{{ order.Note }}</td>
<td width="800">{{ order.OrderId }}</td>
<td width="800">{{ order.food_status }}
<input type="text" name="food_status">
</tr>
</table>
{% endfor %}
<br><a href='' button onclick="myFunction()"><input type="submit" value="Change Status"></button>
</form>
The result should be able to update the food_status based on the order_id. Therefore, every order_id may have different food_status and show it to the template.
Anyone can help me to solve this problem? I really need the help to solve this issue. Really appreciate.
Ok so real problem is that your form is incorrect - you are not sending OrderId to view. Here's quickfix to it:
kitchen_page.html:
<style>
table, th, td {
border: 1px solid black;
}
</style>
<form action="#" method="post">
<table width="800">
<tr>
<th width="800">Table Number</th>
<th width="800">Item</th>
<th width="800">Quantity</th>
<th width="800">Price</th>
<th width="800">Note</th>
<th width="800">Order Id</th>
<th width="800">Status</th>
</tr>
{% csrf_token %}
{% for order in chef_view %}
<tr>
<td width="800">{{ order.Table_No }}</td>
<td width="800">{{ order.Item }}</td>
<td width="800">{{ order.Qty }}</td>
<td width="800">{{ order.Price }}</td>
<td width="800">{{ order.Note }}</td>
<td width="800">{{ order.OrderId }}</td>
<td width="800">{{ order.food_status }}
<input type="text" name="food_status" value="{{ order.food_status }}">
</tr>
<input type="hidden" name="OrderId" value="{{ order.OrderId }}">
{% endfor %}
</table>
<br><button type="submit" value="Change Status">Change Status</button>
</form>
views.py:
def kitchen_view(request):
if request.method == "POST":
order_ids = request.POST.getlist("OrderId")
food_statuses = request.POST.getlist("food_status")
for i in range(len(order_ids)):
OrderItem.objects.filter(OrderId=order_ids[i]).update(food_status=food_statuses[i])
chef_view = OrderItem.objects.all()
return render(request, 'restaurants/kitchen_page.html', {'chef_view': chef_view})
Previously stored filtered objects are not being updated as you have
status = OrderItem.objects.filter(OrderId=request.POST.get("OrderId"))
but when you are updating them, you should be updating the already filtered objects like
status = status.update(food_status=request.POST.get("food_status"))
Hope this helps.
You can update multiple objects at once including the filter in a single line as:
status = OrderItem.objects.filter(OrderId=request.POST.get("OrderId")).update(food_status=request.POST.get("food_status"))
The problem in your code:
status.status1 = OrderItem.objects.update(food_status=request.POST.get("food_status"))
was that you were not using the filter, instead you tried updating the status1 field(Can't see a status1 field in your model) using status.status1.

How to persist data found in tables within a django template, in postgresql

I am scraping several sites and I show the data obtained in 6 tables within a django template.
My intention is to persist the data of the tables in postgresql, but I can not realize how to perform that task.
In principle I am trying to save the data from the second table.
For this I have created the models that I show below, as well as a view that is called: registroDivisaArgentina ().
The template is called quotes.html and within it, there are 6 tables.
I have tried to work with a class called: RegisterArgentineValues () within a forms.py file
models.py
class DivisasArgentina(models.Model):
class Meta:
ordering = ['CodigoDA']
CodigoDA = models.CharField(max_length=50, primary_key = True)
Texto_para_Reporte = models.CharField(max_length=70)
def __str__(self):
return '{}'.format(self.Texto_para_Reporte)
class ValoresDivisasArgentina(models.Model):
class Meta:
ordering = ['Dia']
DivisasArgentina = models.ForeignKey(DivisasArgentina, on_delete=models.PROTECT)
Dia = models.DateField(default=date.today)
Hora = models.DateTimeField(default=timezone.now)
Compra = models.FloatField()
Venta = models.FloatField()
Variacion_dia_anterior = models.FloatField()
ClaveComparacion = models.CharField(max_length=1)
def __str__(self):
return '{} - {} - {}'.format(self.DivisasArgentina, self.Dia, self.ClaveComparacion)
cotizaciones.html
{% extends 'base.html' %}
{% block contenido %}
<form method="POST" class="post-form">{% csrf_token %}
<div class="container">
<table class="table table-striped table-bordered" id="tab1">
<thead>
<tr>
<th>Divisas en el Mundo</th>
<th>Valor</th>
</tr>
</thead>
<tbody>
<tr>
{%for element in cotiz_mun%}
<tr>
{% for key,value in element.items %}
<td> {{ value }} </td>
{% endfor %}
</tr>
{% endfor %}
</tr>
</tbody>
</table>
</div>
<div class="container">
<table class="table table-striped table-bordered" id="tab2">
<thead>
<tr>
<th>Divisas en Argentina</th>
<th>Compra</th>
<th>Venta</th>
</tr>
</thead>
<tbody>
<tr>
{%for element in cotiz_arg%}
<tr>
{% for key,value in element.items %}
<td>{{ value }} </td>
{% endfor %}
</tr>
{% endfor %}
</tr>
</tbody>
<thead>
{{ form.as_table }}
</table>
</div>
<div class="container">
<table class="table table-striped table-bordered" id="tab3">
<thead>
<tr>
<th>Dolar Banco Nacion Argentina (Divisas)</th>
<th>Compra</th>
<th>Venta</th>
</tr>
</thead>
<tbody>
<tr>
{%for element in cotiz_exp%}
<tr>
{% for key,value in element.items %}
<td>{{ value }} </td>
{% endfor %}
</tr>
{% endfor %}
</tr>
</tbody>
</table>
<table class="table table-striped table-bordered" id="tab4">
<thead>
<tr>
<th colspan="4">Dolar Futuro en Argentina</th>
</tr>
<tr>
<th>Mes 1</th>
<th>Mes 2</th>
<th>Mes 3</th>
<th>Mes 4</th>
</tr>
</thead>
<tbody>
<tr>
{%for element in cotiz_dol%}
<td>
{{ element.Valores}}
</td>
{% endfor %}
</tr>
</tbody>
</table>
<table class="table table-striped table-bordered" id="tab5">
<thead>
<tr>
<th colspan="3">Indicadores Varios - Tasa Libor</th>
</tr>
<tr>
<th>Libor a 1 Mes</th>
<th>Libor a 2 Mes</th>
<th>Libor a 3 Mes</th>
</tr>
</thead>
<tbody>
<tr>
{%for element in cotiz_lib%}
<td>
{{ element.Valores }}
</td>
{% endfor %}
</tr>
</tbody>
</table>
<table class="table table-striped table-bordered" id="tab6">
<thead>
<tr>
<th>Indicadores Varios - Indice Merval y Oro</th>
<th>Valores</th>
</tr>
</thead>
<tbody>
<tr>
{%for element in cotiz_ind%}
<tr>
{% for key,value in element.items %}
<td> {{ value }} </td>
{% endfor %}
</tr>
{% endfor %}
</tr>
</tr>
</tbody>
</table>
</div>
<div class="container" id="saveData">
<br></br>
<button type="submit" class="btn btn-primary pull-right">Guardar Datos</button>
</div>
</form>
{% endblock %}
views.py
def mostrar_cotizaciones(request):
cotiz_arg = json.loads(j_cotizaciones_argentina)
cotiz_mun = json.loads(j_cotizaciones_mundiales)
cotiz_exp = json.loads(j_cotizacion_export)
cotiz_dol = json.loads(j_dolar_futuro)
cotiz_ind = json.loads(j_indicadores)
cotiz_lib = json.loads(j_libor)
context = {'cotiz_mun': cotiz_mun,
'cotiz_arg': cotiz_arg,
'cotiz_exp': cotiz_exp,
'cotiz_dol': cotiz_dol,
'cotiz_ind': cotiz_ind,
'cotiz_lib': cotiz_lib,
}
return render(request, 'cotizaciones.html', context)
def registrarDivisaArgentina(request):
if request.method == 'POST':
formulario = RegistrarValoresDivisasArgentinas(request.POST)
if formulario.is_valid():
formulario.save()
return HttpResponseRedirect('/listadoValores')
else:
formulario = RegistrarValoresDivisasArgentinas()
formulario.setup('Registrar', css_class="btn btn-success")
return render(request, 'cotizaciones.html', {'formulario':formulario})
forms.py
from django import fla
from django.forms import ModelForm
from django import forms
from fla.models import *
class RegistrarValoresDivisasArgentinas(forms.ModelForm):
class Meta:
model = ValoresDivisasArgentina
fields= [Compra, Venta]
I have done some tests, but none has given a favorable result. Someone can tell me how to process the data (in the views and forms) that are in the tables, to be able to store them in my postgres tables ?
I do this kind of task very often, and I've found out that the combination of ScraPy with scrapy-djangoitem is a very good combo to use here.
Good luck!

Django-mptt display hierarchy as table

I am trying to display a hierarchical table as a normal table.
model.py
class Dimension_value(MPTTModel):
name = models.CharField(max_length = 200, null=True, blank = True, default = '')
parent = TreeForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, related_name="children")
class MPTTMeta:
order_insertion_by = ['name']
def __str__(self):
return self.name
views.py
def show_genres(request):
return render(request, "accounts/dimension_detail.html", {'dimensions': Dimension_value.objects.all()})
dimension_detail.html
<div class="container-fluid">
<div class="card">
<div class="card-body">
<h2>My dimensions</h2>
{% load mptt_tags %}
<ul>
{% recursetree dimensions %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
</div>
</div></div>
Using django-mptt the lists show just fine. However I have no idea how to display this in a normal table.
For example
Europe|The Netherlands|Noord-Holland
Europe|The Netherlands|Zuid-Holland
Europe|Belgium|Vlaanderen
what I currently get
<table>
<thead>
<tr>
<th class="id orderable">ID</th>
<th class="name orderable">Name</th>
<th class="orderable parent">Parent</th>
<th class="asc lft orderable">Lft</th>
<th class="orderable rght">Rght</th>
<th class="asc orderable tree_id">Tree Id</th>
<th class="level orderable">Level</th>
</tr>
</thead>
<tbody>
<tr class="even">
<td class="id">6</td>
<td class="name">Asia</td>
<td class="parent">—</td>
<td class="lft">1</td>
<td class="rght">4</td>
<td class="tree_id">1</td>
<td class="level">0</td>
</tr>
<tr class="odd">
<td class="id">8</td>
<td class="name">Japan</td>
<td class="parent">Asia</td>
<td class="lft">2</td>
<td class="rght">3</td>
<td class="tree_id">1</td>
<td class="level">1</td>
</tr>
<tr class="even">
<td class="id">5</td>
<td class="name">Europe</td>
<td class="parent">—</td>
<td class="lft">1</td>
<td class="rght">8</td>
<td class="tree_id">2</td>
<td class="level">0</td>
</tr>
<tr class="odd">
<td class="id">7</td>
<td class="name">Belgium</td>
<td class="parent">Europe</td>
<td class="lft">2</td>
<td class="rght">7</td>
<td class="tree_id">2</td>
<td class="level">1</td>
</tr>
<tr class="even">
<td class="id">9</td>
<td class="name">Vlaanderen</td>
<td class="parent">Belgium</td>
<td class="lft">3</td>
<td class="rght">4</td>
<td class="tree_id">2</td>
<td class="level">2</td>
</tr>
<tr class="odd">
<td class="id">10</td>
<td class="name">Wallonie</td>
<td class="parent">Belgium</td>
<td class="lft">5</td>
<td class="rght">6</td>
<td class="tree_id">2</td>
<td class="level">2</td>
</tr>
</tbody>
</table>

django trying to put a table in a view

in my views.py:
def show(request):
query_results=Emails.objects.all()
#from py_utils import open_py_shell;open_py_shell.open_py_shell()
context = { 'query_result' : query_results }
return render(request, 'show.html', context)
in show.html:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
{% for item in query_result %}
<tr>
<td>Ohad</td>
<td>{{ item.email }}</td>
<td>{{ item.baseurl }}</td>
</tr>
{% endfor %}
</tbody>
</table>
an email object has email field and baseurl field but it seems that i cannot load it,
i checked and in query_result i have a list of Emails object just cannot put it in the table, any help?