简体   繁体   English

如何显示Django中所有用户的日程安排?

[英]How can I show daily schedules of all users in Django?

I'm using Django to make a website for gym trainers. 我正在使用Django创建健身教练网站。 what I want is a template for the daily schedules of all trainers like this 我想要的是这样的所有培训师的日程安排模板

But, my page is 但是,我的页面是 在此处输入图片说明

The problem is the 'td' of per trainer's tr repeats as many as the number of schedules the trainer has. 问题是每位培训师的tr的“ td”重复次数与培训师的日程安排次数一样多。 I know the {% for sc in schedules %} is the problem. 我知道{% for sc in schedules %}是问题所在。 But, because schedules are the query set, I should use the for and while using for, I should check the right time to insert the schedule to the right tr, td position. 但是,因为日程表是查询集,所以我应该使用for,在使用for时,我应该检查正确的时间以将日程表插入正确的tr,td位置。 How can I make the successful table to show the daily schedules of all users(trainers)?? 如何制作成功的表格来显示所有用户(培训师)的日程安排? Anybody will be very helpful to me. 任何人都会对我有很大的帮助。

Schedule.model 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)

my views.py 我的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 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>

The problem 问题

If you put the logic in your view instead of the template, it's easy to set up the table exactly like you want it. 如果将逻辑放在视图中而不是模板中,则可以很容易地完全按照需要设置表。

[Edited to use an OrderedDict to preserve the order of trainers.] [编辑以使用OrderedDict来保留培训师的顺序。]

views.py 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示我关注的所有用户? - How can I show all users I follow? 如何通过 django 将特定用户数据显示到串行器中? - How can I show specific users data into the serializer through django? Python:Django:我如何找到用户的纬度和经度,以便我可以显示用户位置? - Python: Django: How can i find the users Lattitude and Longitude so that i can show the users location? 在 Django 中创建时,如何将模型分配给所有用户? - How can I assign a model to all users upon creation in Django? 如何将一些(但不是全部)模型与使用 Django 的用户联系起来? - How can I relate some, but not all, models to users using Django? 我怎样才能在template,django中获取所有用户图像 - How can I get all users images in template,django 如何创建一个脚本,将Django用户的所有电子邮件地址打印到一个小的.txt文件中? - How can I create a script where it will print all email addresses of my Django users into a small .txt file? Django:如何获取所有员工的查询集? - Django: how do I get a queryset of all users that are staff? 如何在pycharm中显示django版本? - How can I show the django version in pycharm? 如何纠正我的ORM语句以显示未与Django中的用户关联的所有朋友? - How can I correct my ORM statement to show all friends not associated with a user in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM