TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'libraries': { # Adding this section should work around the issue.
'staticfiles': 'django.templatetags.static',
},
},
},
]

settings

urls.py

 from django.contrib import admin
from django.urls import path, re_path
from cmdb import views
from django.conf.urls import url, include
from app04 import views urlpatterns = [
path(r'article/', views.article),
re_path(r'article-(?P<article_type_id>\d+)-(?P<category_id>\d+)', views.article),
]
 from django.shortcuts import render
from app04 import models
# Create your views here. def article(request, **kwargs):
article_type_list = models.ArticleType.objects.all()
category_list = models.Category.objects.all()
condition = {}
if not kwargs: # 第一次访问时,字典中的值不存在,设置初值
kwargs['article_type_id'] = 0
kwargs['category_id'] = 0
for k, v in kwargs.items(): # 把值为0的k,v过滤排除掉,字典如:{'category_id': '2', 'article_type_id': '1'}
kwargs[k] = int(v) # 把字典中的字符串转换位数字,传给前台
if v == '':
pass
else:
condition[k] = v
print(condition)
result = models.Article.objects.filter(**condition)
print(result)
return render(request,
'article.html',
{
'result': result,
'article_type_list': article_type_list,
'category_list': category_list,
'arg_dict': kwargs,
}
)

view

 from django import template
from django.utils.safestring import mark_safe
register = template.Library() @register.simple_tag
def filter_all(arg_dict, k):
'''
逻辑:对两种类型的索引显示"全部"时添加高亮
{% if arg_dict.article_type_id == 0 %}
<a class="active" href="/article-0-{{ arg_dict.category_id }}">全部</a>
{% else %}
<a href="/article-0-{{ arg_dict.category_id }}">全部</a>
{% endif %}
:param arg_dict: 视图函数中的kwarg字典,如:{'category_id': '2', 'article_type_id': '1'}
:param k: 前端组合索引的两种分类:1.article_type_id 2.category_id
:return: 返回a标签
'''
ret = ''
if k == 'article_type_id':
if arg_dict['article_type_id'] == 0:
ret = '<a class="active" href="/article-0-%s">全部</a>' % arg_dict['category_id']
else:
ret = '<a href="/article-0-%s">全部</a>' % arg_dict['category_id']
elif k == 'category_id':
if arg_dict['category_id'] == 0:
ret = '<a class="active" href="/article-%s-0">全部</a>' % arg_dict['article_type_id']
else:
ret = '<a href="/article-%s-0">全部</a>' % arg_dict['article_type_id']
return mark_safe(ret) @register.simple_tag
def filter_article_type(arg_dict, g, h):
"""
{% for row in article_type_list %}
{% if row.id == arg_dict.article_type_id %}
<a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>
{% else %}
<a href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>
{% endif %}
{% endfor %}
:param arg_dict:
:param g: article_type_list、category_list这两个列表,用于循环生成标签
:return:很多a标签
"""
a = []
for row in g:
if h == 'article_type_list':
if row.id == arg_dict['article_type_id']:
s = '<a class="active" href="/article-%s-%s">%s</a>' % (row.id, arg_dict['category_id'], row.caption)
else:
s = '<a href="/article-%s-%s">%s</a>' % (row.id, arg_dict['category_id'], row.caption)
a.append(s)
elif h == 'category_list':
if row.id == arg_dict['category_id']:
s = '<a class="active" href="/article-%s-%s">%s</a>' % (row.id, arg_dict['article_type_id'], row.caption)
else:
s = '<a href="/article-%s-%s">%s</a>' % (arg_dict['article_type_id'], row.id, row.caption)
a.append(s)
ret = ''.join(a)
return mark_safe(ret)

