Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeField format in Django template

I'm writing an application in Django and using several TimeFields in a model. I only want to work with the time format hh:mm (not hh:mm:ss or hh p.m./a.m. or other things). I have set the django setting TIME_FORMAT = 'H:i' and USE_L10N = False according to the documentation,

USE_L10N overrides TIME_FORMAT if set to True

In my template I'm using input boxes automatically created by Django like:

{{formName.timeField}}

The problem is that as long as I introduce something like "10:00" and do a POST, when the POST is done Django (or whatever) turns it into "10:00:00" (so it adds the seconds).

  • Is there a way to specify the format in which datetime fields display saved dates? If I could just use something like |date:"H:i" in the value of the input box that would do the trick.

On the other side I know I could do the input box manually (not directly using {{formName.timeField}}), but I've had some problems in the past with that so I would like to avoid that (at least for the moment).

There is also this similar question in Django TimeField Model without seconds, but the solution does not work (it gives me 'NoneType' object has no attribute 'strptime')

like image 957
user989863 Avatar asked Oct 11 '11 16:10

user989863


People also ask

Is there a TimeField in Django?

TimeField is a time field which stores time, represented in Python by a datetime. time instance. As the name suggests, this field is used to store an object of datetime created in python.

How do I change the date format in Django?

Python django format date dd/mm/yyyy And, because the format is dd/mm/yyyy, we must use a slash to separate the date, month and year. To separate the day from the month and the month from the year in a date notation we use DateSeparator.

What is Forloop counter in Django?

Django for loop counter All the variables related to the counter are listed below. forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.

What are template tags in Django?

Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.


2 Answers

I had exactly this problem in my app, and solved it by passing a format parameter to Django's TimeInput widget:

from django import forms

class SettingsForm(forms.Form):
    delivery_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
like image 163
dustyneuron Avatar answered Sep 23 '22 17:09

dustyneuron


Assuming you have a format you want all TimeFields to use, you can add the TIME_INPUT_FORMATS to your settings file. For example, TIME_INPUT_FORMATS = ('%I:%M %p',) will format your times as "5:30 PM".

The docs: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TIME_INPUT_FORMATS

like image 11
Josh Avatar answered Sep 22 '22 17:09

Josh