7-6 列表筛选功能

一、机构类别


后台逻辑:


二、所在地区


后台逻辑:


代码:

{% extends 'base.html' %}
{% block title %}课程机构列表 - 慕学在线网{% endblock %}
{% load staticfiles %}
{% block custom_bread %}
    <section>
        <div class="wp">
            <ul  class="crumbs">
                <li><a href="index.html">首页</a>></li>
                <li>课程机构</li>
            </ul>
        </div>
    </section>
{% endblock %}

{% block content %}
<section>
	<div class="wp butler_list_box list">
	<div class='left'>
		<div class="listoptions">
			<ul>
				<li>
					<h2>机构类别</h2>
					<div class="cont">
                        <a href="?city={{ city_id }}"><span class="{% ifequal category '' %}active2{% endifequal %}">全部</span></a>

                            <a href="?ct=pxjg&city={{ city_id }}"><span class="{% ifequal category 'pxjg' %}active2{% endifequal %}">培训机构</span></a>

                            <a href="?ct=gx&city={{ city_id }}"><span class="{% ifequal category 'gx' %}active2{% endifequal %}">高校</span></a>

                            <a href="?ct=gr&city={{ city_id }}"><span class="{% ifequal category 'gr' %}active2{% endifequal %}">个人</span></a>

					</div>
				</li>
				<li>
					<h2>所在地区</h2>
					<div class="more">更多</div>
					<div class="cont">
                        <a href="?ct={{ category }}"><span class="{% ifequal city_id '' %}active2{% endifequal %}">全部</span></a>
                                {% for city in all_citys %}
                                    <a href="?city={{ city.id }}&ct={{ category }}"><span class="{% ifequal city_id city.id|stringformat:"i" %}active2{% endifequal %}">{{ city.name }}</span></a>
                                {% endfor %}

					</div>
				</li>
			</ul>
		</div>
		<div class="all">共<span class="key">{{ org_nums }}</span>家</div>
		<div class="butler_list company list">
		<div class="layout">
			<div class="head">
				<ul class="tab_header">
					<li class="active"><a href="?ct=&city=">全部</a> </li>
					<li class=""><a href="?sort=students&ct=&city=">学习人数 ↓</a></li>
					<li class=""><a href="?sort=courses&ct=&city=">课程数 ↓</a></li>
				</ul>
			</div>

                {% for course_org in all_orgs.object_list %}
                    <dl class="des difdes">
                    <dt>
                        <a href="org-detail-homepage.html">
                            <img width="200" height="120" class="scrollLoading" data-url="{{ MEDIA_URL }}{{ course_org.image }}"/>
                        </a>
                    </dt>
                    <dd>
                        <div class="clearfix">
                             <a href="org-detail-homepage.html">
                                 <h1>{{ course_org.name }}</h1>
                                 <div class="pic fl">

                                         <img src="{% static 'images/authentication.png'%}"/>

                                         <img src="{% static 'images/gold.png'%}"/>

                                 </div>
                             </a>
                        </div>
                        <ul class="cont">
                            <li class="first"><p class="pic9">课程数:<span>1</span></p><p class="c7">学习人数:<span>1000</span></p></li>
                            <li class="c8" style="padding-left:18px;">北京市海淀区中关村北大街</li>
                            <li class="pic10" style="padding-left:18px;">经典课程:

                                    <a href="/diary/19/">c语言基础入门</a>

                                    <a href="/diary/16/">数据库基础</a>

                            </li>
                        </ul>
                    </dd>
                    <div class="buy start_groupbuy jsShowPerfect2" data-id="22"><br/>联系<br/>服务</div>
                </dl>
                {% endfor %}

		</div>
		 <div class="pageturn">
            <ul class="pagelist">
                {% if all_orgs.has_previous %}
                    <li class="long"><a href="?{{ all_orgs.previous_page_number.querystring }}">上一页</a></li>
                {% endif %}

                {% for page in all_orgs.pages %}
                    {% if page %}
                        {% ifequal page all_orgs.number %}
                            <li class="active"><a href="?{{ page.querystring }}">{{ page }}</a></li>
                        {% else %}
                            <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li>
                        {% endifequal %}
                        {% else %}
                           <li class="none"><a href="">...</a> </li>
                        {% endif %}
                {% endfor %}

                {% if all_orgs.has_next %}
                    <li class="long"><a href="?{{ all_orgs.next_page_number.querystring }}">下一页</a></li>
                {% endif %}

            </ul>
        </div>
		</div>
	</div>
	<div class="right companyright">
			<div class="head">我要学习</div>
			<form class="rightform" id="jsStayForm">
				<div>
                    <img src="{% static 'images/rightform1.png'%}"/>
                    <input type="text" name="name" id="companyName" placeholder="名字" maxlength="25" />
                </div>
				<div>
                    <img src="{% static 'images/rightform2.png'%}"/>
                    <input type="text" name="mobile" id="companyMobile" placeholder="联系电话"/>
                </div>
				<div>
                    <img src="{% static 'images/rightform3.png'%}"/>
                    <input type="text" name="course_name" id="companyAddress" placeholder="课程名" maxlength="50" />
                </div>
                <p class="error company-tips" id="jsCompanyTips"></p>
				<input class="btn" type="text" id="jsStayBtn" value="立即咨询 >" />
            <input type='hidden' name='csrfmiddlewaretoken' value='5I2SlleZJOMUX9QbwYLUIAOshdrdpRcy' />
            </form>
		</div>

		<div class="right companyrank layout">
			<div class="head">授课机构排名</div>


                <dl class="des">
                    <dt class="num fl">1</dt>
                    <dd>
                        <a href="/company/2/"><h1>慕课网</h1></a>
                        <p>北京市</p>
                    </dd>
                </dl>

                <dl class="des">
                    <dt class="num fl">2</dt>
                    <dd>
                        <a href="/company/2/"><h1>慕课网2</h1></a>
                        <p>深圳市</p>
                    </dd>
                </dl>

                <dl class="des">
                    <dt class="num fl">3</dt>
                    <dd>
                        <a href="/company/2/"><h1>北京大学</h1></a>
                        <p>北京市</p>
                    </dd>
                </dl>




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

