2017-08-11 60 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

相關問題