[Fixed]-Display time stored in database in HTML (django)

1👍

You can create a custom template tag do this for you and a method on the model.

I haven’t tested this code, but this should work with or without small adjustments

Add this method to the Frequency model:

def get_day_info(self, date):
        # Where date_string would be a datestring
        date = datetime.strptime(date_string, '%Y-%m-%d')
        day = calendar.day_name[date.weekday()]
        info_time = getattr(self, '{}'.format(day))
        return info_time

And register this as a template tag.

@register.simple_tag
def get_day_info(info, date):
    return info.get_day_info(date)

You would call it in the template like this:

<li><b>Time -</b> {% get_day_info info {{ date }} %}</li>

Leave a comment