Django 组合索引
1 TEMPLATES = [ 2 { 3 'BACKEND': 'django.template.backends.django.DjangoTemplates', 4 'DIRS': [os.path.join(BASE_DIR, 'templates')], 5 'APP_DIRS': True, 6 'OPTIONS': { 7 'context_processors': [ 8 'django.template.context_processors.debug', 9 'django.template.context_processors.request', 10 'django.contrib.auth.context_processors.auth', 11 'django.contrib.messages.context_processors.messages', 12 ], 13 'libraries': { # Adding this section should work around the issue. 14 'staticfiles': 'django.templatetags.static', 15 }, 16 }, 17 }, 18 ]
urls.py
1 from django.contrib import admin 2 from django.urls import path, re_path 3 from cmdb import views 4 from django.conf.urls import url, include 5 from app04 import views 6 7 urlpatterns = [ 8 path(r'article/', views.article), 9 re_path(r'article-(?P<article_type_id>\d+)-(?P<category_id>\d+)', views.article), 10 ]
1 from django.shortcuts import render 2 from app04 import models 3 # Create your views here. 4 5 6 def article(request, **kwargs): 7 article_type_list = models.ArticleType.objects.all() 8 category_list = models.Category.objects.all() 9 condition = {} 10 if not kwargs: # 第一次访问时,字典中的值不存在,设置初值 11 kwargs['article_type_id'] = 0 12 kwargs['category_id'] = 0 13 for k, v in kwargs.items(): # 把值为0的k,v过滤排除掉,字典如:{'category_id': '2', 'article_type_id': '1'} 14 kwargs[k] = int(v) # 把字典中的字符串转换位数字,传给前台 15 if v == '0': 16 pass 17 else: 18 condition[k] = v 19 print(condition) 20 result = models.Article.objects.filter(**condition) 21 print(result) 22 return render(request, 23 'article.html', 24 { 25 'result': result, 26 'article_type_list': article_type_list, 27 'category_list': category_list, 28 'arg_dict': kwargs, 29 } 30 )
1 from django import template 2 from django.utils.safestring import mark_safe 3 register = template.Library() 4 5 6 @register.simple_tag 7 def filter_all(arg_dict, k): 8 ''' 9 逻辑:对两种类型的索引显示"全部"时添加高亮 10 {% if arg_dict.article_type_id == 0 %} 11 <a class="active" href="/article-0-{{ arg_dict.category_id }}">全部</a> 12 {% else %} 13 <a href="/article-0-{{ arg_dict.category_id }}">全部</a> 14 {% endif %} 15 :param arg_dict: 视图函数中的kwarg字典,如:{'category_id': '2', 'article_type_id': '1'} 16 :param k: 前端组合索引的两种分类:1.article_type_id 2.category_id 17 :return: 返回a标签 18 ''' 19 ret = '' 20 if k == 'article_type_id': 21 if arg_dict['article_type_id'] == 0: 22 ret = '<a class="active" href="/article-0-%s">全部</a>' % arg_dict['category_id'] 23 else: 24 ret = '<a href="/article-0-%s">全部</a>' % arg_dict['category_id'] 25 elif k == 'category_id': 26 if arg_dict['category_id'] == 0: 27 ret = '<a class="active" href="/article-%s-0">全部</a>' % arg_dict['article_type_id'] 28 else: 29 ret = '<a href="/article-%s-0">全部</a>' % arg_dict['article_type_id'] 30 return mark_safe(ret) 31 32 33 @register.simple_tag 34 def filter_article_type(arg_dict, g, h): 35 """ 36 {% for row in article_type_list %} 37 {% if row.id == arg_dict.article_type_id %} 38 <a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a> 39 {% else %} 40 <a href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a> 41 {% endif %} 42 {% endfor %} 43 :param arg_dict: 44 :param g: article_type_list、category_list这两个列表,用于循环生成标签 45 :return:很多a标签 46 """ 47 a = [] 48 for row in g: 49 if h == 'article_type_list': 50 if row.id == arg_dict['article_type_id']: 51 s = '<a class="active" href="/article-%s-%s">%s</a>' % (row.id, arg_dict['category_id'], row.caption) 52 else: 53 s = '<a href="/article-%s-%s">%s</a>' % (row.id, arg_dict['category_id'], row.caption) 54 a.append(s) 55 elif h == 'category_list': 56 if row.id == arg_dict['category_id']: 57 s = '<a class="active" href="/article-%s-%s">%s</a>' % (row.id, arg_dict['article_type_id'], row.caption) 58 else: 59 s = '<a href="/article-%s-%s">%s</a>' % (arg_dict['article_type_id'], row.id, row.caption) 60 a.append(s) 61 ret = ''.join(a) 62 return mark_safe(ret)
1 {% load filter %} 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Title</title> 7 <style> 8 .condition a{ 9 display: inline-block; 10 padding: 3px 5px; 11 border:1px solid #dddddd; 12 margin: 5px 5px; 13 } 14 .condition a.active{ 15 background-color: dodgerblue; 16 } 17 </style> 18 </head> 19 <body> 20 <h1>过滤条件</h1> 21 <div class="condition"> 22 <div> 23 {% filter_all arg_dict 'article_type_id'%} 24 {# {% if arg_dict.article_type_id == 0 %}#} 25 {# <a class="active" href="/article-0-{{ arg_dict.category_id }}">全部</a>#} 26 {# {% else %}#} 27 {# <a href="/article-0-{{ arg_dict.category_id }}">全部</a>#} 28 {# {% endif %}#} 29 {% filter_article_type arg_dict article_type_list 'article_type_list' %} 30 {# {% for row in article_type_list %}#} 31 {# {% if row.id == arg_dict.article_type_id %}#} 32 {# <a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>#} 33 {# {% else %}#} 34 {# <a href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>#} 35 {# {% endif %}#} 36 {# {% endfor %}#} 37 </div> 38 <div> 39 {% filter_all arg_dict 'category_id'%} 40 {# {% if arg_dict.category_id == 0 %}#} 41 {# <a class="active" href="/article-{{ arg_dict.article_type_id }}-0">全部</a>#} 42 {# {% else %}#} 43 {# <a href="/article-{{ arg_dict.article_type_id }}-0">全部</a>#} 44 {# {% endif %}#} 45 {% filter_article_type arg_dict category_list 'category_list' %} 46 {# {% for row in category_list %}#} 47 {# {% if row.id == arg_dict.category_id %}#} 48 {# <a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}">{{ row.caption }}</a>#} 49 {# {% else %}#} 50 {# <a href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}">{{ row.caption }}</a>#} 51 {# {% endif %}#} 52 {# {% endfor %}#} 53 </div> 54 </div> 55 56 <h1>查询结果</h1> 57 <ul> 58 {% for row in result %} 59 <li>{{ row.id }}-{{ row.title }}</li> 60 {% endfor %} 61 </ul> 62 </body> 63 </html>
1 from django.db import models 2 3 # Create your models here. 4 5 6 class Category(models.Model): 7 caption = models.CharField(max_length=16) 8 9 10 class ArticleType(models.Model): 11 caption = models.CharField(max_length=16) 12 13 14 class Article(models.Model): 15 title = models.CharField(max_length=32) 16 content = models.CharField(max_length=55) 17 18 category = models.ForeignKey(Category, on_delete=models.CASCADE) 19 article_type = models.ForeignKey(ArticleType, on_delete=models.CASCADE) 20 # 直接写到内存,不写到表中 21 # type_choice = ( 22 # (0, 'python'), 23 # (0, 'openstack'), 24 # (0, 'linux'), 25 # ) 26 # article_type_id = models.IntegerField(choices=type_choice)
上面是通过去数据库取数据实现,下面方法通过从内存中(利用类的静态字段)取数据实现:
1 from django import template 2 from django.utils.safestring import mark_safe 3 register = template.Library() 4 5 6 @register.simple_tag 7 def filter_all(arg_dict, k): 8 ''' 9 逻辑:对两种类型的索引显示"全部"时添加高亮 10 {% if arg_dict.article_type_id == 0 %} 11 <a class="active" href="/article-0-{{ arg_dict.category_id }}">全部</a> 12 {% else %} 13 <a href="/article-0-{{ arg_dict.category_id }}">全部</a> 14 {% endif %} 15 :param arg_dict: 视图函数中的kwarg字典,如:{'category_id': '2', 'article_type_id': '1'} 16 :param k: 前端组合索引的两种分类:1.article_type_id 2.category_id 17 :return: 返回a标签 18 ''' 19 ret = '' 20 if k == 'article_type_id': 21 if arg_dict['article_type_id'] == 0: 22 ret = '<a class="active" href="/article-0-%s">全部</a>' % arg_dict['category_id'] 23 else: 24 ret = '<a href="/article-0-%s">全部</a>' % arg_dict['category_id'] 25 elif k == 'category_id': 26 if arg_dict['category_id'] == 0: 27 ret = '<a class="active" href="/article-%s-0">全部</a>' % arg_dict['article_type_id'] 28 else: 29 ret = '<a href="/article-%s-0">全部</a>' % arg_dict['article_type_id'] 30 return mark_safe(ret) 31 32 33 @register.simple_tag 34 def filter_article_type(arg_dict, g, h): 35 """ 36 逻辑: 37 {% for row in article_type_list %} 38 {% if row.id == arg_dict.article_type_id %} 39 <a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a> 40 {% else %} 41 <a href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a> 42 {% endif %} 43 {% endfor %} 44 :param arg_dict: 45 :param g: article_type_list、category_list这两个列表,用于循环生成标签 46 :param h: article_type_list、category_list这两个列表的字符串形式传参,流程控制 47 :return:很多a标签 48 """ 49 a = [] 50 for row in g: 51 if h == 'article_type_list': 52 if row[0] == arg_dict['article_type_id']: 53 s = '<a class="active" href="/article-%s-%s">%s</a>' % (row[0], arg_dict['category_id'], row[1]) 54 else: 55 s = '<a href="/article-%s-%s">%s</a>' % (row[0], arg_dict['category_id'], row[1]) 56 a.append(s) 57 elif h == 'category_list': 58 if row.id == arg_dict['category_id']: 59 s = '<a class="active" href="/article-%s-%s">%s</a>' % (row.id, arg_dict['article_type_id'], row.caption) 60 else: 61 s = '<a href="/article-%s-%s">%s</a>' % (arg_dict['article_type_id'], row.id, row.caption) 62 a.append(s) 63 ret = ''.join(a) 64 return mark_safe(ret)
1 from django.db import models 2 3 # Create your models here. 4 5 6 class Category(models.Model): 7 caption = models.CharField(max_length=16) 8 9 10 # class ArticleType(models.Model): 11 # caption = models.CharField(max_length=16) 12 13 14 class Article(models.Model): 15 title = models.CharField(max_length=32) 16 content = models.CharField(max_length=55) 17 18 category = models.ForeignKey(Category, on_delete=models.CASCADE) 19 # article_type = models.ForeignKey(ArticleType, on_delete=models.CASCADE) 20 # 通过静态字段实现,直接写到内存,不写到表中 21 type_choice = ( 22 (1, 'python'), 23 (2, 'openstack'), 24 (3, 'linux'), 25 ) 26 article_type_id = models.IntegerField(choices=type_choice)
1 from django.shortcuts import render 2 from app04 import models 3 # Create your views here. 4 5 6 def article(request, **kwargs): 7 # article_type_list = models.ArticleType.objects.all() 8 article_type_list = models.Article.type_choice # 通过静态字段形式访问 9 category_list = models.Category.objects.all() 10 condition = {} 11 if not kwargs: # 第一次访问时,字典中的值不存在,设置初 值 12 kwargs['article_type_id'] = 0 13 kwargs['category_id'] = 0 14 for k, v in kwargs.items(): # 把值为0的k,v过滤排除掉,字典如:{'category_id': '2', 'article_type_id': '1'} 15 kwargs[k] = int(v) # 把字典中的字符串转换位数字,传给前台 16 if v == '0': 17 pass 18 else: 19 condition[k] = v 20 21 result = models.Article.objects.filter(**condition) 22 return render(request, 23 'article.html', 24 { 25 'result': result, 26 'article_type_list': article_type_list, 27 'category_list': category_list, 28 'arg_dict': kwargs, 29 } 30 )