效果

实现效果图如下,根据过滤条件查询相关信息。

知识点

1.配置URL,在路由中使用正则表达式

2.过滤查询

代码

setting.py

 from django.contrib import admin
from django.urls import path,re_path,include
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('article-(\d+)-(\d+).html', views.article),
re_path(r'article-(?P<article_type_id>\d+)-(?P<category_id>\d+).html', views.article,name="article"), #正则过滤
]

models.py

 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=255) category = models.ForeignKey(Category,on_delete=models.CASCADE)
article_type = models.ForeignKey(ArticleType,on_delete=models.CASCADE)

view.py

 from django.shortcuts import render
from app01 import models
# Create your views here. def article(request,*args,**kwargs):
print(kwargs) # 打印所有参数
contition = {} # 定义一个空集合,接收参数
for k,v in kwargs.items():
kwargs[k] = int(v) # 由于参数类型都为str类型,所以该处进行转换
if v == '':
pass
else:
contition[k] = v # 当参数为非0时 进行接收
print(args)
article_type_list = models.ArticleType.objects.all()
category_list = models.Category.objects.all()
result = models.Article.objects.filter(**contition) # 过滤查询
return render(request,
"article.html",
{
'result':result,
'article_type_list':article_type_list,
'category_list':category_list,
'arg_dict':kwargs,
}
)

article.html

 <!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: darkgreen;
}
</style>
</head>
<body>
<h1>过滤条件</h1>
<div class="condition">
{% if arg_dict.article_type_id == 0 %}
<a class="active" href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
{% else %}
<a href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
{% endif %} {% 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 }}.html">{{ row.caption }}</a>
{% else %}
<a href="/article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
{% endif %}
{% endfor %} </div>
<div class="condition">
{% if arg_dict.category_id == 0 %}
<a class="active" href="/article-{{ arg_dict.article_type_id }}-0.html">全部</a>
{% else %}
<a href="/article-{{ arg_dict.article_type_id }}-0.html">全部</a>
{% endif %} {% for row in category_list %}
{% if row.id == arg_dict.category_id %}
<a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
{% else %}
<a href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
{% endif %} {% endfor %}
</div>
<h1>查询结果</h1>
<ul>
{% for row in result %}
<li>{{ row.id }}-{{ row.title }}</li>
{% endfor %}
</ul>
</body>
</html>

python&django 实现页面中关联查询小功能(基础篇)的更多相关文章

  1. python&django 实现页面中关联查询小功能(中级篇)

    目的 组合搜索实现如下图功能 知识点 1.使用自定义标签模板(templatetags) 实现 models.py 和 views.py和初级篇一样 重点如下:在app01目录下创建templatet ...

  2. Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>

    Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...

  3. python Django注册页面显示头像

    python Django注册页面显示头像(views) def register(request): ''' 注册 :param request: :return: ''' if request.m ...

  4. THINKPHP 中关联查询(多表查询)

    THINKPHP 中关联查询(多表查询)可以使用 table() 方法或和join方法,请看示例: 1.Table方法:定义要操作的数据表名称,可以动态改变当前操作的数据表名称,需要写数据表的全名,包 ...

  5. OAF在打开的新页面中添加按钮,功能是关闭当前页面

    OAF在打开的新页面中添加按钮,功能是关闭当前页面 javascript:close()

  6. SSM-MyBatis-15:Mybatis中关联查询(多表操作)

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 先简单提及一下关联查询的分类 1.一对多 1.1单条SQL操作的 1.2多条SQL操作的 2.多对一 2.1单 ...

  7. 关于python+django操作数据库中的表

    数据库中的表示这样设计的 class C(models.Model): name = models.CharField(max_length=32) class B(models.Model): na ...

  8. Mongoose中关联查询populate的使用

    MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...

  9. yii2中关联查询

    yii2 ActiveRecord多表关联以及多表关联搜索的实现 一个老生常谈的问题.最近通过群里的反馈,觉得很多人还是没有去理解这个问题.今天把这个问题讲明白了,看看yii2 ActiveRecor ...

随机推荐

  1. C# web项目利用docx文档作为模板~为打印专做的解决方案

    还是那句话:十年河东,十年河西,莫欺少年穷. 目前,web端打印技术有很多,有收费的专业web打印控件,大家可以参考我的上一篇博客.当然,很多公司不愿意出钱,那么今天咱们就探讨下怎么做免费的打印. w ...

  2. 创建pandas和sqlalchemy的j交互对象,方便于日常的数据库的增删改查(原创)

    #导入第三方库sqlalchemy的数据库引擎 from sqlalchemy import create_engine #导入科学计算库 import pandas as pd #导入绘图库 imp ...

  3. dataTable tab栏切换时错位解决办法

    做后台管理类网站肯定要写列表,首选dataTable,功能强大 最近在做一个tab栏切换时发现了一个很诡异的事情:表头错位了! 主要时因为当table被隐藏后,table的header宽度会计算错乱, ...

  4. hashlib 模块:加密

    import hashlib # 基本使用 cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8')) print(cipher.hexdigest() ...

  5. java篇 之 流程控制语句

    条件判断语句 条件语句: If(boolean类型) {} else {}    (打大括号避免出错) switch (export)语句:export的类型必须是 byte,short,char,i ...

  6. IFE第二天

    HTML是超文本标记语言,HTML5是下一代的HTML标准. HTML元素是组成HTML文档的部分,HTML属性为HTML元素提供附加信息. 文档类型<!DOCTYPE>声明帮助浏览器正确 ...

  7. [2019.03.20]Linux Shell 执行传参数和expr

    前不久入职实习生,现在在帮着组里面dalao们跑Case,时不时要上去收一下有木有Dump,每次敲命令太烦人于是逼着自己学写Shell脚本.一开始真的是很痛苦啊,也没能搞到书,只能凭网上半真半假的消息 ...

  8. Java 删除ArrayList中重复元素,保持顺序

    // 删除ArrayList中重复元素,保持顺序          public static List<Map<String, Object>> removeDuplicat ...

  9. pip模块

    pip模块 Usage: pip <command> [options] Commands: install Install packages. download Download pac ...

  10. Sublime Text3—设置快捷键打开浏览器

    在不同浏览器查看代码效果可谓是家常便饭,所以用不同快捷键对应打开不同浏览器可以大大提高工作效率. 本篇分享个简单的方法只需二步: 一.安装插件SideBarEnhancements ctrl+shif ...