【问题标题】:Bootstrap3 tabs in DjangoDjango 中的 Bootstrap3 选项卡
【发布时间】:2013-11-19 17:59:00
【问题描述】:

我想在我的应用中实现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

为了完整起见,这里是该视图的 urls.py

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

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

【问题讨论】:

  • 所以您想要有两个选项卡,一个显示该州的所有学校,一个显示学校的子集?子集是什么?
  • 您想要一个标签用于每个高中,还是一个标签用于一个州的所有高中?
  • @jproffitt 是的,正确。第二个选项卡的子集是高中,所以在模型中,字段“grades”=“9-12”。
  • @Jay 一个州所有高中的一个标签。

标签: python django django-views twitter-bootstrap-3 django-class-based-views


【解决方案1】:

jproffitts 的答案是正确的,但您也可以根据模板中设置的这一查询进行过滤:

<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_by_state %}
         {% if school.grade == "9-12" %}
        <li>{{ school.school_name }}</li>
         {% endif %}
        {% endfor %}
    </ul>
    </div>  
</div><!-- end content -->

【讨论】:

  • 感谢您提供此示例。我在其他地方(主要是 2 Scoops of Django)读到,从性能的角度来看,在视图中过滤比在模板中更好。你同意吗?
【解决方案2】:

你可以在上下文中添加一个新的查询集:

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 %}

【讨论】:

  • 你刚刚震撼了我的世界。谢谢!
猜你喜欢
  • 2023-03-19
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多