Django DATE_FORMAT in settings.py ignores formatting or gets overwritten

I am trying to change the format. SelectDateWidget

I followed the steps in this question , including setting USE_L10N

to. False

I also looked at the list of valid format strings and tried several combinations.

The problem is that no matter how I format the string, the "Year" drop is always on the left when I want it on the right. It also always formats up to 4 digits, even if I specify 2. However, I can remove it 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

      

Here are some of the formats I've tried and the result. 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 do 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)

      

+3


source to share


1 answer


SelectDateWidget

will use DATE_FORMAT

, but only for about a year, months and days, nothing more. In the code of the widget you can see that it does not 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 work, but appears in reverse order. j N, Y

should give you the opposite of what you get. So, one thing you should try is to put yours DATE_FORMAT

in reverse order. For example, for example:

DATE_FORMAT = 'YNj'

      

Speaking of which, it might be worth taking a look at your HTML, perhaps something in the style that is causing this. A float: right

somewhere maybe?



As for the two-digit year, you can pass the argument for the years you want to display. It can be a list / tuple. Cm:

It takes one optional argument:

years old

An optional list / tuple of years to use in the selection "year" 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)

      

+1


source







All Articles