Search code examples
pythondjangoformsvalidationdatefield

__init__() got multiple values for keyword argument 'input_formats'


forms.py

from django import forms

ACCEPTED_FORMATS = ['%d-%m-%Y', '%d.%m.%Y', '%d/%m/%Y']

class LeaveRequestForm(forms.Form):
    start_date = forms.DateField(input_formats=ACCEPTED_FORMATS)
    end_date = forms.DateField(input_formats=ACCEPTED_FORMATS)
    working_days = forms.IntegerField(min_value=1)

I couldn't find any date format that could pass form validation, every time I received 'Enter a valid date.', so when I tried to define some the exception came out:

__init__() got multiple values for keyword argument 'input_formats'

https://docs.djangoproject.com/en/1.8/ref/forms/fields/#datefield

settings.py

...

USE_I18N = True

USE_L10N = True

...

I also tried built-in SelectDateWidget with no luck, every time produced invalid date. Maybe there is some "cache" issue: "Enter a valid date" Error in Django Forms DateField

What should I do to get past this validation checks and move on with proper dates? Are the docs missing something with an error I encountered?


Solution

  • input_formats is not a kwarg, it's the sole option to DateField. Drop the input_formats=

    ACCEPTED_FORMATS = ['%d-%m-%Y', '%d.%m.%Y', '%d/%m/%Y']
    
    class LeaveRequestForm(forms.Form):
        start_date = forms.DateField(ACCEPTED_FORMATS)
        end_date = forms.DateField(ACCEPTED_FORMATS)
        working_days = forms.IntegerField(min_value=1)