質問

実装したい bootstrap3タブ 私のアプリでは、州ごとに学校のデータを表示します。したがって、example.com/ma/にアクセスすると、マサチューセッツ州とタブが学年ごとにソートする情報が表示されます。

私はすでにquerysetを使用して状態でフィルタリングしているので、example.com/ ma/ on "ma"の結果のみが表示されます。また、すべてのデータをタブの1つに表示できますが、複数のタブに対してフィルタリングすることはできません。シンプルに保つために、ここで「すべて」と「高校」のタブをやりたいだけです。

これが私のものです 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()),

ここで複数のクエリセットを使用したいとは思わないが、代わりに「高校」のビューでコンテキスト_DATAに追加のフィルターを追加する方法を見つけて、テンプレートに追加できる。ただし、追加のコンテキストフィルターを追加しようとする私の試みはすべて失敗しました。考え?

役に立ちましたか?

解決

コンテキストに新しいQuerySetを追加するだけです。

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

他のヒント

jproffittsの回答は正しいですが、テンプレートのこの1つのクエリセットに基づいてフィルタリングすることもできます。

<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 -->
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top