• python&django 实现页面中关联查询小功能(基础篇)


    效果

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

     知识点

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

    2.过滤查询

    代码

    setting.py

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

    models.py

     1 from django.db import models
     2 
     3 # Create your models here.
     4 
     5 class Category(models.Model):
     6     caption = models.CharField(max_length=16)
     7 
     8 class ArticleType(models.Model):
     9     caption = models.CharField(max_length=16)
    10 
    11 class Article(models.Model):
    12     title = models.CharField(max_length=32)
    13     content = models.CharField(max_length=255)
    14 
    15 
    16     category = models.ForeignKey(Category,on_delete=models.CASCADE)
    17     article_type = models.ForeignKey(ArticleType,on_delete=models.CASCADE)

    view.py

     1 from django.shortcuts import render
     2 from app01 import models
     3 # Create your views here.
     4 
     5 
     6 def article(request,*args,**kwargs):
     7     print(kwargs) # 打印所有参数
     8     contition = {} # 定义一个空集合,接收参数
     9     for k,v in kwargs.items():
    10         kwargs[k] = int(v) # 由于参数类型都为str类型,所以该处进行转换
    11         if v == '0':
    12             pass
    13         else:
    14             contition[k] = v # 当参数为非0时 进行接收
    15     print(args)
    16     article_type_list = models.ArticleType.objects.all()
    17     category_list = models.Category.objects.all()
    18     result = models.Article.objects.filter(**contition) # 过滤查询
    19     return  render(request,
    20                    "article.html",
    21                    {
    22                        'result':result,
    23                        'article_type_list':article_type_list,
    24                        'category_list':category_list,
    25                        'arg_dict':kwargs,
    26                    }
    27                    )

    article.html

     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: darkgreen;
    15         }
    16     </style>
    17 </head>
    18 <body>
    19 <h1>过滤条件</h1>
    20 <div class="condition">
    21     {% if arg_dict.article_type_id == 0 %}
    22          <a  class="active" href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
    23     {% else %}
    24          <a href="/article-0-{{ arg_dict.category_id }}.html">全部</a>
    25     {% endif %}
    26 
    27 
    28     {% for row in article_type_list %}
    29         {% if row.id == arg_dict.article_type_id %}
    30         <a  class="active"  href="/article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
    31         {% else %}
    32         <a href="/article-{{ row.id }}-{{ arg_dict.category_id }}.html">{{ row.caption }}</a>
    33         {% endif %}
    34     {% endfor %}
    35 
    36 </div>
    37 <div class="condition">
    38     {% if arg_dict.category_id == 0 %}
    39         <a class="active" href="/article-{{ arg_dict.article_type_id }}-0.html">全部</a>
    40     {% else %}
    41         <a href="/article-{{ arg_dict.article_type_id }}-0.html">全部</a>
    42     {% endif %}
    43 
    44 
    45     {% for row in category_list %}
    46         {% if row.id == arg_dict.category_id %}
    47             <a class="active" href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
    48         {% else %}
    49             <a href="/article-{{ arg_dict.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
    50         {% endif %}
    51 
    52     {% endfor %}
    53 </div>
    54 <h1>查询结果</h1>
    55 <ul>
    56 {% for row in result %}
    57     <li>{{ row.id }}-{{ row.title }}</li>
    58 {% endfor %}
    59 </ul>
    60 </body>
    61 </html>
    View Code
  • 相关阅读:
    二、DBMS_JOB(用于安排和管理作业队列)
    Oracle 常用系统包
    DBMS_OUTPUT(用于输入和输出信息)
    C#获取当前主机硬件信息
    Centos安装mysql5.7
    Rsync安装和配置
    利用Docker编译Hadoop 3.1.0
    hadoop集群环境搭建
    axios请求、拦截器
    import时,什么时候使用花括号'{ }'
  • 原文地址:https://www.cnblogs.com/pengpengzhang/p/10499720.html
Copyright © 2020-2023  润新知