首页 > 其他 > 详细

[py][mx]django实现根据城市和课程机构类别过滤

时间:2018-02-07 23:57:17      阅读:313      评论:0      收藏:0      [点我收藏+]

实现根据城市&课程机构过滤

实现点谁谁高亮,支持取交集.
技术分享图片

直接上代码吧

本质上是过滤,多层过滤,取交集

    def get(self, request):
        all_orgs = CourseOrg.objects.all()  # 所有课程机构

        all_citys = CityDict.objects.all()  # 所有城市列表

        # 取出筛选城市
        city_id = request.GET.get("city", "")
        if city_id:
            all_orgs = all_orgs.filter(city_id=int(city_id))

        # 取出筛选培训机构
        category = request.GET.get('ct', "")
        if category:
            all_orgs = all_orgs.filter(category=category)

        org_nums = all_orgs.count()  # 多少家课程机构
class OrgView(View):  # 课程机构列表页
    def get(self, request):
        all_orgs = CourseOrg.objects.all()  # 所有课程机构

        all_citys = CityDict.objects.all()  # 所有城市列表

        # 取出筛选城市
        city_id = request.GET.get("city", "")#前端传递来city.id
        if city_id:
            all_orgs = all_orgs.filter(city_id=int(city_id))

        # 取出筛选培训机构
        category = request.GET.get('ct', "")#前端传来ct类型
        if category:
            all_orgs = all_orgs.filter(category=category)

        org_nums = all_orgs.count()  # 多少家课程机构
        # 分页模块
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        # all_orgs = ['john', 'edward', 'josh', 'frank']

        # Provide Paginator with the request object for complete querystring generation
        p = Paginator(all_orgs, 3, request=request)
        orgs = p.page(page)

        return render(request, 'org-list.html', {
            "all_orgs": orgs,
            "all_citys": all_citys,
            'org_count': org_nums,
            'city_id': city_id,#将city_id传回去
            'category': category,#将category传回去
        })

org-list.html

{% extends 'base.html' %}{# 一定要出现在第一行 #}
{% load staticfiles %}
{% block title %}
    课程列表
{% endblock %}

{% block custom_bread %}
    <div>
        <ul>
            <li><a href="">首页</a>>课程机构</li>

        </ul>
    </div>
{% endblock %}

{% block content %}
    {#    机构类别    #}
    <div>
        <strong>机构类别</strong>:
        <a href="?city={{ city_id }}"><span class="{% ifequal category '' %}bgColor{% endifequal %}">全部</span></a>
        <a href="?ct=pxjg&city={{ city_id }}"><span class="{% ifequal category 'pxjg' %}bgColor{% endifequal %}">培训机构</span></a>
        <a href="?ct=gx&city={{ city_id }}"><span class="{% ifequal category 'gx' %}bgColor{% endifequal %}">高校</span></a>
        <a href="?ct=gr&city={{ city_id }}"><span class="{% ifequal category 'gr' %}bgColor{% endifequal %}">个人</span></a>
    </div>

    {#    城市    #}
    <div>
        <p><strong>城市:</strong>
            <span class="{% ifequal city_id '' %}bgColor{% endifequal %}"><a href="?ct={{ category }}">全部</a></span>
            {% for city in all_citys %}
                <span class="{% ifequal city_id city.id|stringformat:'i' %}bgColor{% endifequal %}"><a
                        href="?city={{ city.id }}&ct={{ category }}">{{ city.name }}</a></span>
            {% endfor %}
        </p>
    </div>
    {#    课程机构    #}
    <div>
        <strong>共{{ org_count }}家</strong>
        <ul>
            {% for course_org in all_orgs.object_list %}
                <li><img src="{{ MEDIA_URL }}{{ course_org.image }}" alt=""></li>
                <li>{{ course_org }}</li>
            {% endfor %}
        </ul>
        <p>{{ all_orgs.render }}</p>
    </div>
{% endblock %}

这部分


这里city.id是整形,需要django tmpl的方法|stringformat:'i'将前面参数转换为整数后再比较

哪个citry_id,即显示哪个
class="{% ifequal city_id city.id|stringformat:'i' %}bgColor{% endifequal %}"

[py][mx]django实现根据城市和课程机构类别过滤

原文:https://www.cnblogs.com/iiiiiher/p/8428610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!