1.模板
1.1 寻找html模板
python
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
# 'django.contrib.auth.context_processors.auth',
# 'django.contrib.messages.context_processors.messages',
],
},
},
]
优先去项目根目录 > 每个已注册的app的templates目录找。
如何选择:
- 简单的项目,模板都放在根目录。
- 复杂的项目,模板放在各自的app中,公共部分放在templates目录。
扩展:修改内置app的模板也是同样的套路。
1.2 模板处理的本质
渲染完成后,生成了字符串,再返回给浏览器。


1.3 常用语法

1.4 内置函数
在django模板语法中提供了内置函数让我们来。

1.5 自定义模板功能

三种方式:
-
filter
- 数据处理,参数:1~2个
- 数据处理,if条件
-
simple_tag
参数无限制 & 返回文本
-
inclusion_tag
参数无限制 & HTML片段
需求来了:根据用户权限不同显示不同的菜单。

1.6 继承和母版


1.7 模板的导入

2.django中间件

- 类
- 定义方法
- 注册
2.1 原始方式


2.2 MiddlewareMixin(建议)


注意:django1版本。
源码:
-
面向对象
pythonclass MyMd(object): def __init__(self....): pass def __call__(self,....): pass django内部默认执行call方法,传入参数。
-
反射
pythonclass MyMd(object): def __init__(self....): pass def __call__(self,....): if hasattr(self,'process_request'): response = self.process_request(request) ... django内部默认执行call方法,传入参数。
pythonclass MiddlewareMixin: def __init__(self, get_response=None): self.get_response = get_response def __call__(self, request): response = None if hasattr(self, 'process_request'): response = self.process_request(request) response = response or self.get_response(request) if hasattr(self, 'process_response'): response = self.process_response(request, response) return response class MyMd(MiddlewareMixin): def process_request(self,request): ... def process_response(self,request, response): ... django内部默认执行call方法,传入参数。



疑问:prcess_request的执行时,是否已执行了路由匹配?
request.resolver_match
注意:process_view是在django中源码中写死了。

2.3 其他


小结
- 定义中间类
- 类方法
- process_request
- process_view
- process_reponse
- process_exception,视图函数出现异常,自定义异常页面。
- process_template_response,视图函数返回
TemplateResponse
对象 or 对象中含有.render方法。

3.ORM操作
orm,关系对象映射,本质翻译的。

3.1 表结构
实现:创建表、修改表、删除表。
在app中的models.py中按照规则编写类 ===> 表结构。
-
编写类
pythonfrom django.db import models class UserInfo(models.Model): name = models.CharField(max_length=16) age = models.IntegerField()
-
注册app
pythonINSTALLED_APPS = [ # 'django.contrib.admin', # 'django.contrib.auth', # 'django.contrib.contenttypes', # 'django.contrib.sessions', # 'django.contrib.messages', 'django.contrib.staticfiles', 'apps.app01.apps.App01Config', 'apps.app02.apps.App02Config', ]
-
命令,django根据models中类生成一个
对数据库操作的配置文件
=>migrations
python manage.py makemigrations
-
命令,读取已经注册么给app中的migrations目录将配置文件 -> 转换成:生成表,修改表 SQL -> 连接数据库去运行。
pythonpython manage.py migrate
-
那个数据库?
-
数据库账户和密码?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
-
常见问题:请不要再手动去修改数据的表结构 + 时刻保证 ORM和数据表是对应。
3.1.1 常见字段和参数
-
字段
CharField SmallIntegerField IntegerField BigIntegerField DateField DateTimeField BooleanField -> 其实数据库不支持真假,根据SmallIntegerField创造出来出来。 0 1 DecimalField -> 精确的小数
-
参数
pythonname = models.CharField(verbose_name="姓名", max_length=16) name = models.CharField(verbose_name="姓名", max_length=16, default="哈哈哈") # 经常查询,速度快(MySQL,https://www.bilibili.com/video/BV15R4y1b7y9) name = models.CharField(verbose_name="姓名", max_length=16, default="哈哈哈", null=True, blank=True, db_index=True) email = models.CharField(verbose_name="姓名", max_length=16, default="哈哈哈", null=True, blank=True, unique=True) # 在数据库存储时只能是:sh、bj (上海、北京一般用于页面显示中文) code = models.CharField(verbose_name="姓名", max_length=16, choices=(("sh", "上海"), ("bj", "北京")),default="sh")
python# 不用 max_length=16 count = models.IntegerField(verbose_name="数量", default=1, null=True, blank=True, unique=True) code = models.IntegerField(verbose_name="性别",choices=((1, "男"), (2, "女")),default=1)
pythonregister_date = models.DateField(verbose_name="注册时间", auto_now=True)
pythonamount = models.DecimalField(verbose_name="余额", max_digits=10, decimal_places=2)
示例:
python
from django.db import models
class UserInfo(models.Model):
name = models.CharField(verbose_name="姓名", max_length=16, db_index=True)
age = models.PositiveIntegerField(verbose_name="年龄")
email = models.CharField(verbose_name="邮箱", max_length=128, unique=True)
amount = models.DecimalField(verbose_name="余额", max_digits=10, decimal_places=2, default=0)
register_date = models.DateField(verbose_name="注册时间", auto_now=True)
class Goods(models.Model):
title = models.CharField(verbose_name="标题", max_length=32)
# detail = models.CharField(verbose_name="详细信息", max_length=255)
detail = models.TextField(verbose_name="详细信息")
price = models.PositiveIntegerField(verbose_name="价格")
count = models.PositiveBigIntegerField(verbose_name="库存", default=0)
3.1.2 表关系





注意:ManyToManyField生成的表字段只能id/bid/gid