首页 > 代码库 > 组合搜索组件

组合搜索组件

一、方法一获取前一次的URL,然后通过模板来实现

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html, views.article, name=article),
]
技术分享
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <style>
 7         .condition a{
 8             display: inline-block;
 9             padding: 3px 5px;
10             border: 1px solid #dddddd;
11             margin: 5px 5px;
12         }
13         .condition a.active{
14             background-color: brown;
15         }
16     </style>
17 </head>
18 <body>
19     <h1>过滤条件</h1>
20     <div class="condition">
21         <div>
22             {% if arg_dict.article_type_id == 0 %}
23                 <a class="active" href="article-0-{{ arg_dict.category_id }}.html">全部</a>
24             {% else %}
25                 <a href="article-0-{{ arg_dict.category_id }}.html">全部</a>
26             {% endif %}
27             {% for row in article_type_list %}
28                 {% if row.id == arg_dict.article_type_id %}
29                     <a class="active" href="article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
30                 {% else %}
31                     <a href="article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
32                 {% endif %}
33             {% endfor %}
34         </div>
35         <div>
36             {% if arg_dict.category_id == 0 %}
37                 <a class="active" href="article-{{ arg_dict.article_type_id }}-0.html">全部</a>
38             {% else %}
39                 <a href="article-{{ arg_dict.article_type_id }}-0.html">全部</a>
40             {% endif %}
41             {% for row in category_list %}
42                 {% if row.id == arg_dict.category_id %}
43                     <a class="active" href="article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
44                 {% else %}
45                     <a href="article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
46                 {% endif %}
47             {% endfor %}
48         </div>
49     </div>
50 
51     <h1>查询结果</h1>
52     <ul>
53     {% for row in result %}
54         <li>{{ row.id }}-{{ row.title }}</li>
55     {% endfor %}
56     </ul>
57 </body>
58 </html>
html
技术分享
 1 from django.shortcuts import render
 2 from app01 import models
 3 # Create your views here.
 4 
 5 
 6 def article(request, **kwargs):
 7     condition = {}
 8     for k, v in kwargs.items():
 9         kwargs[k] = int(v)
10         if v == "0":
11             pass
12         else:
13             condition[k] = v
14     article_type_list = models.ArticleType.objects.all()
15     category_list = models.Category.objects.all()
16     result = models.Article.objects.filter(**condition)
17     return render(request,
18                   "article.html",
19                   {"result": result,
20                    "article_type_list": article_type_list,
21                    "category_list": category_list,
22                    "arg_dict": kwargs,
23                    },
24                   )
views

 

组合搜索组件