Learning to write an online screening qualified data according to different filter criteria and displays the Web site of record

Screening and training institutions in each city

On the front-end interface in accordance with the city's id to display information about the city.

      
      
1
2
3
      
      
{% for city in all_cities %}
<a href="?city={{ city.id }}"><span class="">{{ city.name }}</span></a>
{% endfor %}

We further screened in views (the view function) in accordance with the conditions.

Here's a little common sense. For example, we define training institutions in the model when there is a foreign key, although we pass a field name city, but the actual name of the field in the database is stored city_id, so give us a tremendous use of data Convenience.

      
      
1
2
3
4
5
      
      
Screening out of the city #
city_id = request.GET.get('city', '')
if city_id:
all_orgs = all_orgs.filter(city_id)

Here there was just being given that parameter error:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
      
      
Request Method: GET
Request URL: http://localhost:8000/org_list/?city=1
Django Version: 1.9
Exception Type: ValueError
Exception Value:
not enough values to unpack (expected 2, got 1)
Exception Location: /home/peter/mymooc/mymoocvenv/lib/python3.5/site-packages/django/db/models/sql/query.py in build_filter, line 1146
Python Executable: /home/peter/mymooc/mymoocvenv/bin/python
Python Version: 3.5.2
Python Path:
['/home/peter/mymooc/apps',
'/home/peter/mymooc',
'/root/.venvburrito/lib/python2.7/site-packages',
'/root/.venvburrito/lib/python2.7/site-packages/setuptools-28.8.0-py2.7.egg',
'/root/.venvburrito/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg',
'/home/peter/mymooc/mymoocvenv/lib/python35.zip',
'/home/peter/mymooc/mymoocvenv/lib/python3.5',
'/home/peter/mymooc/mymoocvenv/lib/python3.5/plat-x86_64-linux-gnu',
'/home/peter/mymooc/mymoocvenv/lib/python3.5/lib-dynload',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/home/peter/mymooc/mymoocvenv/lib/python3.5/site-packages']

修改如下:

      
      
1
2
      
      
if city_id:
all_orgs = all_orgs.filter(city_id=int(city_id))

ok,因为city_id在数据库中也是以整形数的形式存储的,也就是int类型的。而从前端页面传过来的的city_id是字符串类型的,所以需要强制转换成整形数,才能正确的找到要筛选的数据。

附上filter的源码:

      
      
1
2
3
4
5
6
      
      
def filter(self, *args, **kwargs):
"""
Returns a new QuerySet instance with the args ANDed to the existing
set.
"""
return self._filter_or_exclude(False, *args, **kwargs)

我们继续做选中状态的处理,如果当前的city的id和后端传过来的city的ID一样,那么就把当前的city设置成选中的状态。

这里总结的点就是每一个链接都要有一个合理的值,这个值可以是后台传过来的东西。

      
      
1
2
3
4
5
6
7
8
9
10
11
      
      
<h2>机构类别</h2>
<div class="cont">
<a href="?city={{ city.id }}"><span class="{% ifequal category '' %}active2{% endifequal %}">全部</span></a>
大专栏   自己写一个在线学习网站记录之按照不同的筛选条件筛选符合条件的数据并显示v class="line">
<a href="?ct=trainingorg&city={{ city.id }}"><span class="{% ifequal category 'trainingorg' %}active2{% endifequal %}">培训机构</span></a>
<a href="?ct=university&city={{ city.id }}"><span class="{% ifequal category 'university' %}active2{% endifequal %}">高校</span></a>
<a href="?ct=personal&city={{ city.id }}"><span class="{% ifequal category 'personal' %}active2{% endifequal %}">个人</span></a>
</div>

在这里培训机构的类别是固定的,所以不用再随着后台传入数据的变化而变化。但是培训机构的信息是从后端传过来的。

      
      
1
2
3
4
5
6
      
      
<div class="cont">
<a href="?ct={{ category }}"><span class="{% ifequal city_id '' %} active2 {% endifequal %}">全部</span></a>
{% for city in all_cities %}
<a href="?city={{ city.id }}&ct={{ category }}"><span class="{% ifequal city_id city.id|stringformat:"i" %}active2{% endifequal %}">{{ city.name }}</span></a>
{% endfor %}
</div>

在这里实现双重筛选,所以每次判断的条件都会取交集。


在培训机构的界面有一个机构排名,我们这里默认按照人气排名。其实这也是一个筛选的问题,或者说是一个排序的问题。

人气体现在什么地方呢,这么看是体现在点击量上,因为点击量越多,说明越受关注。具体的实现如下,直接上代码:

      
      
1
      
      
hot_orgs = all_orgs.order_by("click_num")[:5]

首先定义hot_oegs来存储筛选的信息,比如上面的代码的意思就是在所有的培训机构中抽出人气(点击量)前五名的培训机构。all_orgs是一个列表形式的,我们使用python中的内置的order_by函数对里面的元素进行排序,将排序结果做成切片,显示排名在前的前几个。然后

      
      
1
2
3
4
5
6
7
8
9
10
      
      
return render(request, "org-list.html", {
"all_orgs" : all_orgs,
"orgs" : orgs,
"all_cities" : all_cities,
"city_id":city_id,
"category":category,
"org_nums":org_nums,
"hot_orgs":hot_orgs,
"sort":sort,
})

我们将hot_orgs 传到前端文件,并且传入的名称也是hot_orgs,在前端的html文件中:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
      
      
<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>
</div>

This completes the ranking agencies.

Next similar can be achieved in accordance with the number of people learning and ascending or descending order with the number of courses and so on, in fact, the previous function is similar.


There are several problems to be solved next, is a template of

      
      
1
      
      
Ifequal% {%} {%}% endifequal

Next problem is an equal sign equal in views.py file because the syntax is python, the symbols are represented by double equal sign equal in the templates (i.e. html file, is represented by a like symbol equal number), this should be clear.

python comes with a lot of useful functions, this is its powerful place.

Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11712679.html