8π
DateTime fields will always store also seconds; however, you can easily tell the template to just show the hours and minute, with the time
filter:
{{ value|time:"H:M" }}
where βvalueβ is the variable containing the datetime field.
Of course, you can also resort to other tricks, like cutting out the seconds from the field while saving; it would require just a small change to the code in the view handling the form, to do something like this:
if form.is_valid():
instance = form.save(commit=False)
instance.nosecs = instance.nosecs.strptime(instance.nosecs.strftime("%H:%M"), "%H:%M")
instance.save()
(note: this is an ugly and untested code, just to give the idea!)
Finally, you should note that the admin will still display the seconds in the field.
It should not be a big concern, though, because admin should be only used by a kind of users that can be instructed not to use that part of the field.
In case you want to patch also the admin, you can still assign your own widget to the form, and thus having the admin using it. Of course, this would mean a significant additional effort.
11π
Django widget can be used to achieve this easily.
from django import forms
class timeSlotForm(forms.Form):
from_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
- Static files won't load when out of debug in Django
- How to Hash Django user password in Django Rest Framework?
- Django: default language i18n
- Django makemigrations not detecting project/apps/myapp
- Best practice when using folium on django
5π
So I think the proposed and accepted solution is not optimal because with:
datetime.widget = forms.SplitDateTimeWidget(time_format=('%H:%M'))
For a SplitDateTimeField in my case but for you only change it to TimeWidget.
Hope it helps other people too.
- Django App Not Showing up in Admin Interface
- Python classes that refer to each other
- Hosting my Django site
2π
TimeField model
in Template
Is displayed
{{ value|time:"H:i" }}
Is not displayed
{{ value|time:"H:M" }}
Django 1.4.1
- Multiple USERNAME_FIELD in django user model
- Django ManagementForm data is missing or has been tampered with
2π
For a ModelForm
, you can easily add a widget like this, to avoid the seconds being shown (just show hh:mm):
class MyCreateForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('time_in', 'time_out', )
widgets = {
'time_in': forms.TimeInput(format='%H:%M'),
'time_out': forms.TimeInput(format='%H:%M'),
}
- How can I programmatically add content to a Wagtail StreamField?
- Django β Access ForeignKey value without hitting database
- Datetime Field Received a Naive Datetime
- Ruby HAML with Django?
2π
You can at least modify the output in the __str__
method on the model by using datetime.time.isoformat(timespec='minutes')
, like this:
def __str__(self):
return self.value.isoformat(timespec='minutes')
Now the value is showing as HH:MM in admin pages.
- How to disable request logging in Django and uWSGI?
- Django: Best Way to Add Javascript to Custom Widgets
- How to compare version string ("x.y.z") in MySQL?
0π
On Django 1.9 the following format should work:
{{ yourData.value|time:"H:i" }}
Django has a whole set of template tags and filters.
Django 1.9 documentation on this is:
https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#time
- Celery workers unable to connect to redis on docker instances
- Stop nosetests from printing logging information?
- How do you get Django to make a RESTful call?
- How to set initial values for a ModelForm when instance is also given
- Unit tests fail after a Django upgrade