Why won't Django render its select widget with this code

I am trying to use Django's default select widget to render an html dropdown and I just can't seem to figure out what I am doing wrong...

here is my models.py

class VehicleYear(TimeStampedModel):
    oem_year = models.IntegerField(
        unique=True, null=False, blank=False, default=current_year, validators=[
        MinValueValidator(1900), max_value_current_year])

here is my forms.py

class SelectYear(forms.Form):
  vehicle_year = forms.ModelChoiceField(queryset=VehicleYear.objects.all(), label="Select Year")

here is my views.py

def Year(request):
  context = {'form': SelectYear()}
  return render(request, 'vehicles/select_year.html', context)

here is my html template(select_year.html):

{{ form.as_p }}

select_year.html is part of select_filter.html and is using

{% include "folder/select_year.html" %}

Couple of things to check:

  • def Year(request) should be lowercase by convention def year(request)
  • before that line return render(request, 'vehicles/select_year.html', context), put a print(context) statement to check that you are in the correct view
  • in the template where you call {% include "folder/select_year.html" %} is the form set (it should be set via the views' context for that template)?
  • Shouldn't the include be {% include "vehicles/select_year.html" %} ?
Вернуться на верх