# Changing input type in a class based create view

**URL:** <https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334>\
**Category:** Templates & Frontend\
**Created:** [May 9, 2020, 10:36am UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334 "2020-05-09T10:36:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathanacres](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jonathanacres/32/1237_2.png) [@jonathanacres](https://forum.djangoproject.com/u/jonathanacres)\
**Post date:** [May 9, 2020, 10:36am UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/1 "2020-05-09T10:36:35Z")

</div>

Hi folks

I’ve created a widget to change date input type to a datepicker:

```
from django import forms

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

```

I can use this fine with models I define in forms.py but I was wondering if there’s a way to override the input type in a class based view in views.py. Specifically, CreateView:

```
class ActionCreate(LoginRequiredMixin, CreateView):
    model = Action
    fields = ' __all__'

```

Any help, much appreciated.  
Thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 9, 2020, 12:18pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/2 "2020-05-09T12:18:14Z")

</div>

The widget used in a form is an attribute of the form, not of the view that is presenting that form.

So what you’re looking for is the [form\_class](https://docs.djangoproject.com/en/3.0/topics/class-based-views/generic-editing/#model-forms) attribute of your view that will use your custom form.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 9, 2020, 1:21pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/3 "2020-05-09T13:21:07Z")

</div>

Yes as Ken says. Defining `model` and `fields` on a `FormView` are shortcuts meaning “create a Form class for me like this.” For any level of customization you need to create the form class yourself.

---

<div class="post-metadata">

**Author:** ![jonathanacres](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jonathanacres/32/1237_2.png) [@jonathanacres](https://forum.djangoproject.com/u/jonathanacres)\
**Post date:** [May 9, 2020, 1:44pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/4 "2020-05-09T13:44:15Z")

</div>

Great, thanks guys. I’ll give that a crack.

---

<div class="post-metadata">

**Author:** ![Seb-Jesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/seb-jesh/32/7263_2.png) [@Seb-Jesh](https://forum.djangoproject.com/u/Seb-Jesh)\
**Post date:** [January 30, 2022, 4:43pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/5 "2022-01-30T16:43:51Z")

</div>

Did you find a solution to this? Override Date Input in Create View?

---

<div class="post-metadata">

**Author:** ![mscheper](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mscheper/32/26256_2.png) [@mscheper](https://forum.djangoproject.com/u/mscheper)\
**Post date:** [June 26, 2025, 10:14pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/6 "2025-06-26T22:14:19Z")

</div>

@adamchainz: Sorry for necromancing, but I wanted to check whether this is still the best way. I have quite a few dates in my models, and `<input type="date">` actually works quite well in mobile browsers, and it’d be nice not to have to add so much boilerplate to every form for a model with `DateField`s.

I had a look at the code for `BaseModelForm`, to try to find how it determines which widget to use, and see if I can make a custom version of that, but didn’t have any joy.

I also noticed that `django/forms/templates/django/forms/widgets/date.html` simply includes `django/forms/widgets/input.html`, so I tried [overriding it](https://docs.djangoproject.com/en/5.2/howto/overriding-templates/#overriding-from-the-project-s-templates-directory), by copying `input.html` to `django/forms/widgets/date.html` under my app’s templates directory, and hardcoding the `type` attribute. But that didn’t work, either, presumably because something as core Djang as forms doesn’t look for templates in the same way.

Am I on the right track with either of these, or is there another way? Or will I just have to spoon-feed my custom widget to each date field by hand?

Thanks!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2025, 4:25am UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/7 "2025-06-27T04:25:55Z")

</div>

Overriding form widgets isn’t quite the same as overriding page templates.

In addition to creating your new widget template, you also should use the `TemplatesSetting` renderer.

See: [Overriding built-in widget templates](https://docs.djangoproject.com/en/5.1/ref/forms/renderers/#overriding-built-in-widget-templates) and [TemplatesSetting](https://docs.djangoproject.com/en/5.1/ref/forms/renderers/#templatessetting)

You might also want to read the thread at [Overriding Widgets templates not working (following help), template loader not searching app dirs](https://forum.djangoproject.com/t/overriding-widgets-templates-not-working-following-help-template-loader-not-searching-app-dirs/12974)

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [June 27, 2025, 1:45pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/8 "2025-06-27T13:45:28Z")

</div>

I don’t think you need to touch templates. You should be able to make a custom base form class that always uses the custom widget, using `formfield_callback` per [Overriding the default fields](https://docs.djangoproject.com/en/5.2/topics/forms/modelforms/#overriding-the-default-fields). Something like the following (untested):

```python
from django import forms
from django.db import models

from example.models import NicheMuseum

class DateInput(forms.DateInput):
    """
    Subclass of Django’s DateInput widget to use <input type=date>.
    """
    input_type = "date"

def customize_fields(db_field, **kwargs):
    if isinstance(db_field, (models.DateField, models.DateTimeField)):
        kwargs["widget"] = DateInput
    return db_field.formfield(**kwargs)

class NicheMuseumForm(forms.ModelForm):
    class Meta:
        model = NicheMuseum
        fields = ["name", "created_at"]
        formfield_callback = urlfields_assume_https

```

---

<div class="post-metadata">

**Author:** ![mscheper](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mscheper/32/26256_2.png) [@mscheper](https://forum.djangoproject.com/u/mscheper)\
**Post date:** [June 27, 2025, 4:28pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/9 "2025-06-27T16:28:33Z")

</div>

Beautiful, that worked! Thank you! I just had to set a different input type for `DateTimeField`s (and set `formfield_callback` in my form class to `customise_fields`, not ‘urlfields\_assume\_https’ 🙂):

```python
class DateInput(forms.DateInput):
    """Django's DateInput widget with type="date"."""

    input_type = "date"

class DateTimeInput(forms.DateTimeInput):
    """Django's DateTimeInput widget with type="datetime-local"."""

    input_type = "datetime-local"

def customise_fields(db_field, **kwargs):
    """Set date and date/time widgets to `DateInput` or `DateTimeInput`."""
    if isinstance(db_field, models.DateField):
        kwargs["widget"] = DateInput
    if isinstance(db_field, models.DateTimeField):
        kwargs["widget"] = DateTimeInput
    return db_field.formfield(**kwargs)

```

It’s no bother to set `formfield_callback` in each form’s `Meta` class, but for the sake of completing my understanding, I’m curious: The following, unsurprisingly, doesn’t work—the `formfield_callback` doesn’t get set in the subclass’s `Meta`—but is it possible to write `CustomModelForm` so it would?

```python
class CustomModelForm(forms.ModelForm):

    class Meta:
        formfield_callback = customise_fields

class NicheMuseumForm(CustomModelForm):

    class Meta: # This doesn't inherit CustomModelForm.Meta
        model = NicheMuseum
        fields = ["name", "created_at"]

```

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [June 27, 2025, 9:12pm UTC](https://forum.djangoproject.com/t/changing-input-type-in-a-class-based-create-view/2334/10 "2025-06-27T21:12:59Z")

</div>

> [@mscheper](#):
>
> Beautiful, that worked! Thank you!

Glad to help. I may adapt this into a blog post.

> [@mscheper](#):
>
> (and set `formfield_callback` in my form class to `customise_fields`, not ‘urlfields\_assume\_https’ 🙂

oh yeah, sorry, I adapted from my example over at [http://localhost:8080/tech/2023/12/07/django-fix-urlfield-assume-scheme-warnings/#modelform-classes-with-formfield-callback](http://localhost:8080/tech/2023/12/07/django-fix-urlfield-assume-scheme-warnings/#modelform-classes-with-formfield-callback) 🙂

> [@mscheper](#):
>
> It’s no bother to set `formfield_callback` in each form’s `Meta` class, but for the sake of completing my understanding, I’m curious: The following, unsurprisingly, doesn’t work—the `formfield_callback` doesn’t get set in the subclass’s `Meta`—but is it possible to write `CustomModelForm` so it would?

Yeah, it’s inconsistent with models - to inherit when subclassing, you need to inherit the `Meta` too, per [these docs](https://docs.djangoproject.com/en/5.2/topics/forms/modelforms/#form-inheritance):

```auto
class CustomModelForm(forms.ModelForm):
    class Meta:
        formfield_callback = customise_fields

class NicheMuseumForm(CustomModelForm):
    class Meta(CustomModelForm.Meta):
        model = NicheMuseum
        fields = ["name", "created_at"]

```

You could also hack automatic setting of attributes with ` __init_subclass__ `, but that’s probably not worth it.
