Reputation: 4155
I am trying to change the format of SelectDateWidget
I have followed the steps in this question including setting USE_L10N
to False
I have also looked at the list of acceptable format strings and tried multiple combinations.
The issue is that no matter how I format the string the "Year" drop down is always on the left when I want it on the right. It also always formats to 4 digits even if I specify 2. I am however able to remove it altogether by simply not specifying it.
Can anyone tell me how to format the date in "Day : Moth Year"?
I am using Django 1.6.2
settings.py
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Dublin'
USE_I18N = True
USE_L10N = False
USE_TZ = True
These are some of the formats I have tried and the outcome. Month(long) = "January" etc.
DATE_FORMAT = 'j N, Y' # Year(4 digit) : Month(long) : Day
DATE_FORMAT = 'j N, y' # Year(4 digit) : Month(long) : Day
DATE_FORMAT = 'j N' # Month(long) : Day
DATE_FORMAT = 'N j, Y' # Year(4 digit) : Day : Month(long)
DATE_FORMAT = 'N j, y' # Year(4 digit) : Day : Month(long)
DATE_FORMAT = 'N j' # Day : Month(long)
DATE_FORMAT = 'N, j, Y' # Year(4 digit) : Day : Month(long)
DATE_FORMAT = 'd, F, Y' # Year(4 digit) : Month(long) : Day
DATE_FORMAT = 'd, F, y' # Year(4 digit) : Month(long) : Day
I am trying to render birthdate
below which is part of my Person Model created using ModelForm
models.py
class Person(models.Model):
birthdate = models.DateField(null=True, blank=True) #overwritten in forms.py
forms.py
from django.forms import ModelForm
from django.forms.extras import SelectDateWidget
class SurveyFormA(forms.ModelForm):
birthdate = forms.DateField(widget=extras.SelectDateWidget(), required=False)
Upvotes: 4
Views: 1791
Reputation: 17144
SelectDateWidget
will use DATE_FORMAT
, but only for the order of year, months and days, nothing more. You can see in the widget code itself that it doesn't distinguish between y
and Y
or b
and F
, etc.:
elif char in 'Yy':
output.append('year')
#if not self.first_select: self.first_select = 'year'
elif char in 'bEFMmNn':
output.append('month')
#if not self.first_select: self.first_select = 'month'
elif char in 'dj':
output.append('day')
#if not self.first_select: self.first_select = 'day'
But what's strange in your case is that the parsing seems to be working, but is displayed in reverse. j N, Y
should give you the opposite of what you get. So, one thing you should try is put your DATE_FORMAT
in reverse. Like this for example:
DATE_FORMAT = 'YNj'
That being said, you should maybe have a look at your HTML, probably something in the styling that causes this. A float: right
somewhere maybe?
As for the 2 digits year, you can pass an argument for the years you want displayed. This can be a list/tuple. See:
Takes one optional argument:
years
An optional list/tuple of years to use in the “year” select box. The default is a list containing the current year and the next 9 years.
https://docs.djangoproject.com/en/1.6/ref/forms/widgets/
Like this:
birthdate = forms.DateField(widget=extras.SelectDateWidget(years=('15','16','17','18','19')), required=False)
Upvotes: 2