DateTime widget in default CreateView form

666 views
Skip to first unread message

Anil Felipe Duggirala

unread,
Oct 7, 2021, 6:01:34 PM10/7/21
to django...@googlegroups.com
hello,
I have tried to find an answer to this question online, with no clear response.
I have created a simple class based CreateView for a model that has a DateTime attribute.
Simply rendering the form using the {{ form.as_p }} tag in the template associated to the aforementioned view does not give me a DateTime widget in the form.

What is the easiest way or most standard way to achieve this?
I have read something about ModelForm, should I use that? Would that automatically give me a widget in the rendered HTML?

I would also like to know, how would I render that form using crispy forms?

Thanks very much for your help.


sum abiut

unread,
Oct 7, 2021, 6:51:37 PM10/7/21
to django...@googlegroups.com
You can use modelform.

in your form.py you can do something like this

# setup date picker start
class DateInput(forms.DateInput):
input_type = 'date'

class Formname(forms.ModelForm):
         class Meta:
                         model= yourmodel
                         fields = ('datefield')
                         widgets ={'datefield': DateInput() }






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c101be88-2409-4a12-b97b-1af7aa30048c%40www.fastmail.com.


--


Anil Felipe Duggirala

unread,
Oct 7, 2021, 10:14:10 PM10/7/21
to django...@googlegroups.com
On Thu, Oct 7, 2021, at 5:50 PM, sum abiut wrote:
> You can use modelform.
>
> in your form.py you can do something like this
>
> # setup date picker start
> class DateInput(forms.DateInput):
> input_type = 'date'
>
> class Formname(forms.ModelForm):
> class Meta:
> model= yourmodel
> fields = ('datefield')
> widgets ={'datefield': DateInput() }

Thank you for the quick response Sum. I am still unsure about the usage of ModelForm, do I need to specify somewhere that the CreateView should use the ModelForm I define instead of the default form?
Where am I supposed to declare the 'class Formname...' ? (Is this supposed to be done in the models.py file or in the forms.py file?)
Previously you meant the forms.py file right ? (not 'form.py')
The fields list is supposed to be in [] and not in () right?

Why doesn't the default form, that is automatically generated from the model when one uses the CreateView class based view , include the correct widgets?

thanks again,

MR INDIA

unread,
Oct 8, 2021, 9:41:24 AM10/8/21
to Django users

Anil Felipe Duggirala

unread,
Oct 8, 2021, 11:49:46 AM10/8/21
to django...@googlegroups.com
On Fri, Oct 8, 2021, at 8:41 AM, MR INDIA wrote:
> Answer to this query on stack overflow
> <https://stackoverflow.com/questions/27321692/override-a-django-generic-class-based-view-widget>
Thanks very much for the link MR INDIA.
That is exactly what Im looking to do, however, its not working for me for some reason.
How can I check if I am getting the desired result in my rendered HTML?
I am working with a DateTimeField. So I should be getting a type='datetime' in the <input> tag correct?

I don't understand why other fields like a CharField with choices, automatically, renders a <select> tag on my form, without even specifying a widget for it as described in the link. Is it not expected that a DateTimeField would render the type:'datetime' in the html form? (without any additional modifications). The link above was created 6 years ago, has this not changed in all that time?

I will show you what I have:

--models.py--
class Lesson(models.Model):
...
time = models.DateTimeField("Fecha y Hora")

--views.py--
class LessonCreateView(CreateView):
model = Lesson
template_name = 'new_lesson.html'
form_class = LessonForm

def form_valid(self, form): # new
form.instance.instructor = self.request.user
return super().form_valid(form)

--forms.py--
class LessonForm(forms.ModelForm):

class Meta:
model = Lesson
fields = ['title', 'time', 'nivel']
widgets = {
'time': forms.DateTimeInput()
}

In my rendered HTML I however get a type="text" for the 'time' field.

Iyanuoluwa Loko

unread,
Oct 8, 2021, 11:59:02 AM10/8/21
to django...@googlegroups.com
The choice is not a widget per se. The date-time in the model will accept a date time input, however, the calendar icon is a widget, and doesn't come automatically.
The date-time has to accept a date, and if you input the date in the textbox in the accepted django format, it will work fine. To have the calendar icon, it is a widget, and you ycan call it by editing the HTML input type, or adding the widget to the views.py

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Anil Felipe Duggirala

unread,
Oct 8, 2021, 12:09:38 PM10/8/21
to django...@googlegroups.com
On Fri, Oct 8, 2021, at 10:57 AM, Iyanuoluwa Loko wrote:
> The date-time has to accept a date, and if you input the date in the
> textbox in the accepted django format, it will work fine. To have the
> calendar icon, it is a widget, and you ycan call it by editing the HTML
> input type, or adding the widget to the views.py

Thanks very much for that clarification Iyanuoluwa. Could you tell me how exactly I would add that 'Calendar' widget to my views.py (or my forms.py?)?

thank you.
Reply all
Reply to author
Forward
0 new messages