retro_coder
retro_coder

Reputation: 456

How to display Django model data in HTML table?

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

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>

However, within my views.py, I am only accessing the latest start and stop times in the HTML table. But, my aim is to display all the time logs for each user.

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?

Thank you! :)

Upvotes: 1

Views: 2398

Answers (2)

Niladry Kar
Niladry Kar

Reputation: 1203

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

Upvotes: 1

ger.s.brett
ger.s.brett

Reputation: 3308

In my opinion you need to restructure your models :

class Timeentry(models.Model):
    user_id= models.ForeignKey(User, on_delete = models.CASCADE)
    start_time = models.DateTimeField()
    stop_time = models.DateTimeField(null=True, blank=True)

Only this way you will now what end time belongs to what start time - if there is two entries a day (see your example on Dec. 31).

Then you can simply retrieve the data and handle empty values:

for user in data:
    sta_time = Timeentry.objects.filter(user_id = user).valueslist("start_time","stop_time")
    sta_time_corrected=[]
    for sti in sta_time:#build table and replace empty values
        sta_time_corrected.append( [user.first_name]+["No Time" if x=="" else x for x in sti])
    store_data.append(sta_time_corrected)

Upvotes: 0

Related Questions