Reputation: 301
I know this has probably been asked a million times, but I searched forever and couldn't find an answer. I'm trying to create several select drop downs to use as a search filter on my index page. I have loaded the models with data, but when I try to render the data on the template, I'm not seeing what is in the model. Here is my code:
views.py
from django.views.generic import TemplateView
from .models import LengthRange, Hull
class IndexView(TemplateView):
template_name = 'index.html'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['length_ranges'] = LengthRange.objects.all()
context['hull_types'] = Hull.objects.all()
return context
urls.py
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name='index.html'), name='index'),
]
and my snippet from index.html:
<h2 class="about-item__text">
A boat with a length of
<select>
<option value="*" selected>any size</option>
{% for length in length_ranges %}
<option value="{{ length.pk }}">{{ length.range }}</option>
{% endfor %}
</select>
, with hull type of
<select>
<option value="*" selected>any</option>
{% for hull in hull_types %}
<option value="{{ hull.pk }}">{{ hull.type }}</option>
{% endfor %}
</select>
It's been a really long time since I worked in Django, but this should have been relatively easy. What am I missing here?
Upvotes: 2
Views: 1885
Reputation: 309089
You are using TemplateView
in your URL pattern - you should import your view and use that instead.
from myapp.views import IndexView
urlpatterns = [
path('', IndexView.as_view(), name='index'),
]
Upvotes: 2