سؤال

اريد انفاءة bootstrap3 علامات التبويب في تطبيقي ، الذي يعرض بيانات المدرسة حسب الولاية. لذلك إذا ذهبت إلى example.com/ma/ ، فسترى معلومات عن ولاية ماساتشوستس وعلامات التبويب لفرزها حسب مستوى الصف.

أنا أستخدم بالفعل QuerySet للتصفية حسب الحالة بحيث تظهر نتائج على سبيل المثال/ ama/ فقط "MA". ويمكنني إظهار جميع البيانات في إحدى علامات التبويب ، لكن لا يمكنني تصفيةها لعلامات التبويب المتعددة. للحفاظ على الأمر بسيطًا ، أريد فقط القيام بعلامات تبويب لـ "All" و "High School" هنا.

هنا أنا النماذج: من نماذج استيراد 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()),

لا أعتقد أنني أرغب في استخدام QuerySets متعددة هنا ، ولكن بدلاً من ذلك ، أجد طريقة لإضافة مرشح إضافي إلى Context_data في عرض "Highschool" الذي يمكنني إضافته إلى القالب الخاص بي. ومع ذلك فشلت محاولاتي لإضافة مرشحات سياق إضافية. أفكار؟

هل كانت مفيدة؟

المحلول

يمكنك فقط إضافة 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 صحيحة ، ولكن يمكنك أيضًا التصفية بناءً على هذا الاستعلام الواحد في القالب الخاص بك:

<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