问题
I want to implement Bootstrap3 tabs in my app, which displays school data by state. So if you go to example.com/ma/ you will see information for the state of Massachusetts and tabs to sort by grade level.
I am already using the queryset to filter by state so that on example.com/ma/ only "ma" results appear. And I can show ALL data in one of the tabs, but can't filter it out for multiple tabs. To keep it simple, I just want to do tabs for "All" and "High School" here.
Here is my models.py: from django.db import models
class School(models.Model):
school_name = models.CharField(max_length=200)
location_state = models.CharField(max_length=2)
grades = models.CharField(max_length=20)
Here is my template for 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 %}
And here is my 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
For completeness, here is the urls.py for this view:
url(r'^(?P<location_state>[A-Z]{2})/$', StateListView.as_view()),
I don't believe I want to use multiple querysets here, but instead find a way to add an additional filter to my context_data in the view for "highschool" which I can then add to my template. However my attempts to add additional context filters have all failed. Thoughts?
回答1:
You can just add a new queryset to the context:
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
Then loop schools_highschool
in the template. I think your template is a little off too. Maybe do this:
{% 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 %}
回答2:
jproffitts answer is correct, but you can also filter based on this one query set in your template:
<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 -->
来源:https://stackoverflow.com/questions/19846967/bootstrap3-tabs-in-django