2017-08-11 57 views
0

我正在使用Django为健身房教练做一个网站。 我想是所有培训师这样如何显示Django中所有用户的每日日程安排?

但每天的日程安排一个模板,我的网页是 enter image description here

的问题是每个教练的TR重复多达数量“TD”训练师的时间表。我知道{% for sc in schedules %}是问题所在。但是,因为时间表是查询集合,所以我应该使用for和while来使用for,我应该检查将时间表插入到右侧tr,td位置的正确时间。我如何让成功的表格显示所有用户(培训师)的每日时间表?任何人都会对我非常有帮助。

Schedule.model

class Schedule(models.Model): 
    Trainer = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True,null=True, related_name='schedule', on_delete=models.SET_NULL) 
    title = models.CharField(max_length=12,blank=True,) 

    start = models.DateTimeField(null=True, blank=True) 

我views.py

def staff_daily_schedule_search(request): 

all_schedules = Schedule.objects.all() 
Fitness_list = User.objects.filter(groups__name='Fitness') # Fitness Trainers 

search_date1 = request.GET.get('search_date','') 
search_date= datetime.datetime.strptime(search_date1, '%Y-%m-%d') #%T = %H:%M:%S '%Y-%m-%d' 

schedules= Schedule.objects.none() 
for f in Fitness_list: 
    sc = f.schedule.filter(start__year=search_date.year).filter(start__month = search_date.month).filter(start__day = search_date.day) 
    print(sc) 
    schedules |= sc 

context = { 
    'search_date' : search_date1 if search_date1 else datetime.date.today(), 
    'Fitness_list':Fitness_list, 
    'schedules' : schedules, 
} 
return render(request, 'management/staff_daily_schedule.html', context) 

staff_daily_schedule.html

<form action="{% url 'management:staff_daily_schedule_search' %}" method="GET"> 
    <span><input type="date" class="search_date my-control" name="search_date" value="{{ search_date }}" ></span> 
      <a id="today" class="btn btn-warning">오늘</a> 
    <button class="btn btn-info" value="검색" >검색</button> 
</form> 
<table class="table table-bordered"> 
<thead> 
    <tr> 
     <th></th> 
     <th>06:00 </th> 
     <th>07:00 ~ 07:50</th> 
     <th>08:00 ~ 08:50</th> 
     <th>09:00 ~ 09:50</th> 
     <th>10:00 ~ 10:50</th> 
    </tr> 
</thead> 
<tbody> 
    {% for trainer in Fitness_list %} 
    <tr> 
     <td>{{ trainer }} </td> 

      {% for sc in schedules %} <!-- because of this for, td repeats as many as the number of schedule per trainer has..--> 
      {% if sc.Trainer == trainer %} 

       {% if sc.start.hour == 21 %} <!--HOUR of 6:00 a.m = 21--> 
        <td>{{ sc }}</td> 
       {% else %} 
        <td ></td> 
       {% endif %} 

       {% if sc.start.hour == 22 %} 
        <td>{{ sc }}</td> 
       {% else %} 
        <td ></td> 
       {% endif %} 

       {% if sc.start.hour == 23 %} 
        <td>{{ sc }}</td> 
       {% else %} 
        <td ></td> 
       {% endif %} 

       {% if sc.start.hour == 0 %} <!-- 9 a.m. --> 
        <td>{{ sc }}</td> 
       {% else %} 
        <td></td> 
       {% endif %} 

       {% if sc.start.hour == 1 %} 
        <td>{{ sc }}</td> 
       {% else %} 
        <td></td> 
       {% endif %} 
      {% endif %} 
      {% endfor %} 


    </tr> 
    {% endfor %} <!-- tr repetition as trainers number--> 

</tbody> 

</table> 

问题

回答

0

如果你把逻辑视图,而不是的模板,很容易就像你想要的那样设置桌子。

[编辑,以使用OrderedDict维护培训人员的顺序。]

views.py

def staff_daily_schedule_search(request): 

    Fitness_list = User.objects.filter(groups__name='Fitness') # Fitness Trainers 

    search_date1 = request.GET.get('search_date','') 
    search_date= datetime.datetime.strptime(search_date1, '%Y-%m-%d') #%T = %H:%M:%S '%Y-%m-%d' 

    trainer_dict = OrderedDict() 
    # Initialize each row of the table with the trainer's name and a blank schedule 
    for f in Fitness_list: 
     trainer_dict[str(f.id)] = {'name': f.get_full_name(), 'hour_21': '', 
      'hour_22': '', 'hour_23': '', 'hour_0': '', 'hour_1': ''} 

    schedules = Schedule.objects.filter(start__year=search_date.year).filter(start__month = 
     search_date.month).filter(start__day = search_date.day) 
    # Insert each schedule item into the appropriate table row and cell 
    for sc in schedules: 
     trainer_dict[str(sc.Trainer.id)]['hour_' + str(sc.start.hour)] = sc.title 

    context = { 
     'search_date' : search_date1 if search_date1 else datetime.date.today(), 
     'trainer_dict': trainer_dict 
    } 
    return render(request, 'management/staff_daily_schedule.html', context) 

staff_daily_schedule.html

<form action="{% url 'management:staff_daily_schedule_search' %}" method="GET"> 
    <span><input type="date" class="search_date my-control" name="search_date" value="{{ search_date }}" ></span> 
      <a id="today" class="btn btn-warning">오늘</a> 
    <button class="btn btn-info" value="검색" >검색</button> 
</form> 
<table class="table table-bordered"> 
<thead> 
    <tr> 
     <th></th> 
     <th>06:00 </th> 
     <th>07:00 ~ 07:50</th> 
     <th>08:00 ~ 08:50</th> 
     <th>09:00 ~ 09:50</th> 
     <th>10:00 ~ 10:50</th> 
    </tr> 
</thead> 
<tbody> 
    {% for trainer in trainer_dict %} 
     <tr> 
      <td>{{ trainer.name }}</td> 
      <td>{{ trainer.hour_21 }}</td> 
      <td>{{ trainer.hour_22 }}</td> 
      <td>{{ trainer.hour_23 }}</td> 
      <td>{{ trainer.hour_0 }}</td> 
      <td>{{ trainer.hour_1 }}</td> 
     </tr> 
    {% endfor %} 
</tbody> 

</table> 
+0

谢谢!但是,trainer.hour_21有一个错误:'str'对象没有属性'hour_21'。所以,{{trainer.hour_21,22 ..}}什么都不显示。 – Julia

+0

在视图中添加'trainer_dict = trainer_dict.items()'并更改模板中的'{{training for trainer,training in trainer_dict%}'{{training.hour_21}}'解决了问题。非常感谢你,常青树! – Julia