'change datetime format using tempus_dominus DateTimePicker
I installed and imported DateTimePicker and used it as a widget to a Django DateTime Field
When clicking on the field it shows me the date and time in format 07/30/2020 4:33 PM.
However, the model is not accepting the input because it needs to be in format : 2020-07-30 16:33. Any ideea how to manipulate the form widget to show this format?
forms.py:
from django import forms
from .models import BlogPost
from tempus_dominus.widgets import DatePicker, TimePicker, DateTimePicker
class BlogPostModelForm(forms.ModelForm):
publish_date = forms.DateTimeField(
widget=DateTimePicker(
options={
'useCurrent': True,
'collapse': False,
# Calendar and time widget formatting
'time': 'fa fa-clock-o',
'date': 'fa fa-calendar',
'up': 'fa fa-arrow-up',
'down': 'fa fa-arrow-down',
'previous': 'fa fa-chevron-left',
'next': 'fa fa-chevron-right',
'today': 'fa fa-calendar-check-o',
'clear': 'fa fa-delete',
'close': 'fa fa-times'
},
attrs={
'append': 'fa fa-calendar',
'icon_toggle': True,
}
)
)
class Meta:
model = BlogPost
fields = ['title','image', 'slug', 'content', 'publish_date']
def clean_title(self, *args, **kwargs):
instance = self.instance
print('instance is: ',instance)
title = self.cleaned_data.get('title')
qs = BlogPost.objects.filter(title__iexact=title)
if instance is not None:
qs = qs.exclude(pk=instance.pk) # id=instance.id
if qs.exists():
raise forms.ValidationError("This title has already been used. Please try again.")
return title
forms.html:
<!doctype html>
{% extends "blog/base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block head_title %}
{{title}}
{% endblock %}
{% block content %}
{% if title %}
<h1>{{ title }}</h1>
{% endif %}
<form method='POST' enctype="multipart/form-data" action='.'> {% csrf_token %}
{{ form|crispy }}
{{ form.media }}
<button type='submit'>Send</button>
</form>
{% endblock %}
Solution 1:[1]
try to add: "format": "YYYY-MM-DD HH:mm" in the options.
Solution 2:[2]
There are two settings parameters for date and datetime. So in your case TEMPUS_DOMINUS_DATETIME_FORMAT shoud be somethins like:
TEMPUS_DOMINUS_DATETIME_FORMAT = 'YYYY-MM-DD HH:mm'
TEMPUS_DOMINUS_DATE_FORMAT = 'YYYY-MM-DD'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | django_mango |
Solution 2 | Artem Skliar |