Issue
Am new to Django, am trying to retrieve data from the models into html table. Essentially, I want to retrieve all teachers and their attendance Status(Present, Absent and Late ) in a particular month that the user selects. Am able to pass the context to the template from the views here is my code
models.py
class TeacherAttendance(models.Model):
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, blank=False, null=True)
date = models.DateField()
status = models.CharField(max_length=50, choices=ATTENDANCE)
objects = models.Manager()
class Meta:
default_permissions = ('view', 'add', 'change', 'delete')
ordering = ["date", ]
get_latest_by = "date"
views.py
def teacher_attendance_report(request):
context = {}
form = TeacherAttendanceForm(request.POST)
if request.method == 'POST':
if request.POST.get('group_by'):
grab_data_passed = request.POST.get('group_by')
get_details = TeacherAttendance.objects.filter(date__month=grab_data_passed)
z=0
range=9
days=[]
if z< range:
dats = TeacherAttendance.objects.filter(date__month=grab_data_passed)[z].date
z+=1
y=dats.month
x=dats.year
cal = calendar.TextCalendar(calendar.WEDNESDAY)
for day in cal.itermonthdays(x, y):
days.append(day)
context['days']=days
teacher = User.objects.filter(is_teacher=True)
for i in teacher:
x=i.id
get_details = TeacherAttendance.objects.filter(teacher__user=x, date__month=grab_data_passed)
context['get_details']=get_details
return render(request, 'Reports/teacher_attendance_report_details.html', context)
context={'form':form}
return render(request, 'Reports/teacher_attendance_report_index.html', context)
I don't know whether that's the best pythonic way of retrieving data in a view function.
here is my
template
<table id="datatable-responsive"
class="datatable-responsive table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Teacher <i class="fa fa-long-arrow-down" z></i> - Date <i class="fa fa-long-arrow-right"></i></th>
<th>{{days}}</th>
</tr>
</thead>
<tbody>
{% for details in get_details %}
<tr>
<td>{{ details.teacher }}</td>
<td>{{ details.status }}</td>
</tr>
{% if empty %}
<tr>
<td colspan="9" class="text-center bg-warning">No Data</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
Currently this is what I am getting in the browser
I want to achieve something similar to
Any advice or code restructuring to achieve desired target is well come, thank you.
Solution
I know its too late to answer this question, But i was recently creating a attendance system for employees. I needed exactly this thing. after searching the internet i did not found any suitable thing. so i created a custom template filter which takes the user and date as argument and return the attendance of that date
here is the code:
@register.filter(name='gmd')
def get_attendance(user,date):
a = Attendance.objects.filter(user=user,attendance__date=date).values_list('attendance',flat=True)
if a:
if a.count() > 1:
l = ''
for i in a:
l += (i.strftime('%m/%d/%Y, %H:%M:%S %p')) + '\n'
return l
else:
l = list(a)[0]
return l.strftime('%m/%d/%Y, %H:%M:%S %p')
return '--'
Template File:
{% load attendance_extras %}
{% block content %}
<h1 class='text-center m-2'>Attendance Record of Month {{month}}</h1>
<div class="table-responsive">
<table class="table table-hover table-dark table-striped table-bordered">
<thead>
<tr>
<th scope="col">Employee Name</th>
{% for date in days_list %}
<th scope="col" class ='text-center' name="{{date|date:'d-m-Y'}}">
{{date|date:'d/m/Y'}}
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<th class ='text-center' name="{{user}}" scope="row">{{user}}</th>
{% for date in days_list %}
<td >{{ user|gmd:date|linebreaks }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock content %} ```
Answered By - Sourabh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.