Search code examples
pythondjangohtmldjango-formsdjango-widget

Django SelectDateWidget not rendering correctly


I am trying to render the built-in SelectDateWidget in Django, however in the webpage it shows as

<django.forms.widgets.SelectDateWidget object at 0xb4f6bc6c> Submit

I am new to Django, I read the documentation and looked up on the internet, but couldn't came up with a solution.

Here is my,

views.py

from django.shortcuts import render
from django.forms.widgets import SelectDateWidget

def yillik(request):
    form = SelectDateWidget()
    return render(request, 'izinyillik.html', {'form': form})

izinyillik.html

<form method="post">
     {% csrf_token %}
     {{ form }}
     <button type="submit">Submit</button>
</form>

Solution

  • You can't use a widget on its own like that. A widget needs to live in a field, which in turn needs to live on a form. You need to declare them both:

    class MyForm(forms.Form):
        my_field = forms.DateField(widget=SelectDateWidget)