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
overridesTIME_FORMAT
if set toTrue
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).
|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
')
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.
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.
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.
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.
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'))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With