0
votes

In my creation template, the date field is rendered as a CharField and does not allow to select a date, while I need this validation date.

My Model includes a DateField :

class Interview(models.Model):
     date_planned = models.DateField(verbose_name="Planned Date", blank=True, null=True)

My View calls the ModelForm quite normally:

class InterviewCreateView(UserAccessMixin, CreateView):
    permission_required = "interviews.add_interview"
    model = Interview
    form_class = InterviewForm
    fields = ['date_planned', 'topics']
    template_name = "interviews/interview_create_form.html"
    success_url = reverse_lazy("interviews:interviews_list")
    ```
 My Form just calls the Meta and includes a widget:

class InterviewForm(ModelForm): def init(self, *args, **kwargs): super().init(*args, **kwargs) self.fields['date_planned'].widget.attrs.update({'size': 40, 'type': 'date'})

class Meta:
    model = Interview
    fields=['topics', 'date_planned']```

Indeed the size of the input fields goes up to 40, however the date format is ignored by the system and lets my date_planned field being displayed as a CharField

2
What is your question?Code-Apprentice
My question was that the DateInput was simply ignored when dealing with the form and in the template I had only a field from type "text" and not "date". Dosbodoke's answer works great.Fabrice Jaouën
I'm glad that you got the help you needed. For future reference, you should ask a direct question to get the help you need. Your comment "My question was..." still doesn't actually ask a question.Code-Apprentice
Thank you for the hint !Fabrice Jaouën

2 Answers

1
votes

In you forms.py you can create a class

class DateInput(forms.DateInput):
    input_type = 'date'

class Meta:
   model = Interview
   fields = ['topics', 'date_planned']
   widgets = {'date_planned': DateInput(),}

that should work, unless for the DatePicker problem.

0
votes

Thank Dosbodoke the complete answer should look like this:

class DateInput(forms.DateInput):
    input_type = "date"

class InterviewForm(ModelForm):
    class Meta:
        model = Interview
        fields = ['topics', 'date_planned']
        widgets = {'date_planned': DateInput(),}