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

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

实现点谁谁高亮,支持取交集.

直接上代码吧

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

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

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

时间: 2024-08-30 10:43:05

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

[py][mx]django添加后台课程机构页数据-图片上传设置

分析下课程页前台部分 机构类别-目前机构库中没有这个字段,需要追加下 所在地区 xadmin可以手动添加 课程机构 涉及到机构封面图, 即图片上传media设置, 也需要在xadmin里手动添加几条 用xadmin添加数据 所在地区添加 课程机构添加 补充机构类别在model里的字段 class CourseOrg(models.Model): catagory = models.CharField(max_length=20, default="pxjg", choices=((&q

[py][mx]django分页第三方模块django-pure-pagination

分页模块django-pure-pagination - 一款基于django pagination封装的更好用的分页模块 https://github.com/jamespacileo/django-pure-pagination - 安装 pip install django-pure-pagination views.py from pure_pagination import Paginator, EmptyPage, PageNotAnInteger class OrgView(Vie

[py][mx]django处理登录逻辑

处理登录逻辑 users/views.py from django.contrib.auth import authenticate, login def user_login(request): if request.method == "POST": user_name = request.POST.get("username", "") pass_word = request.POST.get("password", &

[py][mx]django自定义认证类

创建自定义验证用户名密码类CustomBackend users/views.py from django.contrib.auth import authenticate, login from django.contrib.auth.backends import ModelBackend from django.shortcuts import render # Create your views here. from users.models import UserProfile cla

[py][mx]django使用class写views-免去判断方法的烦恼

修改views使用class模式 类模式写views - 免去了函数模式的判断的烦恼 users/views.py from django.views.generic import View class UserView(View):#继承了View类,它里面实现get post等方法, 使用类模式写免去了函数模式的判断 def get(self, request): return render(request, "login.html", {}) def post(self, req

Django(十四)课程机构列表页数据展示,Django的modelform,关于urls的重新分发

关于urls的重新分发: 如果所有url都配置在根路径的urls.py里,会特别多,而且也不易于修改,Django框架里支持urls的重新分发: 1.在根路径的urls配置上: PS:namespace是定义命名空间,比如下面这种配置, urlpatterns = [ #课程机构url配置,验证码,每个app可以书写自己的url,然后通过include进来 url(r'^org/', include('oraganization.urls',namespace="org")), ] 2

[py][mx]django的cookie和session操作

这玩意可以实现7天免登录等功能. session和cookie机制原理和交互过程 交互过程 ① 客户端访问,无服务端写入的Cookie ② 服务端的Cookie写入浏览器 ③ 浏览器解析Cookie,保存至浏览器文件 ④ 客户端访问,有服务端写入的Cookie ⑤ 服务器获取 django请求中的cookie 第一次访问服务端会给一个csrf的cookie 登录完成后,默认给一个为期半个月的cookie 用于访问别的也没使用. django中cookie与session的实现原理 app默认注册

[py][mx]django get方法返回login页面

get方法返回login.html users/views.py def login(request): if request.method == "POST": pass elif request.method == "GET": return render(request, "login.html") 前端请求发来request对象里包含了method,path等. 可以debug单步调试看到 users/urls.py from users

[py][mx]django通过邮箱找回密码

忘记密码处理流程 直接上代码 class ActiveView(View): # 主要功能是修改user_profile里的is_active字段为1 def get(self, request, active_code): all_reocrds = EmailVerifyRecord.objects.filter(code=active_code) if all_reocrds: for record in all_reocrds: email = record.email user = U