【发布时间】:2019-08-11 11:11:25
【问题描述】:
我知道这可能已经被问过一百万次了,但我一直在寻找却找不到答案。我正在尝试创建几个选择下拉菜单以用作我的索引页面上的搜索过滤器。我已经用数据加载了模型,但是当我尝试在模板上呈现数据时,我没有看到模型中的内容。这是我的代码:
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'),
]
还有来自 index.html 的我的 sn-p:
<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>
自从我在 Django 工作以来已经很长时间了,但这应该相对容易。我在这里错过了什么?
【问题讨论】:
标签: python django django-templates django-views