filter

 {% load filter %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.condition a{
display: inline-block;
padding: 3px 5px;
border:1px solid #dddddd;
margin: 5px 5px;
}
.condition a.active{
background-color: dodgerblue;
}
</style>
</head>
<body>
<h1>过滤条件</h1>
<div class="condition">
<div>
{% filter_all arg_dict 'article_type_id'%}
{# {% if arg_dict.article_type_id == 0 %}#}
{# <a class="active" href="/article-0-{{ arg_dict.category_id }}">全部</a>#}
{# {% else %}#}
{# <a href="/article-0-{{ arg_dict.category_id }}">全部</a>#}
{# {% endif %}#}
{% filter_article_type arg_dict article_type_list 'article_type_list' %}
{# {% for row in article_type_list %}#}
{# {% if row.id == arg_dict.article_type_id %}#}
{# <a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>#}
{# {% else %}#}
{# <a href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>#}
{# {% endif %}#}
{# {% endfor %}#}
</div>
<div>
{% filter_all arg_dict 'category_id'%}
{# {% if arg_dict.category_id == 0 %}#}
{# <a class="active" href="/article-{{ arg_dict.article_type_id }}-0">全部</a>#}
{# {% else %}#}
{# <a href="/article-{{ arg_dict.article_type_id }}-0">全部</a>#}
{# {% endif %}#}
{% filter_article_type arg_dict category_list 'category_list' %}
{# {% for row in category_list %}#}
{# {% if row.id == arg_dict.category_id %}#}
{# <a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}">{{ row.caption }}</a>#}
{# {% else %}#}
{# <a href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}">{{ row.caption }}</a>#}
{# {% endif %}#}
{# {% endfor %}#}
</div>
</div> <h1>查询结果</h1>
<ul>
{% for row in result %}
<li>{{ row.id }}-{{ row.title }}</li>
{% endfor %}
</ul>
</body>
</html>

html

 from django.db import models

 # Create your models here.

 class Category(models.Model):
caption = models.CharField(max_length=16) class ArticleType(models.Model):
caption = models.CharField(max_length=16) class Article(models.Model):
title = models.CharField(max_length=32)
content = models.CharField(max_length=55) category = models.ForeignKey(Category, on_delete=models.CASCADE)
article_type = models.ForeignKey(ArticleType, on_delete=models.CASCADE)
# 直接写到内存,不写到表中
# type_choice = (
# (0, 'python'),
# (0, 'openstack'),
# (0, 'linux'),
# )
# article_type_id = models.IntegerField(choices=type_choice)

models

上面是通过去数据库取数据实现,下面方法通过从内存中(利用类的静态字段)取数据实现:

 from django import template
from django.utils.safestring import mark_safe
register = template.Library() @register.simple_tag
def filter_all(arg_dict, k):
'''
逻辑:对两种类型的索引显示"全部"时添加高亮
{% if arg_dict.article_type_id == 0 %}
<a class="active" href="/article-0-{{ arg_dict.category_id }}">全部</a>
{% else %}
<a href="/article-0-{{ arg_dict.category_id }}">全部</a>
{% endif %}
:param arg_dict: 视图函数中的kwarg字典,如:{'category_id': '2', 'article_type_id': '1'}
:param k: 前端组合索引的两种分类:1.article_type_id 2.category_id
:return: 返回a标签
'''
ret = ''
if k == 'article_type_id':
if arg_dict['article_type_id'] == 0:
ret = '<a class="active" href="/article-0-%s">全部</a>' % arg_dict['category_id']
else:
ret = '<a href="/article-0-%s">全部</a>' % arg_dict['category_id']
elif k == 'category_id':
if arg_dict['category_id'] == 0:
ret = '<a class="active" href="/article-%s-0">全部</a>' % arg_dict['article_type_id']
else:
ret = '<a href="/article-%s-0">全部</a>' % arg_dict['article_type_id']
return mark_safe(ret) @register.simple_tag
def filter_article_type(arg_dict, g, h):
"""
逻辑:
{% for row in article_type_list %}
{% if row.id == arg_dict.article_type_id %}
<a class="active" href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>
{% else %}
<a href="/article-{{ row.id }}-{{ arg_dict.category_id }}">{{ row.caption }}</a>
{% endif %}
{% endfor %}
:param arg_dict:
:param g: article_type_list、category_list这两个列表,用于循环生成标签
:param h: article_type_list、category_list这两个列表的字符串形式传参,流程控制
:return:很多a标签
"""
a = []
for row in g:
if h == 'article_type_list':
if row[0] == arg_dict['article_type_id']:
s = '<a class="active" href="/article-%s-%s">%s</a>' % (row[0], arg_dict['category_id'], row[1])
else:
s = '<a href="/article-%s-%s">%s</a>' % (row[0], arg_dict['category_id'], row[1])
a.append(s)
elif h == 'category_list':
if row.id == arg_dict['category_id']:
s = '<a class="active" href="/article-%s-%s">%s</a>' % (row.id, arg_dict['article_type_id'], row.caption)
else:
s = '<a href="/article-%s-%s">%s</a>' % (arg_dict['article_type_id'], row.id, row.caption)
a.append(s)
ret = ''.join(a)
return mark_safe(ret)

filter

 from django.db import models

 # Create your models here.

 class Category(models.Model):
caption = models.CharField(max_length=16) # class ArticleType(models.Model):
# caption = models.CharField(max_length=16) class Article(models.Model):
title = models.CharField(max_length=32)
content = models.CharField(max_length=55) category = models.ForeignKey(Category, on_delete=models.CASCADE)
# article_type = models.ForeignKey(ArticleType, on_delete=models.CASCADE)
# 通过静态字段实现,直接写到内存,不写到表中
type_choice = (
(1, 'python'),
(2, 'openstack'),
(3, 'linux'),
)
article_type_id = models.IntegerField(choices=type_choice)

models

 from django.shortcuts import render
from app04 import models
# Create your views here. def article(request, **kwargs):
# article_type_list = models.ArticleType.objects.all()
article_type_list = models.Article.type_choice # 通过静态字段形式访问
category_list = models.Category.objects.all()
condition = {}
if not kwargs: # 第一次访问时,字典中的值不存在,设置初 值
kwargs['article_type_id'] = 0
kwargs['category_id'] = 0
for k, v in kwargs.items(): # 把值为0的k,v过滤排除掉,字典如:{'category_id': '2', 'article_type_id': '1'}
kwargs[k] = int(v) # 把字典中的字符串转换位数字,传给前台
if v == '':
pass
else:
condition[k] = v result = models.Article.objects.filter(**condition)
return render(request,
'article.html',
{
'result': result,
'article_type_list': article_type_list,
'category_list': category_list,
'arg_dict': kwargs,
}
)

view

Django 组合索引的更多相关文章

  1. Oracle组合索引与回表

    回表 简单来说就是数据库根据索引找到了指定的记录所在行后,还需要根据rowid再次到数据块里取数据的操作. "回表"一般就是指执行计划里显示的"TABLE ACCESS ...

  2. [慢查优化]建索引时注意字段选择性 & 范围查询注意组合索引的字段顺序

    文章转自:http://www.cnblogs.com/zhengyun_ustc/p/slowquery2.html 写在前面的话: 之前曾说过"不要求每个人一定理解 联表查询(join/ ...

  3. MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划

    这篇文章主要介绍了MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划的相关资料,需要的朋友可以参考下 一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存 ...

  4. mysql组合索引顺序参考

    问题背景 : 当我们需要创建一个组合索引, 索引的顺序对于效率影响很大, 怎么确定索引的顺序; 解决方法 : 我们应该依据字段的全局基数和选择性, 而不是字段的某个具体的值来确定; 表结构 :  dc ...

  5. mysql组合索引与字段顺序

    很多时候,我们在mysql中创建了索引,但是某些查询还是很慢,根本就没有使用到索引!一般来说,可能是某些字段没有创建索引,或者是组合索引中字段的顺序与查询语句中字段的顺序不符. 看下面的例子:假设有一 ...

  6. Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别

    Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...

  7. MySQL单列索引和组合索引的区别介绍

    MySQL单列索引和组合索引的区别介绍 作者:佚名出处:IT专家网2010-11-22 13:05 MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引的区别可能有 ...

  8. 组合索引leaf 数据存储

    1 Z 2 X 3 U 4 T 5 G 6 F 7 C 8 B 9 A 1 A 2 B 3 C 4 D Oracle的索引是以平衡树的方式组织存储的:保存的是索引列的值,以及该行的rowid的一部分( ...

  9. oracle 优化——索引与组合索引

    1.索引结构.第一张图是索引的官方图解,右侧是存储方式的图解. 图中很清晰的展示了索引存储的状况. 在leaf 节点中存储了一列,索引所对应项的 :值,rowId,长度,头信息(控制信息) 这样我们就 ...

随机推荐

  1. Eclipse Debug 调试

    Debug 调试 Java 程序 我们可以在 Package Explorer 视图调试 Java 程序,操作步骤如下: 鼠标右击包含 main 函数的 java 类 选择 Debug As > ...

  2. web开发之web 验证码--- webqq 机器人

    一步一步来做WebQQ机器人-(一)(验证码) http://www.cnblogs.com/lianmin/p/4231340.html http://www.cnblogs.com/liulun/ ...

  3. php 佛祖保佑 永无bug

    <pre name="code" class="java">/* _ooOoo_ o8888888o 88" . "88 (| ...

  4. 针对中科院java接口的用法和问题

    1.下载附加的中科院分词工具包(要下载的到我的博客里面免费下载就可以) 2.解压后会看到例如以下几个目录 3.把java工程导入eclipse中.点击import.再选择existing projec ...

  5. POJ 2479 Maximum sum(双向DP)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Des ...

  6. 2、easyUI-创建 CRUD可编辑dataGrid(表格)

    在介绍这节之前,我们先看一下效果图: 双击可以进入编辑

  7. Java前端Rsa公钥加密,后端Rsa私钥解密(支持字符和中文)

    Base64工具类,可以让rsa编码的乱码变成一串字符序列 package com.utils; import java.io.ByteArrayInputStream; import java.io ...

  8. [python数据结构] hashable, list, tuple, set, frozenset

    学习 cs212 unit4 时遇到了 tuple, list, set 同时使用的问题,并且进行了拼接.合并操作.于是我就被弄混了.所以在这里进行一下总结. hashable and unhasha ...

  9. Stolz–Cesàro theorem

    w http://planetmath.org/sites/default/files/texpdf/33795.pdf Stolz–Cesàro theorem - Wikipedia  https ...

  10. 利用Hibernate注解生成表

    转自:http://blog.csdn.net/madison__/article/details/55677099 Hibernate4注释 @Entity(name = "tbl_use ...