如何在HTML表中显示Django模型数据? [英] How to display Django model data in HTML table?

查看:323
本文介绍了如何在HTML表中显示Django模型数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,即Starttime和Stoptime,分别存储开始时间和停止时间.现在,每个注册用户将具有多个开始时间和结束时间实例,例如:

I have two models i.e Starttime and Stoptime that store the start time and stop time respectively. Right now, each registered user will have multiple instances of the start and stop time, like so:

Name    Start_time                 Stop_time

Bobby   Dec. 31, 2019, 5:39 a.m    Dec. 31, 2019, 5:50 a.m
        Jan. 01, 2020, 9:00 a.m    Jan. 01, 2020, 18:00 a.m
        Jan. 02, 2020, 6:00 a.m    Jan. 02, 2020, 19:00 a.m
        ...                        ...                 

Tina    Dec. 31, 2019, 9:00 a.m    Dec. 31, 2019, 10:00 a.m
        Dec. 31, 2019, 12:00 p.m   Dec. 31, 2019, 15:00 p.m
        Jan. 01, 2020, 9:00 a.m    Jan. 01, 2020, 11:00 a.m
        Jan. 02, 2020, 5:00 a.m    Jan. 02, 2020, 9:00 a.m
        Jan. 02, 2020, 10:00 a.m   Jan. 02, 2020, 12:00 a.m
        ...                        ... 

我想在HTML表中完全像这样显示我的数据.

I want to display my data exactly like this within the HTML table.

models.py:

class Starttime(models.Model):
    user_id= models.ForeignKey(User, on_delete = models.CASCADE)
    start_time = models.DateTimeField()

class Stoptime(models.Model):
    user_id= models.ForeignKey(User, on_delete = models.CASCADE)
    stop_time = models.DateTimeField()

views.py:

#Right now, I am accessing only the latest values from both the models. However, I want to display all of them neatly in my HTML table.
def interface(request):
    data = User.objects.filter(pk__gt=1) #All users apart from the SuperUser admin
    store_data = []
    for user in data:
        sta_time = Starttime.objects.filter(user_id = user)
        sta_data = sta_time.values_list('start_time', flat=True).latest('start_time')

        sto_time = Stoptime.objects.filter(user_id = user)
        sto_data = sto_time.values_list('stop_time', flat=True).latest('stop_time')

        store_data.append((user.first_name, sta_data, sto_data))
    return render(request, 'users/interface.html', {'data': store_data})

interface.html:

<table>
    <tr>
        <th> Name</th>
        <th> Start Time </th> 
        <th> Stop Time </th>
    </tr>
        {% for column in data %}
        <tr>
            <td>{{column.0}}</td>
            <td>{{column.1}}</td>
            <td>{{column.2}}</td>
        </tr>
        {% endfor %}

 </table>

但是,在我的views.py中,我仅访问HTML表中的最新开始时间和停止时间.但是,我的目的是显示每个用户的所有时间日志.

如何在HTML表格的每一列中分别显示所有时间对象,有没有办法检查两个模型的时间数据是否为空?

How can I display all the time objects respectively in each column within the HTML table and is there a way to check if the time data for both my models are empty or not?

谢谢! :)

推荐答案

输入一个键和一个这样的值对

Make a Key and a value pair like this

store_data.append({
 'first_name' : user.first_name, 
 'sta_data': sta_data, 
 'sto_data' : sto_data
})

在模板"中按如下方式使用它:

And In Template use it like this:

{% for column in data %}
    <tr>
        <td>{{column.first_name}}</td>
        <td>{{column.sta_data}}</td>
        <td>{{column.sto_data}}</td>
    </tr>
    {% endfor %}

这篇关于如何在HTML表中显示Django模型数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