gpt4 book ai didi

python - Django 中的 Bootstrap3 选项卡

转载 作者:太空宇宙 更新时间:2023-11-03 18:44:29 25 4
gpt4 key购买 nike

我想实现Bootstrap3 tabs在我的应用程序中,它按州显示学校数据。因此,如果您访问 example.com/ma/,您将看到马萨诸塞州的信息以及按年级排序的选项卡。

我已经使用查询集按州进行过滤,以便在 example.com/ma/上仅显示“ma”结果。我可以在一个选项卡中显示所有数据,但无法为多个选项卡过滤掉它。为了简单起见,我只想在这里为“全部”和“高中”做标签。

这是我的models.py: 从 django.db 导入模型

class School(models.Model):
school_name = models.CharField(max_length=200)
location_state = models.CharField(max_length=2)
grades = models.CharField(max_length=20)

这是我的 state.py 模板:

{% extends 'base.html' %}

{% block content %}

<h2>{{ state }}</h2> #This works and shows the state based on the URL

<div class="row">
<div class="col-12 col-sm-12 col-lg-12">
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="#all">All</a></li>
<li><a href="#high">High School</a></li>
</ul>
</div>

{% for school in schools_by_state %}
<div id="content" class="tab-content">
<div class="tab-pane active" id="all">
<ul>
<li>{{ school.school_name }}</li>
</ul>
</div>
<div class="tab-pane" id="high">
<ul>
<li>{{ ???highschool??? }}</li>
</ul>
</div>
</div><!-- end content -->
</div><!-- end row -->
{% endfor %}

{% endblock content %}

这是我的views.py:

from django.views.generic import ListView

from .models import School

class StateListView(ListView):
model = School
template_name = 'state.html'
context_object_name = 'schools_by_state'

def get_queryset(self):
state_list = self.kwargs['location_state']
return School.objects.filter(location_state=state_list)

def get_context_data(self, **kwargs):
context = super(StateListView, self).get_context_data(**kwargs)
context.update({'state': self.kwargs['location_state']})
return context

为了完整起见,这里是此 View 的urls.py:

url(r'^(?P<location_state>[A-Z]{2})/$', StateListView.as_view()),

我不认为我想在这里使用多个查询集,而是找到一种方法来向“highschool” View 中的 context_data 添加额外的过滤器,然后我可以将其添加到我的模板中。然而,我添加额外上下文过滤器的尝试都失败了。想法?

最佳答案

您可以将新的查询集添加到上下文中:

def get_context_data(self, **kwargs):
context = super(StateListView, self).get_context_data(**kwargs)
context.update({'state': self.kwargs['location_state']})

context['schools_highschool'] = context['schools_by_state'].filter(grades='9-12')

return context

然后在模板中循环schools_highschool。我认为你的模板也有点不对劲。也许这样做:

{% extends 'base.html' %}

{% block content %}

<h2>{{ state }}</h2> #This works and shows the state based on the URL

<div class="row">
<div class="col-12 col-sm-12 col-lg-12">
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="#all">All</a></li>
<li><a href="#high">High School</a></li>
</ul>
</div>


<div id="content" class="tab-content">

<div class="tab-pane active" id="all">
<ul>
{% for school in schools_by_state %}
<li>{{ school.school_name }}</li>
{% endfor %}
</ul>
</div>

<div class="tab-pane" id="high">
<ul>
{% for school in schools_highschool %}
<li>{{ school.school_name }}</li>
{% endfor %}
</ul>
</div>

</div><!-- end content -->

</div><!-- end row -->

{% endblock content %}

关于python - Django 中的 Bootstrap3 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com