Borja
Borja

Reputation: 91

Change DateTime input format in django form

I am trying to change the DateTime format in one of my django forms.

The format I would like it to accept is: day month year hour_minute_seconds timezone 11 Aug 2015 12:00:00 GMT -> %d %b %Y %H:%M:%S %Z

I have been doing some tests but I have not been able to get the right way.

I have disabled the L10N in the settings.py file so I can use the %b month format.

This is my forms.py file content

from django import forms
from django.forms import DateTimeField

from web.apps.customer.models import Reported_VPN_User

class Customer_settings_vpn_Form(forms.ModelForm):

    event_date = DateTimeField(widget=forms.widgets.DateTimeInput(format="%d %b %Y %H:%M:%S %Z"))

    class Meta:
        model = Reported_VPN_User
        fields = '__all__'

When I try to input a date in the required format, the form does not allow me to go ahead.

I have been checking the DATETIME_INPUT_FORMAT (https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-DATETIME_FORMAT) and I dont see what I am looking for, but checking the python date documentation (https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior) I see that it should be possible.

How can I modify the datetime format so I can make it work as requested?

ps: I am not an expert in django.

Upvotes: 8

Views: 28858

Answers (1)

mcastle
mcastle

Reputation: 2982

Don't use format. Use input_formats instead, which accepts a list of formats:

event_date = DateTimeField(input_formats=["%d %b %Y %H:%M:%S %Z"])

Upvotes: 13

Related Questions