46👍
The problem turned out to be that I needed both the ISO and UK date formats in the settings.py
file like so:
DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')
and then the forms.py adjusted to the following:
class ClientDetailsForm(ModelForm):
date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
class Meta:
model = ClientDetails
Why USE_L10N = True
can’t just do this I don’t know!
23👍
DATE_INPUT_FORMATS
in settings.py has no effect with USE_I18N = True
. This is because django will load specific format for active locale. Django ships with format definitions for many different locales.
You can override django default format definitions:
mysite/
formats/
__init__.py
en/
__init__.py
formats.py
As described in django documentation: Creating custom formats file
- [Django]-Best way to make Django's login_required the default
- [Django]-Django: Using F arguments in datetime.timedelta inside a query
- [Django]-How do you convert a PIL `Image` to a Django `File`?
13👍
In Sevenearths answer there’s the comment
Why
USE_L10N = True
can’t just do this I don’t know!
The documentation says that USE_L10N = True
is the default setting, but to get UK dates to work the LANGUAGE_CODE
must also be changed from its default setting of en-us
to en-GB
.
Once changed, date fields automatically accept the formats “dd/mm/yyyy” and “yyyy-mm-dd” (and possibly others, I’ve not tested them all) without needing to set input_formats = settings.DATE_INPUT_FORMATS
in every form date field.
- [Django]-How to print line breaks in Python Django template
- [Django]-Django static files versioning
- [Django]-QuerySet, Object has no attribute id – Django
4👍
I used below code that worked on Both Add and Edit Form Case.
in settings.py
DATE_INPUT_FORMATS = ('%d/%m/%Y','%d-%m-%Y','%Y-%m-%d')
in forms.py
class MyModelForm(forms.ModelForm):
issue_date = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), input_formats=settings.DATE_INPUT_FORMATS)
- [Django]-Converting Django QuerySet to pandas DataFrame
- [Django]-Is virtualenv recommended for django production server?
- [Django]-Django – get HTML output into a variable
3👍
This might not be the answer to the exact question, but since I got here… I would suggest using a date widget, and let Django save the date the way it understands it. Define the field in your form like so:
import datetime
from django import forms
def last_years():
first_year = datetime.datetime.now().year - 6
return list(range(first_year + 7, first_year, -1))
class MyForm(forms.Form):
# This is it... it's user friendly, and can't go wrong parsing it
date = forms.DateField(widget=forms.SelectDateWidget(years = last_years()))
If you got it into a date field of the Django Model, then the only problem left would be format the output representation. Am I right?
- [Django]-Django.db.utils.OperationalError: fe_sendauth: no password supplied
- [Django]-OSError: [Errno 18] Invalid cross-device link
- [Django]-Django filter events occurring today
2👍
This works for me
date_of_birth = forms.DateField(
localize=True,
widget=forms.DateInput(format = '%Y-%m-%d',attrs={'type': 'date'}),
)
- [Django]-Django – view sql query without publishing migrations
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-Django get list of models in application
1👍
You need to set USE_L10N False
as shown below to make DATE_INPUT_FORMATS work to change the date format of DateTimeField() and DateField(). *USE_L10N
is prior to DATE_INPUT_FORMATS
so you need to set USE_L10N
False
:
DATE_INPUT_FORMATS = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = False # Here
The doc says below in DATE_INPUT_FORMATS:
When
USE_L10N
isTrue
, the locale-dictated format has higher precedence and will be applied instead.
- [Django]-Can one use the Django database layer outside of Django?
- [Django]-Django object multiple exclude()
- [Django]-How do I go straight to template, in Django's urls.py?
0👍
I would overwrite the DateTimeField put my our logic. It doesn’t matter how postgresql is storing it, you can format it how ever you want.
Docs:
http://docs.djangoproject.com/en/dev/ref/models/instances/
- [Django]-Django Model Fields Indexing
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
- [Django]-How do I save multiple django models in a single transaction?