后台逻辑代码:

# _*_ encoding:utf-8 _*_
from django.shortcuts import render
from django.views.generic import View
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from .models import CourseOrg, CityDict
# Create your views here.


class OrgView(View):
    """
    课程机构列表功能
    """
    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()
        # 对课程机构进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        # 一定要3个参数,不然报错
        p = Paginator(all_orgs, 5, request=request)

        orgs = p.page(page)

        return render(request, "org-list.html", {
            "all_orgs": orgs,
            "all_citys": all_citys,
            "org_nums": org_nums,
            "city_id": city_id,
            "category": category
        })

三、授课机构排名

		<div class="right companyrank layout">
			<div class="head">授课机构排名</div>

                {% for curent_org in hot_orgs %}
                    <dl class="des">
                    <dt class="num fl">{{ forloop.counter }}</dt>
                    <dd>
                        <a href="/company/2/"><h1>{{ curent_org.name }}</h1></a>
                        <p>{{ curent_org.address }}</p>
                    </dd>
                </dl>

                {% endfor %}

		</div>

后台逻辑:

        # 热门机构
        hot_orgs = all_orgs.order_by("-click_nums")[:3]

四、排序  

学习人数 课程数

由于之前model里没有学习人数和课程数,现在加上


同步字段到数据库



			<div class="head">
				<ul class="tab_header">
					<li class="{% if sort == '' %}active{% endif %}"><a href="?&ct={{ category }}&city={{ city_id }}">全部</a> </li>
					<li class="{% if sort == 'students' %}active{% endif %}"><a href="?sort=students&ct={{ category }}&city={{ city_id }}">学习人数 ↓</a></li>
					<li class="{% if sort == 'courses' %}active{% endif %}"><a href="?sort=courses&&ct={{ category }}&city={{ city_id }}">课程数 ↓</a></li>
				</ul>
			</div>

        # 排序
        sort = request.GET.get('sort', "")
        if sort:
            if sort == "students":
                # 倒序
                all_orgs = all_orgs.order_by("-students")
            elif sort == "courses":
                all_orgs = all_orgs.order_by("-course_nums")





















猜你喜欢

转载自blog.csdn.net/huanglianggu/article/details/80725508
7-6