Python 进阶篇

到目前为止,当我们的程序涉及与数据库相关的操作时,我们通常会这样做:

[En]

So far, when our program involves database-related operations, we usually do this:

Python 进阶篇

Python 进阶篇
* 创建数据库,设计表结构和字段
Python 进阶篇
* 使用 MySQLdb 来连接数据库,并编写数据访问层代码
Python 进阶篇
* 业务逻辑层去调用数据访问层执行数据库操作
Python 进阶篇

Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
import MySQLdb
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def GetList(sql):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    cursor = db.cursor()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    cursor.execute(sql)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    data = cursor.fetchall()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    db.close()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    return data
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def GetSingle(sql):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    cursor = db.cursor()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    cursor.execute(sql)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    data = cursor.fetchone()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    db.close()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    return data
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
django为使用一种新的方式,即:关系对象映射(Object Relational Mapping,简称ORM)。
Python 进阶篇

Python 进阶篇
PHP:activerecord
Python 进阶篇

Python 进阶篇
Java:Hibernate
Python 进阶篇

Python 进阶篇
C#:Entity Framework
Python 进阶篇

Python 进阶篇
django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表。
Python 进阶篇

Python 进阶篇
一、创建表
Python 进阶篇

Python 进阶篇
1、基本结构
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.db import models
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class userinfo(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    name = models.CharField(max_length=30)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    email = models.EmailField()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    memo = models.TextField()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
字段类型
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
AutoField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - int自增列,必须填入参数 primary_key=True
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    BigAutoField(AutoField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - bigint自增列,必须填入参数 primary_key=True
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        注:当model中如果没有自增列,则自动会创建一个列名为id的列
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        from django.db import models
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        class UserInfo(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # 自动创建一个列名为id的且为自增的整数列
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            username = models.CharField(max_length=32)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        class Group(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # 自定义自增列
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            nid = models.AutoField(primary_key=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            name = models.CharField(max_length=32)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SmallIntegerField(IntegerField):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 小整数 -32768 ~ 32767
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    PositiveSmallIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 正小整数 0 ~ 32767
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    IntegerField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 整数列(有符号的) -2147483648 ~ 2147483647
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    PositiveIntegerField(PositiveIntegerRelDbTypeMixin, IntegerField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 正整数 0 ~ 2147483647
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    BigIntegerField(IntegerField):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 长整型(有符号的) -9223372036854775808 ~ 9223372036854775807
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    自定义无符号整数字段
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        class UnsignedIntegerField(models.IntegerField):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            def db_type(self, connection):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                return 'integer UNSIGNED'
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        PS: 返回值为字段在数据库中的属性,Django字段默认的值为:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'AutoField': 'integer AUTO_INCREMENT',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BigAutoField': 'bigint AUTO_INCREMENT',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BinaryField': 'longblob',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BooleanField': 'bool',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'CharField': 'varchar(%(max_length)s)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'DateField': 'date',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'DateTimeField': 'datetime',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'DurationField': 'bigint',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'FileField': 'varchar(%(max_length)s)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'FilePathField': 'varchar(%(max_length)s)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'FloatField': 'double precision',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'IntegerField': 'integer',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BigIntegerField': 'bigint',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'IPAddressField': 'char(15)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'GenericIPAddressField': 'char(39)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'NullBooleanField': 'bool',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'OneToOneField': 'integer',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'PositiveIntegerField': 'integer UNSIGNED',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'PositiveSmallIntegerField': 'smallint UNSIGNED',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'SlugField': 'varchar(%(max_length)s)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'SmallIntegerField': 'smallint',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'TextField': 'longtext',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'TimeField': 'time',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'UUIDField': 'char(32)',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    BooleanField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 布尔值类型
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    NullBooleanField(Field):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 可以为空的布尔值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CharField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符类型
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 必须提供max_length参数, max_length表示字符长度
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    TextField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 文本类型
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    EmailField(CharField):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串类型,Django Admin以及ModelForm中提供验证机制
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    IPAddressField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串类型,Django Admin以及ModelForm中提供验证 IPV4 机制
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    GenericIPAddressField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串类型,Django Admin以及ModelForm中提供验证 Ipv4和Ipv6
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 参数:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            protocol,用于指定Ipv4或Ipv6, 'both',"ipv4","ipv6"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            unpack_ipv4, 如果指定为True,则输入::ffff:192.0.2.1时候,可解析为192.0.2.1,开启刺功能,需要protocol="both"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    URLField(CharField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串类型,Django Admin以及ModelForm中提供验证 URL
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SlugField(CharField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串类型,Django Admin以及ModelForm中提供验证支持 字母、数字、下划线、连接符(减号)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CommaSeparatedIntegerField(CharField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串类型,格式必须为逗号分割的数字
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    UUIDField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串类型,Django Admin以及ModelForm中提供对UUID格式的验证
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    FilePathField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串,Django Admin以及ModelForm中提供读取文件夹下文件的功能
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 参数:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                path,                      文件夹路径
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                match=None,                正则匹配
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                recursive=False,           递归下面的文件夹
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                allow_files=True,          允许文件
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                allow_folders=False,       允许文件夹
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    FileField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串,路径保存在数据库,文件上传到指定目录
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 参数:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            upload_to = ""      上传文件的保存路径
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            storage = None      存储组件,默认django.core.files.storage.FileSystemStorage
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    ImageField(FileField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 字符串,路径保存在数据库,文件上传到指定目录
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 参数:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            upload_to = ""      上传文件的保存路径
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            storage = None      存储组件,默认django.core.files.storage.FileSystemStorage
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            width_field=None,   上传图片的高度保存的数据库字段名(字符串)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            height_field=None   上传图片的宽度保存的数据库字段名(字符串)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    DateTimeField(DateField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 日期+时间格式 YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    DateField(DateTimeCheckMixin, Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 日期格式      YYYY-MM-DD
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    TimeField(DateTimeCheckMixin, Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 时间格式      HH:MM[:ss[.uuuuuu]]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    DurationField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 长整数,时间间隔,数据库中按照bigint存储,ORM中获取的值为datetime.timedelta类型
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    FloatField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 浮点型
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    DecimalField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 10进制小数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 参数:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            max_digits,小数总长度
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            decimal_places,小数位长度
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    BinaryField(Field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        - 二进制类型
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
字段选项
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
null                数据库中字段是否可以为空
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    db_column           数据库中字段的列名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    db_tablespace
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    default             数据库中字段的默认值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    primary_key         数据库中字段是否为主键
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    db_index            数据库中字段是否可以建立索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    unique              数据库中字段是否可以建立唯一索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    unique_for_date     数据库中字段【日期】部分是否可以建立唯一索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    unique_for_month    数据库中字段【月】部分是否可以建立唯一索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    unique_for_year     数据库中字段【年】部分是否可以建立唯一索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    verbose_name        Admin中显示的字段名称
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    blank               Admin中是否允许用户输入为空
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    editable            Admin中是否可以编辑
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    help_text           Admin中该字段的提示信息
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    choices             Admin中显示选择框的内容,用不变动的数据放在内存中从而避免跨表操作
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                        如:gf = models.IntegerField(choices=[(0, '何穗'),(1, '大表姐'),],default=1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    error_messages      自定义错误信息(字典类型),从而定制想要显示的错误信息;
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                        字典健:null, blank, invalid, invalid_choice, unique, and unique_for_date
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                        如:{'null': "不能为空.", 'invalid': '格式错误'}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    validators          自定义错误验证(列表类型),从而定制想要的验证规则
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                        from django.core.validators import RegexValidator
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                        from django.core.validators import EmailValidator,URLValidator,DecimalValidator,\
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                        MaxLengthValidator,MinLengthValidator,MaxValueValidator,MinValueValidator
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                        如:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            test = models.CharField(
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                max_length=32,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                error_messages={
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    'c1': '优先错信息1',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    'c2': '优先错信息2',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    'c3': '优先错信息3',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                },
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                validators=[
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    RegexValidator(regex='root_\d+', message='错误了', code='c1'),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    RegexValidator(regex='root_112233\d+', message='又错误了', code='c2'),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    EmailValidator(message='又错误了', code='c3'), ]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            )
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
Meta
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class UserInfo(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        nid = models.AutoField(primary_key=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        username = models.CharField(max_length=32)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        class Meta:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # 数据库中生成的表名称 默认 app名称 + 下划线 + 类名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            db_table = "table_name"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # 联合索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            index_together = [
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                ("pub_date", "deadline"),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            ]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # 联合唯一索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            unique_together = (("driver", "restaurant"),)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # admin中显示的表名称
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            verbose_name
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # verbose_name加s
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            verbose_name_plural
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    更多:https://docs.djangoproject.com/en/1.10/ref/models/options/
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
1.触发Model中的验证和错误提示有两种方式:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        a. Django Admin中的错误信息会优先根据Admiin内部的ModelForm错误信息提示,如果都成功,才来检查Model的字段并显示指定错误信息
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        b. 调用Model对象的 clean_fields 方法,如:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # models.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            class UserInfo(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                nid = models.AutoField(primary_key=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                username = models.CharField(max_length=32)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                email = models.EmailField(error_messages={'invalid': '格式错了.'})
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # views.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            def index(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                obj = models.UserInfo(username='11234', email='uu')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                try:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                    print(obj.clean_fields())
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                except Exception as e:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                    print(e)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                return HttpResponse('ok')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
           # Model的clean方法是一个钩子,可用于定制操作,如:上述的异常处理。
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    2.Admin中修改错误提示
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # admin.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        from django.contrib import admin
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        from model_club import models
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        from django import forms
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        class UserInfoForm(forms.ModelForm):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            username = forms.CharField(error_messages={'required': '用户名不能为空.'})
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            email = forms.EmailField(error_messages={'invalid': '邮箱格式错误.'})
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            age = forms.IntegerField(initial=1, error_messages={'required': '请输入数值.', 'invalid': '年龄必须为数值.'})
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            class Meta:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                model = models.UserInfo
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                # fields = ('username',)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                fields = "__all__"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        class UserInfoAdmin(admin.ModelAdmin):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            form = UserInfoForm
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        admin.site.register(models.UserInfo, UserInfoAdmin)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、连表结构
Python 进阶篇

Python 进阶篇
* 一对多:models.ForeignKey(其他表)
Python 进阶篇
* 多对多:models.ManyToManyField(其他表)
Python 进阶篇
* 一对一:models.OneToOneField(其他表)
Python 进阶篇

Python 进阶篇

应用场景:

Python 进阶篇

Python 进阶篇
* 一对多:在表格中创建一行数据时,有一个下拉框(可重复选择)
[En]

one-to-many: when a row of data is created in a table, there is a single drop-down box (which can be repeatedly selected)

Python 进阶篇
例如:创建用户信息时候,需要选择一个用户类型【普通用户】【金牌用户】【铂金用户】等。
Python 进阶篇
* 多对多:在某表中创建一行数据是,有一个可以多选的下拉框
Python 进阶篇
例如:创建用户信息,需要为用户指定多个爱好
Python 进阶篇
* 一对一:在表格中创建一行数据时,只有一个下拉框(下拉框中的内容在使用一次后消失
[En]

one-to-one: when you create a row of data in a table, there is a single drop-down box (the contents of the drop-down box disappear after being used once

Python 进阶篇
例如:原有含 10列数据的 一张表保存相关信息,经过一段时间之后,10列无法满足需求,需要为原来的表再添加5列数据
Python 进阶篇

Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
ForeignKey(ForeignObject) # ForeignObject(RelatedField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        to,                         # 要进行关联的表名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        to_field=None,              # 要关联的表中的字段名称
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        on_delete=None,             # 当删除关联表中的数据时,当前表与其关联的行的行为
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        - models.CASCADE,删除关联数据,与之关联也删除
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        - models.DO_NOTHING,删除关联数据,引发错误IntegrityError
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        - models.PROTECT,删除关联数据,引发错误ProtectedError
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        - models.SET_NULL,删除关联数据,与之关联的值设置为null(前提FK字段需要设置为可空)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        - models.SET_DEFAULT,删除关联数据,与之关联的值设置为默认值(前提FK字段需要设置默认值)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        - models.SET,删除关联数据,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                      a. 与之关联的值设置为指定值,设置:models.SET(值)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                      b. 与之关联的值设置为可执行对象的返回值,设置:models.SET(可执行对象)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                        def func():
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                            return 10
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                        class MyModel(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                            user = models.ForeignKey(
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                                to="User",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                                to_field="id"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                                on_delete=models.SET(func),)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        related_name=None,          # 反向操作时,使用的字段名,用于代替 【表名_set】 如: obj.表名_set.all()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        related_query_name=None,    # 反向操作时,使用的连接前缀,用于替换【表名】     如: models.UserGroup.objects.filter(表名__字段名=1).values('表名__字段名')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        limit_choices_to=None,      # 在Admin或ModelForm中显示关联数据时,提供的条件:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    # 如:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to={'nid__gt': 5}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=lambda : {'nid__gt': 5}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            from django.db.models import Q
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=Q(nid__gt=10)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=Q(nid=8) | Q(nid__gt=10)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=lambda : Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption='root')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        db_constraint=True          # 是否在数据库中创建外键约束
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        parent_link=False           # 在Admin中是否显示关联数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    OneToOneField(ForeignKey)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        to,                         # 要进行关联的表名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        to_field=None               # 要关联的表中的字段名称
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        on_delete=None,             # 当删除关联表中的数据时,当前表与其关联的行的行为
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    ###### 对于一对一 ######
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    # 1. 一对一其实就是 一对多 + 唯一索引
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    # 2.当两个类之间有继承关系时,默认会创建一个一对一字段
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    # 如下会在A表中额外增加一个c_ptr_id列且唯一:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            class C(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                nid = models.AutoField(primary_key=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                part = models.CharField(max_length=12)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            class A(C):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                id = models.AutoField(primary_key=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                code = models.CharField(max_length=1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    ManyToManyField(RelatedField)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        to,                         # 要进行关联的表名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        related_name=None,          # 反向操作时,使用的字段名,用于代替 【表名_set】 如: obj.表名_set.all()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        related_query_name=None,    # 反向操作时,使用的连接前缀,用于替换【表名】     如: models.UserGroup.objects.filter(表名__字段名=1).values('表名__字段名')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        limit_choices_to=None,      # 在Admin或ModelForm中显示关联数据时,提供的条件:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    # 如:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to={'nid__gt': 5}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=lambda : {'nid__gt': 5}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            from django.db.models import Q
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=Q(nid__gt=10)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=Q(nid=8) | Q(nid__gt=10)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            - limit_choices_to=lambda : Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption='root')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        symmetrical=None,           # 仅用于多对多自关联时,symmetrical用于指定内部是否创建反向操作的字段
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                    # 做如下操作时,不同的symmetrical会有不同的可选字段
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        models.BB.objects.filter(...)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        # 可选字段有:code, id, m1
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            class BB(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            code = models.CharField(max_length=12)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            m1 = models.ManyToManyField('self',symmetrical=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        # 可选字段有: bb, code, id, m1
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            class BB(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            code = models.CharField(max_length=12)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            m1 = models.ManyToManyField('self',symmetrical=False)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        through=None,               # 自定义第三张表时,使用字段用于指定关系表
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        through_fields=None,        # 自定义第三张表时,使用字段用于指定关系表中那些字段做多对多关系表
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        from django.db import models
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        class Person(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            name = models.CharField(max_length=50)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        class Group(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            name = models.CharField(max_length=128)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            members = models.ManyToManyField(
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                Person,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                through='Membership',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                through_fields=('group', 'person'),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            )
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                        class Membership(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            group = models.ForeignKey(Group, on_delete=models.CASCADE)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            person = models.ForeignKey(Person, on_delete=models.CASCADE)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            inviter = models.ForeignKey(
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                Person,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                on_delete=models.CASCADE,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                related_name="membership_invites",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            )
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            invite_reason = models.CharField(max_length=64)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        db_constraint=True,         # 是否在数据库中创建外键约束
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        db_table=None,              # 默认创建第三张表时,数据库中表的名称
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
字段以及参数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
二、操作表
Python 进阶篇

Python 进阶篇
1、基本操作
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 增
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.create(c1='xx', c2='oo')  增加一条数据,可以接受字典类型数据 **kwargs
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # obj = models.Tb1(c1='xx', c2='oo')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # obj.save()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 查
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.get(id=123)         # 获取单条数据,不存在则报错(不建议)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.all()               # 获取全部
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.filter(name='seven') # 获取指定条件的数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 删
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.filter(name='seven').delete() # 删除指定条件的数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 改
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.filter(name='seven').update(gender='0')  # 将指定条件的数据更新,均支持 **kwargs
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # obj = models.Tb1.objects.get(id=1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # obj.c1 = '111'
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # obj.save()                                                 # 修改单条数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、进阶操作(了不起的双下划线)
Python 进阶篇

Python 进阶篇
利用双下划线将字段和对应的操作连接起来
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 获取个数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(name='seven').count()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 大于,小于
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(id__gt=1)              # 获取id大于1的值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(id__gte=1)              # 获取id大于等于1的值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(id__lt=10)             # 获取id小于10的值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(id__lte=10)             # 获取id小于10的值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(id__lt=10, id__gt=1)   # 获取id大于1 且 小于10的值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # in
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(id__in=[11, 22, 33])   # 获取id等于11、22、33的数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.exclude(id__in=[11, 22, 33])  # not in
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # isnull
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__isnull=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # contains
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(name__contains="ven")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.exclude(name__icontains="ven")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # range
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(id__range=[1, 2])   # 范围bettwen and
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 其他类似
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # startswith,istartswith, endswith, iendswith,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # order by
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(name='seven').order_by('id')    # asc
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(name='seven').order_by('-id')   # desc
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # group by
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # from django.db.models import Count, Min, Max, Sum
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # limit 、offset
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # models.Tb1.objects.all()[10:20]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # regex正则匹配,iregex 不区分大小写
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.get(title__regex=r'^(An?|The) +')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.get(title__iregex=r'^(an?|the) +')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # date
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # year
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__year=2005)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__year__gte=2005)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # month
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__month=12)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__month__gte=6)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # day
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__day=3)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__day__gte=3)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # week_day
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__week_day=2)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Entry.objects.filter(pub_date__week_day__gte=2)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # hour
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(timestamp__hour=23)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(time__hour=5)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(timestamp__hour__gte=12)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # minute
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(timestamp__minute=29)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(time__minute=46)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(timestamp__minute__gte=29)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # second
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(timestamp__second=31)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(time__second=2)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # Event.objects.filter(timestamp__second__gte=31)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
进阶操作
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
3、其他操作
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# extra
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #    Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #    Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #    Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"])
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #    Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid'])
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # F
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # from django.db.models import F
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.update(num=F('num')+1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # Q
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 方式一:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # Q(nid__gt=10)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # Q(nid=8) | Q(nid__gt=10)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption='root')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 方式二:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # con = Q()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q1 = Q()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q1.connector = 'OR'
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q1.children.append(('id', 1))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q1.children.append(('id', 10))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q1.children.append(('id', 9))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q2 = Q()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q2.connector = 'OR'
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q2.children.append(('c1', 1))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q2.children.append(('c1', 10))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # q2.children.append(('c1', 9))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # con.add(q1, 'AND')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # con.add(q2, 'AND')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # models.Tb1.objects.filter(con)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 执行原生SQL
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # from django.db import connection, connections
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # cursor = connection.cursor()  # cursor = connections['default'].cursor()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # cursor.execute("""SELECT * from auth_user where id = %s""", [1])
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # row = cursor.fetchone()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
4、连表操作(了不起的双下划线)
Python 进阶篇

Python 进阶篇
利用双下划线和 _set 将表之间的操作连接起来
Python 进阶篇

Python 进阶篇
model
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class UserProfile(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    user_info = models.OneToOneField('UserInfo')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    username = models.CharField(max_length=64)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    password = models.CharField(max_length=64)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def __unicode__(self):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return self.username
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class UserInfo(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    user_type_choice = (
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        (0, u'普通用户'),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        (1, u'高级用户'),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    )
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    user_type = models.IntegerField(choices=user_type_choice)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    name = models.CharField(max_length=32)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    email = models.CharField(max_length=32)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    address = models.CharField(max_length=128)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def __unicode__(self):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return self.name
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class UserGroup(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    caption = models.CharField(max_length=64)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    user_info = models.ManyToManyField('UserInfo')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def __unicode__(self):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return self.caption
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class Host(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    hostname = models.CharField(max_length=64)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    ip = models.GenericIPAddressField()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    user_group = models.ForeignKey('UserGroup')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def __unicode__(self):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return self.hostname
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
user_info_obj = models.UserInfo.objects.filter(id=1).first()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
print user_info_obj.user_type
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
print user_info_obj.get_user_type_display()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
print user_info_obj.userprofile.password
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
user_info_obj = models.UserInfo.objects.filter(id=1).values('email', 'userprofile__username').first()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
print user_info_obj.keys()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
print user_info_obj.values()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
类似一对一
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
1、搜索条件使用 __ 连接
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
2、获取值时使用 . 连接增删改查
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
user_info_obj = models.UserInfo.objects.get(name=u'武沛齐')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
user_info_objs = models.UserInfo.objects.all()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
group_obj = models.UserGroup.objects.get(caption='CEO')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
group_objs = models.UserGroup.objects.all()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 添加数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#group_obj.user_info.add(user_info_obj)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#group_obj.user_info.add(*user_info_objs)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 删除数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#group_obj.user_info.remove(user_info_obj)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#group_obj.user_info.remove(*user_info_objs)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 添加数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#user_info_obj.usergroup_set.add(group_obj)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#user_info_obj.usergroup_set.add(*group_objs)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 删除数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#user_info_obj.usergroup_set.remove(group_obj)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#user_info_obj.usergroup_set.remove(*group_objs)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 获取数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#print group_obj.user_info.all()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#print group_obj.user_info.all().filter(id=1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 获取数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#print user_info_obj.usergroup_set.all()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#print user_info_obj.usergroup_set.all().filter(caption='CEO')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#print user_info_obj.usergroup_set.all().filter(caption='DBA')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
扩展:
Python 进阶篇

Python 进阶篇
a、自定义上传
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def upload_file(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if request.method == "POST":
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        obj = request.FILES.get('fafafa')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        f = open(obj.name, 'wb')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        for chunk in obj.chunks():
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            f.write(chunk)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        f.close()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    return render(request, 'file.html')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
b、Form上传文件实例
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class FileForm(forms.Form):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    ExcelFile = forms.FileField()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.db import models
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class UploadFile(models.Model):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    userid = models.CharField(max_length = 30)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    file = models.FileField(upload_to = './upload/')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    date = models.DateTimeField(auto_now_add=True)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def UploadFile(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    uf = AssetForm.FileForm(request.POST,request.FILES)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if uf.is_valid():
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            upload = models.UploadFile()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            upload.userid = 1
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            upload.file = uf.cleaned_data['ExcelFile']
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            upload.save()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            print upload.file
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇

Form

Python 进阶篇

Python 进阶篇
django中的Form一般有两种功能:
Python 进阶篇

Python 进阶篇
* 输入html
Python 进阶篇
* 验证用户输入
Python 进阶篇

Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#!/usr/bin/env python
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# -*- coding:utf-8 -*-
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
import re
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django import forms
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.core.exceptions import ValidationError
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def mobile_validate(value):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if not mobile_re.match(value):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        raise ValidationError('手机号码格式错误')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class PublishForm(forms.Form):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    user_type_choice = (
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        (0, u'普通用户'),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        (1, u'高级用户'),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    )
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    user_type = forms.IntegerField(widget=forms.widgets.Select(choices=user_type_choice,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                                  attrs={'class': "form-control"}))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    title = forms.CharField(max_length=20,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            min_length=5,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            error_messages={'required': u'标题不能为空',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            'min_length': u'标题最少为5个字符',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                            'max_length': u'标题最多为20个字符'},
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            widget=forms.TextInput(attrs={'class': "form-control",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                          'placeholder': u'标题5-20个字符'}))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    memo = forms.CharField(required=False,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                           max_length=256,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                           widget=forms.widgets.Textarea(attrs={'class': "form-control no-radius", 'placeholder': u'详细描述', 'rows': 3}))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    phone = forms.CharField(validators=[mobile_validate, ],
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            error_messages={'required': u'手机不能为空'},
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            widget=forms.TextInput(attrs={'class': "form-control",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                                                          'placeholder': u'手机号码'}))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    email = forms.EmailField(required=False,
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            error_messages={'required': u'邮箱不能为空','invalid': u'邮箱格式错误'},
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                            widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': u'邮箱'}))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def publish(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    ret = {'status': False, 'data': '', 'error': '', 'summary': ''}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if request.method == 'POST':
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request_form = PublishForm(request.POST)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        if request_form.is_valid():
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            request_dict = request_form.clean()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            print request_dict
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            ret['status'] = True
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            error_msg = request_form.errors.as_json()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            ret['error'] = json.loads(error_msg)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    return HttpResponse(json.dumps(ret))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
扩展:ModelForm
Python 进阶篇

Python 进阶篇
在使用Model和Form时,都需要对字段进行定义并指定类型,通过ModelForm则可以省去From中字段的定义
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class AdminModelForm(forms.ModelForm):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    class Meta:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        model = models.Admin
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #fields = '__all__'
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        fields = ('username', 'email')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        widgets = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'email' : forms.PasswordInput(attrs={'class':"alex"}),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇

跨站请求伪造

Python 进阶篇

Python 进阶篇
一、简介
Python 进阶篇

Python 进阶篇
django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成。而对于django中设置防跨站请求伪造功能有分为全局和局部。
Python 进阶篇

Python 进阶篇
全局:
Python 进阶篇

Python 进阶篇
中间件 django.middleware.csrf.CsrfViewMiddleware
Python 进阶篇

Python 进阶篇
局部:
Python 进阶篇

Python 进阶篇
* @csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
Python 进阶篇
* @csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。
Python 进阶篇

Python 进阶篇
注:from django.views.decorators.csrf import csrf_exempt,csrf_protect
Python 进阶篇

Python 进阶篇
二、应用
Python 进阶篇

Python 进阶篇
1、普通表单
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
veiw中设置返回值:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
  return render_to_response('Account/Login.html',data,context_instance=RequestContext(request))  
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
     或者
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
     return render(request, 'xxx.html', data)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
html中设置Token:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
  {% csrf_token %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、Ajax
Python 进阶篇

Python 进阶篇
对于传统的form,可以通过表单的方式将token再次发送到服务端,而对于ajax的话,使用如下方式。
Python 进阶篇

Python 进阶篇
view.py
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.template.context import RequestContext
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# Create your views here.
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def test(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if request.method == 'POST':
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        print request.POST
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return HttpResponse('ok')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    return  render_to_response('app01/test.html',context_instance=RequestContext(request))
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
text.html
Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
"en">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    "UTF-8">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% csrf_token %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    "button" onclick="Do();"  value="Do it"/>
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
更多:https://docs.djangoproject.com/en/dev/ref/csrf/#ajax
Python 进阶篇

Python 进阶篇

Cookie

Python 进阶篇

Python 进阶篇
1、获取Cookie:
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
request.COOKIES['key']
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
request.get_signed_cookie(key, default=RAISE_ERROR, salt='', max_age=None)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    参数:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        default: 默认值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
           salt: 加密盐
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        max_age: 后台控制过期时间
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、设置Cookie:
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
rep = HttpResponse(...) 或 rep = render(request, ...)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
rep.set_cookie(key,value,...)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
rep.set_signed_cookie(key,value,salt='加密盐',...)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    参数:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        key,              键
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        value='',         值
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        max_age=None,     超时时间
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        expires=None,     超时时间(IE requires expires, so set it if hasn't been already.)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        path='/',         Cookie生效的路径,/ 表示根路径,特殊的:跟路径的cookie可以被任何url的页面访问
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        domain=None,      Cookie生效的域名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        secure=False,     https传输
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        httponly=False    只能http协议传输,无法被JavaScript获取(不是绝对,底层抓包可以获取到也可以被覆盖)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
由于cookie保存在客户端的电脑上,所以,JavaScript和jquery也可以操作cookie。
Python 进阶篇
Python 进阶篇Python 进阶篇

Python 进阶篇
$.cookie("list_pager_num", 30,{ path: '/' });

View Code

Python 进阶篇

Python 进阶篇

Session

Python 进阶篇

Python 进阶篇
Django中默认支持Session,其内部提供了5种类型的Session供开发者使用:
Python 进阶篇

Python 进阶篇
* 数据库(默认)
Python 进阶篇
* 缓存
Python 进阶篇
* 文件
Python 进阶篇
* 缓存+数据库
Python 进阶篇
* 加密cookie
Python 进阶篇

Python 进阶篇
1、数据库Session
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
Django默认支持Session,并且默认是将Session数据存储在数据库中,即:django_session 表中。
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
a. 配置 settings.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_ENGINE = 'django.contrib.sessions.backends.db'   # 引擎(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_NAME = "sessionid"                       # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_PATH = "/"                               # Session的cookie保存的路径(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_DOMAIN = None                             # Session的cookie保存的域名(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_SECURE = False                            # 是否Https传输cookie(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_HTTPONLY = True                           # 是否Session的cookie只支持http传输(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_AGE = 1209600                             # Session的cookie失效日期(2周)(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_EXPIRE_AT_BROWSER_CLOSE = False                  # 是否关闭浏览器使得Session过期(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_SAVE_EVERY_REQUEST = False                       # 是否每次请求都保存Session,默认修改之后才保存(默认)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
b. 使用
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def index(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 获取、设置、删除Session中数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session['k1']
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.get('k1',None)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session['k1'] = 123
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.setdefault('k1',123) # 存在则不设置
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        del request.session['k1']
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 所有 键、值、键值对
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.keys()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.values()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.items()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.iterkeys()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.itervalues()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.iteritems()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 用户session的随机字符串
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.session_key
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 将所有Session失效日期小于当前日期的数据删除
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.clear_expired()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 检查 用户session的随机字符串 在数据库中是否
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.exists("session_key")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 删除当前用户的所有Session数据
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.delete("session_key")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        request.session.set_expiry(value)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            * 如果value是个整数,session会在些秒数后失效。
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            * 如果value是个datatime或timedelta,session就会在这个时间后失效。
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            * 如果value是0,用户关闭浏览器session就会失效。
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            * 如果value是None,session会依赖全局session失效策略。
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、缓存Session
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
a. 配置 settings.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_ENGINE = 'django.contrib.sessions.backends.cache'  # 引擎
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_CACHE_ALIAS = 'default'                            # 使用的缓存别名(默认内存缓存,也可以是memcache),此处别名依赖缓存的设置
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_NAME = "sessionid"                        # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_PATH = "/"                                # Session的cookie保存的路径
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_DOMAIN = None                              # Session的cookie保存的域名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_SECURE = False                             # 是否Https传输cookie
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_HTTPONLY = True                            # 是否Session的cookie只支持http传输
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_AGE = 1209600                              # Session的cookie失效日期(2周)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_EXPIRE_AT_BROWSER_CLOSE = False                   # 是否关闭浏览器使得Session过期
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_SAVE_EVERY_REQUEST = False                        # 是否每次请求都保存Session,默认修改之后才保存
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
b. 使用
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    同上
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
3、文件Session
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
a. 配置 settings.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_ENGINE = 'django.contrib.sessions.backends.file'    # 引擎
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_FILE_PATH = None                                    # 缓存文件路径,如果为None,则使用tempfile模块获取一个临时地址tempfile.gettempdir()                                                            # 如:/var/folders/d3/j9tj0gz93dg06bmwxmhh6_xm0000gn/T
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_NAME = "sessionid"                          # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_PATH = "/"                                  # Session的cookie保存的路径
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_DOMAIN = None                                # Session的cookie保存的域名
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_SECURE = False                               # 是否Https传输cookie
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_HTTPONLY = True                              # 是否Session的cookie只支持http传输
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_COOKIE_AGE = 1209600                                # Session的cookie失效日期(2周)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_EXPIRE_AT_BROWSER_CLOSE = False                     # 是否关闭浏览器使得Session过期
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_SAVE_EVERY_REQUEST = False                          # 是否每次请求都保存Session,默认修改之后才保存
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
b. 使用
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    同上
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
4、缓存+数据库Session
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
数据库用于做持久化,缓存用于提高效率
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
a. 配置 settings.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'        # 引擎
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
b. 使用
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    同上
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
5、加密cookie Session
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
a. 配置 settings.py
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'   # 引擎
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
b. 使用
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    同上
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
更多参考:猛击这里猛击这里
Python 进阶篇

Python 进阶篇
扩展:Session用户验证
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def login(func):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def wrap(request, *args, **kwargs):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 如果未登陆,跳转到指定页面
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        if request.path == '/test/':
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            return redirect('http://www.baidu.com')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return func(request, *args, **kwargs)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    return wrap
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇

分页

Python 进阶篇

Python 进阶篇
一、Django内置分页
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.shortcuts import render
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
L = []
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
for i in range(999):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    L.append(i)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def index(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    current_page = request.GET.get('p')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    paginator = Paginator(L, 10)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # per_page: 每页显示条目数量
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # count:    数据总个数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # num_pages:总页数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # page_range:总页数的索引范围,如: (1,10),(1,200)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # page:     page对象
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    try:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        posts = paginator.page(current_page)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # has_next              是否有下一页
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # next_page_number      下一页页码
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # has_previous          是否有上一页
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # previous_page_number  上一页页码
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # object_list           分页之后的数据列表
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # number                当前页
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # paginator             paginator对象
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    except PageNotAnInteger:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        posts = paginator.page(1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    except EmptyPage:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        posts = paginator.page(paginator.num_pages)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/posts})
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
"en">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    "UTF-8">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% for item in posts %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        {{ item }}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% endfor %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class="pagination">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
      class="step-links">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        {% if posts.has_previous %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            "?p={{ posts.previous_page_number }}">Previous
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        {% endif %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
          class="current">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
          {% if posts.has_next %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
              "?p={{ posts.next_page_number }}">Next
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
          {% endif %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
from django.shortcuts import render
Python 进阶篇
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
Python 进阶篇

Python 进阶篇
class CustomPaginator(Paginator):Python 进阶篇 def init(self, current_page, max_pager_num, args, kwargs):Python 进阶篇 """Python 进阶篇 :param current_page: 当前页Python 进阶篇 :param max_pager_num:最多显示的页码个数Python 进阶篇 :param args:Python 进阶篇 :param kwargs:Python 进阶篇 :return:Python 进阶篇 """Python 进阶篇 self.current_page = int(current_page)Python 进阶篇 self.max_pager_num = max_pager_numPython 进阶篇 super(CustomPaginator, self).init(args, **kwargs)Python 进阶篇

Python 进阶篇
def page_num_range(self):Python 进阶篇 # 当前页面Python 进阶篇 # self.current_pagePython 进阶篇 # 总页数Python 进阶篇 # self.num_pagesPython 进阶篇 # 最多显示的页码个数Python 进阶篇 # self.max_pager_numPython 进阶篇 print(1)Python 进阶篇 if self.num_pages < self.max_pager_num:Python 进阶篇 return range(1, self.num_pages + 1)Python 进阶篇 print(2)Python 进阶篇 part = int(self.max_pager_num / 2)Python 进阶篇 if self.current_page - part < 1:Python 进阶篇 return range(1, self.max_pager_num + 1)Python 进阶篇 print(3)Python 进阶篇 if self.current_page + part > self.num_pages:Python 进阶篇 return range(self.num_pages + 1 - self.max_pager_num, self.num_pages + 1)Python 进阶篇 print(4)Python 进阶篇 return range(self.current_page - part, self.current_page + part + 1)Python 进阶篇

Python 进阶篇L
= []Python 进阶篇for i in range(999):Python 进阶篇 L.append(i)Python 进阶篇

Python 进阶篇
def index(request):Python 进阶篇 current_page = request.GET.get('p')Python 进阶篇 paginator = CustomPaginator(current_page, 11, L, 10)Python 进阶篇 # per_page: 每页显示条目数量Python 进阶篇 # count: 数据总个数Python 进阶篇 # num_pages:总页数Python 进阶篇 # page_range:总页数的索引范围,如: (1,10),(1,200)Python 进阶篇 # page: page对象Python 进阶篇 try:Python 进阶篇 posts = paginator.page(current_page)Python 进阶篇 # has_next 是否有下一页Python 进阶篇 # next_page_number 下一页页码Python 进阶篇 # has_previous 是否有上一页Python 进阶篇 # previous_page_number 上一页页码Python 进阶篇 # object_list 分页之后的数据列表Python 进阶篇 # number 当前页Python 进阶篇 # paginator paginator对象Python 进阶篇 except PageNotAnInteger:Python 进阶篇 posts = paginator.page(1)Python 进阶篇 except EmptyPage:Python 进阶篇 posts = paginator.page(paginator.num_pages)Python 进阶篇

Python 进阶篇Python 进阶篇Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
"en">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    "UTF-8">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% for item in posts %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        {{ item }}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% endfor %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class="pagination">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class="step-links">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
{% if posts.has_previous %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    "?p={{ posts.previous_page_number }}">Previous
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
{% endif %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% for i in posts.paginator.page_num_range %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        "?p={{ i }}">{{ i }}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% endfor %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% if posts.has_next %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        "?p={{ posts.next_page_number }}">Next
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    {% endif %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class="current">
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
二、自定义分页
Python 进阶篇

Python 进阶篇
分页对于每个站点都是必要的,而对于分页,它是根据用户的输入计算数据库表中的起始位置。
[En]

Paging is necessary for every site, and for paging, it is to calculate the starting position in the database table based on the user's input.

Python 进阶篇

Python 进阶篇
1、设定每页显示数据条数
Python 进阶篇

Python 进阶篇
2、用户输入页码(第一页、第二页...)
Python 进阶篇

Python 进阶篇
3、根据设定的每页显示条数和当前页码,计算出需要取数据表的起始位置
Python 进阶篇

Python 进阶篇
4、在数据表中根据起始位置取值,页面上输出数据
Python 进阶篇

Python 进阶篇
需求又来了,需要在页面上显示分页的页面。如:[上一页][1][2][3][4][5][下一页]
Python 进阶篇

Python 进阶篇
1、设定每页显示数据条数
Python 进阶篇

Python 进阶篇
2、用户输入页码(第一页、第二页...)
Python 进阶篇

Python 进阶篇
3、设定显示多少页号
Python 进阶篇

Python 进阶篇
4、获取当前数据总条数
Python 进阶篇

Python 进阶篇
5、根据设定显示多少页号和数据总条数计算出,总页数
Python 进阶篇

Python 进阶篇
6、根据设定的每页显示条数和当前页码,计算出需要取数据表的起始位置
Python 进阶篇

Python 进阶篇
7、在数据表中根据起始位置取值,页面上输出数据
Python 进阶篇

Python 进阶篇
8、输出分页html,如:[上一页][1][2][3][4][5][下一页]
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#!/usr/bin/env python
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# _*_coding:utf-8_*_
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.utils.safestring import mark_safe
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class PageInfo(object):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def __init__(self,current,totalItem,peritems=5):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        self.__current=current
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        self.__peritems=peritems
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        self.__totalItem=totalItem
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def From(self):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return (self.__current-1)*self.__peritems
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def To(self):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return self.__current*self.__peritems
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def TotalPage(self):  #总页数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        result=divmod(self.__totalItem,self.__peritems)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        if result[1]==0:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            return result[0]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            return result[0]+1
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def Custompager(baseurl,currentPage,totalpage):  #基础页,当前页,总页数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    perPager=11
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #总页数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #0 -- totalpage
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    #总页数>11
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #当前页大于5 currentPage-5 -- currentPage+5
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            #currentPage+5是否超过总页数,超过总页数,end就是总页数
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        #当前页小于5 0 -- 11
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    begin=0
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    end=0
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if totalpage :
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        begin=0
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        end=totalpage
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        if currentPage>5:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            begin=currentPage-5
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            end=currentPage+5
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            if end > totalpage:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                end=totalpage
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            begin=0
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            end=11
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pager_list=[]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if currentPage:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        first="首页"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        first="首页" % (baseurl,1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pager_list.append(first)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if currentPage:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        prev="上一页"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        prev="上一页" % (baseurl,currentPage-1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pager_list.append(prev)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    for i in range(begin+1,end+1):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        if i == currentPage:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            temp="%d" % (baseurl,i,i)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            temp="%d" % (baseurl,i,i)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        pager_list.append(temp)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if currentPage>=totalpage:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        next="下一页"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        next="下一页" % (baseurl,currentPage+1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pager_list.append(next)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    if currentPage>=totalpage:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        last="末页"
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        last="末页" % (baseurl,totalpage)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pager_list.append(last)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    result=''.join(pager_list)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    return mark_safe(result)   #把字符串转成html语言
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
总结,分页时需要做三件事:
Python 进阶篇

Python 进阶篇
* 创建处理分页数据的类
Python 进阶篇
* 根据分页数据获取数据
Python 进阶篇
* 输出分页HTML,即:[上一页][1][2][3][4][5][下一页]
Python 进阶篇

Python 进阶篇

缓存

Python 进阶篇

Python 进阶篇
由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5分钟内再有人来访问时,则不再去执行view中的操作,而是直接从内存或者Redis中之前缓存的内容拿到,并返回。
Python 进阶篇

Python 进阶篇
Django中提供了6种缓存方式:
Python 进阶篇

Python 进阶篇
* 开发调试
Python 进阶篇
* 内存
Python 进阶篇
* 文件
Python 进阶篇
* 数据库
Python 进阶篇
* Memcache缓存(python-memcached模块)
Python 进阶篇
* Memcache缓存(pylibmc模块)
Python 进阶篇

Python 进阶篇
1、配置
Python 进阶篇

Python 进阶篇
a、开发调试
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 此为开始调试用,实际内部不做任何操作
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 配置:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'BACKEND': 'django.core.cache.backends.dummy.DummyCache',     # 引擎
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'TIMEOUT': 300,                                               # 缓存超时时间(默认300,None表示永不过期,0表示立即过期)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'OPTIONS':{
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                    'MAX_ENTRIES': 300,                                       # 最大缓存个数(默认300)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                    'CULL_FREQUENCY': 3,                                      # 缓存到达最大个数之后,剔除缓存个数的比例,即:1/CULL_FREQUENCY(默认3)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                },
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'KEY_PREFIX': '',                                             # 缓存key的前缀(默认空)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'VERSION': 1,                                                 # 缓存key的版本(默认1)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'KEY_FUNCTION' 函数名                                          # 生成key的函数(默认函数会生成为:【前缀:版本:key】)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 自定义key
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def default_key_func(key, key_prefix, version):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
"""
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        Default function to generate keys.
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        Constructs the key used by all other methods. By default it prepends
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        the key_prefix'. KEY_FUNCTION can be used to specify an alternate
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        function with custom key making behavior.
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
"""
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return '%s:%s:%s' % (key_prefix, version, key)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def get_key_func(key_func):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
"""
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        Function to decide which key function to use.
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        Defaults to default_key_func`.
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
"""
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        if key_func is not None:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            if callable(key_func):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                return key_func
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                return import_string(key_func)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        return default_key_func
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
b、内存
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 此缓存将内容保存至内存的变量中
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 配置:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'LOCATION': 'unique-snowflake',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 注:其他配置同开发调试版本
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
c、文件
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 此缓存将内容保存至文件
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 配置:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'LOCATION': '/var/tmp/django_cache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 注:其他配置同开发调试版本
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
d、数据库
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 此缓存将内容保存至数据库
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 配置:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                'LOCATION': 'my_cache_table', # 数据库表
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # 注:执行创建表命令 python manage.py createcachetable
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
e、Memcache缓存(python-memcached模块)
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 此缓存使用python-memcached模块连接memcache
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'LOCATION': '127.0.0.1:11211',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'LOCATION': 'unix:/tmp/memcached.sock',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'LOCATION': [
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                '172.19.26.240:11211',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                '172.19.26.242:11211',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            ]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
f、Memcache缓存(pylibmc模块)
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# 此缓存使用pylibmc模块连接memcache
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'LOCATION': '127.0.0.1:11211',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'LOCATION': '/tmp/memcached.sock',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'default': {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            'LOCATION': [
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                '172.19.26.240:11211',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
                '172.19.26.242:11211',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            ]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
g. Redis缓存(依赖:pip3 install django-redis)
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
CACHES = {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    "default": {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        "BACKEND": "django_redis.cache.RedisCache",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        "LOCATION": "redis://127.0.0.1:6379",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        "OPTIONS": {
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            # "PASSWORD": "密码",
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    }
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django_redis import get_redis_connection
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
conn = get_redis_connection("default")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、应用
Python 进阶篇

Python 进阶篇
a. 全站使用
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
使用中间件,经过一系列的认证等操作,如果内容在缓存中存在,则使用FetchFromCacheMiddleware获取内容并返回给用户,当返回给用户之前,判断缓存中是否已经存在,如果不存在则UpdateCacheMiddleware会将缓存保存至缓存,从而实现全站缓存
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    MIDDLEWARE = [
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'django.middleware.cache.UpdateCacheMiddleware',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        # 其他中间件...
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        'django.middleware.cache.FetchFromCacheMiddleware',
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    ]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHE_MIDDLEWARE_ALIAS = ""
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHE_MIDDLEWARE_SECONDS = ""
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    CACHE_MIDDLEWARE_KEY_PREFIX = ""
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
b. 单独视图缓存
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
方式一:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        from django.views.decorators.cache import cache_page
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        @cache_page(60 * 15)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        def my_view(request):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            ...
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    方式二:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        from django.views.decorators.cache import cache_page
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        urlpatterns = [
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            url(r'^foo/([0-9]{1,2})/$', cache_page(60 * 15)(my_view)),
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        ]
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
c、局部视图使用
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
a. 引入TemplateTag
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        {% load cache %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    b. 使用缓存
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        {% cache 5000 缓存key %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            缓存内容
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        {% endcache %}
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
更多:猛击这里
Python 进阶篇

Python 进阶篇

序列化

Python 进阶篇

Python 进阶篇
关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的为Json格式。
Python 进阶篇

Python 进阶篇
1、serializers
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.core import serializers
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
ret = models.BookType.objects.all()
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
data = serializers.serialize("json", ret)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、json.dumps
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
import json
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
#ret = models.BookType.objects.all().values('caption')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
ret = models.BookType.objects.all().values_list('caption')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
ret=list(ret)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
result = json.dumps(ret)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
由于json.dumps时无法处理datetime日期,所以可以通过自定义处理器来做扩展,如:
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
import json
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from datetime import date
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from datetime import datetime
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
class JsonCustomEncoder(json.JSONEncoder):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def default(self, field):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        if isinstance(field, datetime):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            return o.strftime('%Y-%m-%d %H:%M:%S')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        elif isinstance(field, date):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            return o.strftime('%Y-%m-%d')
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        else:
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
            return json.JSONEncoder.default(self, field)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
# ds = json.dumps(d, cls=JsonCustomEncoder)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇

信号

Python 进阶篇

Python 进阶篇
Django中提供了"信号调度",用于在框架执行操作时解耦。通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者。
Python 进阶篇

Python 进阶篇
1、Django内置信号
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
Model signals
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pre_init                    # django的modal执行其构造方法前,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    post_init                   # django的modal执行其构造方法后,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pre_save                    # django的modal对象保存前,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    post_save                   # django的modal对象保存后,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pre_delete                  # django的modal对象删除前,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    post_delete                 # django的modal对象删除后,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    m2m_changed                 # django的modal中使用m2m字段操作第三张表(add,remove,clear)前后,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    class_prepared              # 程序启动时,检测已注册的app中modal类,对于每一个类,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
Management signals
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    pre_migrate                 # 执行migrate命令前,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    post_migrate                # 执行migrate命令后,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
Request/response signals
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    request_started             # 请求到来前,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    request_finished            # 请求结束后,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    got_request_exception       # 请求异常后,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
Test signals
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    setting_changed             # 使用test测试修改配置文件时,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    template_rendered           # 使用test测试渲染模板时,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
Database Wrappers
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    connection_created          # 创建数据库连接时,自动触发
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
对于Django内置的信号,仅需注册指定信号,当程序执行相应操作时,自动触发注册函数:
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.core.signals import request_finished
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.core.signals import request_started
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.core.signals import got_request_exception
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.db.models.signals import class_prepared
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.db.models.signals import pre_init, post_init
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.db.models.signals import pre_save, post_save
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.db.models.signals import pre_delete, post_delete
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.db.models.signals import m2m_changed
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.db.models.signals import pre_migrate, post_migrate
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.test.signals import setting_changed
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.test.signals import template_rendered
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    from django.db.backends.signals import connection_created
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    def callback(sender, **kwargs):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        print("xxoo_callback")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
        print(sender,kwargs)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    xxoo.connect(callback)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    # xxoo指上述导入的内容
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.core.signals import request_finished
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from django.dispatch import receiver
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
@receiver(request_finished)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def my_callback(sender, **kwargs):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    print("Request finished!")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
2、自定义信号
Python 进阶篇

Python 进阶篇
a. 定义信号
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
import django.dispatch
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
pizza_done = django.dispatch.Signal(providing_args=["toppings", "size"])
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
b. 注册信号
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
def callback(sender, **kwargs):
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    print("callback")
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
    print(sender,kwargs)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
pizza_done.connect(callback)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
c. 触发信号
Python 进阶篇
Python 进阶篇Python 进阶篇
Python 进阶篇

Python 进阶篇
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
from 路径 import pizza_done
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)
pizza_done.send(sender='seven',toppings=123, size=456)
![Python 进阶篇](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20220818/span>:)

Python 进阶篇

Python 进阶篇
View Code
Python 进阶篇

Python 进阶篇
由于内置信号的触发者已经集成到Django中,所以其会自动调用,而对于自定义信号则需要开发者在任意位置触发。
Python 进阶篇

Python 进阶篇
更多:猛击这里

Original: https://www.cnblogs.com/zhumengke/p/9553692.html
Author: 逐梦客!
Title: Python 进阶篇

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15833/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部