django 如何从以月份日期为表头的表中的模型中取数?

myss37ts  于 2022-12-14  发布在  Go
关注(0)|答案(3)|浏览(110)

我是Django的新手,我正在尝试从模型中检索数据到html表中。本质上,我想检索用户选择的特定月份的所有教师和他们的出勤状态(出席,缺席和迟到)。我能够从视图中传递上下文到模板,这是我的代码

型号.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"

查看次数.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)

我不知道这是否是在视图函数中检索数据的最佳Python方法。
这是我

模板

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

目前这是我在浏览器

中得到的
我想实现类似于

的内容
任何建议或代码重组,以实现预期的目标是很好的来,谢谢.

oiopk7p5

oiopk7p51#

我知道现在回答这个问题已经太晚了,但是我最近正在为员工创建一个考勤系统。我需要的正是这个东西。在互联网上搜索后,我没有找到任何合适的东西。所以我创建了一个自定义模板过滤器,它以用户和日期为参数,并返回该日期的考勤
下面是代码:

@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 %} ```
blmhpbnm

blmhpbnm2#

你需要把所有的信息存储在一个列表中。例如:

get_all_details = list()
for i in teacher:
    x=i.id
    get_all_details.append(TeacherAttendance.objects.filter(teacher__user=x, date__month=grab_data_passed))

并在模板中呈现:

context['get_all_details']=get_all_details

并更新模板:

{% for get_details in get_all_details %}
    {% 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 %}
{% endfor %}
ipakzgxi

ipakzgxi3#

尝试

<th>{% for day in days %}{{day}}{% endfor %}</th>

代替

<th>{{days}}</th>

之后,您需要将教师列表发送到视图中的上下文,类似于

teacher = User.objects.filter(is_teacher=True)
context['teachers'] = teacher

然后在你的html中尝试

<tbody>
   {% for teacher in teachers %}
     <tr>
       <td>{{ teacher }}</td>
       {% for details in get_details %}
       {% for day in days %}
         {% if detail.teacher == teacher and detail.date|date:'d' == day %}<td>{{ detail.status }}</td>{% else %}<td>--</td>{% endif %}
       {%endfor%}
       {%endfor%}
      </tr>
</tbody>

有一件事你必须改变是你需要另一种格式的日期列表它必须是[01,02,03,04,.. 10,.. 20,.. 30]

相关问题