再学-Djiago

Mr.zh Lv3

Djiago基础

一、入门

1. path函数

image-20241129203306959

2. url的使用

image-20241129210736254

image-20241129211048359

二、模板

1. 渲染模板

image-20241129212647216

image-20241129213615519

2. DTL模板语法

1. 变量

image-20241129220123805

image-20241129221020507

2. 常用模板标签

image-20241129221247617

image-20241130113603414

image-20241130115028839

3.url标签

image-20241130115249848

image-20241130123620745

4. 渲染解析中的过滤器

image-20241130125309187

image-20241130125510788

image-20241130131700980

image-20241130131740098

image-20241130131858332

image-20241130131911674

image-20241130131934249

image-20241130132110353

image-20241130132221958

5. 模板结构

image-20241130142904285

6. 模板继承

image-20241130143422290

image-20241130143458434

7. 静态文件

image-20241130143620597

image-20241130144322030

image-20241130144420694

image-20241130144547555

三、数据库

1. MySQL的驱动程序

image-20241130145025448

2. 操作数据库

①:配置

image-20241130145132052

②:原生SQL操作数据库

image-20241130192714861

1
2
3
4
5
6
7
8
9
10
11
12
13
from django.db import connection
# 获取游标对象
cursor = connection.cursor()

# 执行sql语句
cursor.execute('select * from op')

# 获取所有的数据
rows = cursor.fetchall()
# 遍历查询到的数据
book = []
for row in rows:
book.append({'name':row[0], 'age':row[1]})

image-20241130193806502

③:ORM模型操作数据库

image-20241130194752660

  1. 创建表

image-20241130200634859

image-20241130201508271

image-20241130201846881

  1. 添加数据

image-20241130202507840

  1. 查找数据

image-20241130204346741

image-20241130210421096

  1. 修改和删除数据

image-20241130210606798

④:模型常见的Field

image-20241130210900100

image-20241201111448331

image-20241201131020273

image-20241201131115928

image-20241201131146401

⑤:Field的常用参数

image-20241201192301731

⑥:模型中Meta配置

image-20241201195038024

image-20241201204531076

3. 外键

image-20241201204652750

image-20241201205524030

image-20241201205434040

image-20241201205719709

①:外键的删除操作

1
# 所引用的这条外键记录如果被删除,那么当前的数据如何进行处理

image-20241201205931829

②:表的关系

  • 一对多
    image-20241202102919397

    image-20241201211034956

  • 一对一

  • 多对多
    image-20241202103600464

4. 查询条件

①:常用查询条件

image-20241202104538676

image-20241202105539152

image-20241202105600080

image-20241202110135293

image-20241202110416324

image-20241202110438095

image-20241202110501619

image-20241202112002193

image-20241202112132612

image-20241202112210930

image-20241202112313791

②:聚合函数

image-20241202112931341

image-20241202131107186

image-20241202140401125

5. F表达式和Q表达式

①:F表达式

image-20241202141547796

image-20241202142304974

②:Q表达式

image-20241202142923946

四、视图高级

五、表单

1. Django中的表单

1
2
3
4
5
6
# forms.py
from django import forms
# 留言板的表单
class MessageBoardForm(forms.Form):
title = forms.CharField(max_length=120 , min_length=2 , label='标题' , error_messages={"min_length":"标题最小长度至少为2!" , "max_length":"标题最大长度不能超过120个字符!"})
content = forms.CharField(max_length=200)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# views.py
from django.shortcuts import render , HttpResponse
from .forms import MessageBoardForm
from django.views.decorators.http import require_http_methods

# 针对这个方法只需要get和post请求,可以使用一个装饰器
@require_http_methods(['GET','POST'])
def index(request):
if request.method == 'GET':
f = MessageBoardForm()
context = {
'form':f
}
return render(request, 'index.html' , context=context)
elif request.method == 'POST':
f = MessageBoardForm(request.POST)
# 表示表单数据正确
if f.is_valid():
title = f.cleaned_data['title']
content = f.cleaned_data['content']
email = f.cleaned_data['email']
return HttpResponse(f"{title} , {content} , {email}")
else:
return HttpResponse(f.errors)

2.表单验证

①:常用验证器

image-20241202190031998

②:自定义验证器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 自定义 表单验证器
class RegisterForm(forms.Form):
phone = forms.CharField(validators=[validators.RegexValidator(r'1[345678]\d{9}' , message="手机号码格式错误!")])
pwd1 = forms.CharField(max_length=120)
pwd2 = forms.CharField(max_length=120)

# 上面是通过正则的方式,如果涉及到查询数据库之类的:比如用户注册的手机号,需要判断是不是已经注册过,可以通过自定义验证逻辑来实现
# 针对某一个字段进行验证的重写方式
def clean_phone(self):
phone = self.cleaned_data['phone']
if phone == '17634774652':
raise forms.ValidationError("该手机号已经注册!!!")
else:
return phone

# 针对多个字段进行效验的重写方式
def clean(self):
new_data = super().clean()
print(type(new_data) , new_data)
pwd1 = new_data.get('pwd1')
pwd2 = new_data.get('pwd2')
if pwd1 != pwd2:
raise forms.ValidationError("密码输入不一致!")

③:ModelForm

image-20241203110458161

image-20241203110539952

image-20241203114246980

六、cookie

1.Cookie介绍

image-20241203120404218

2. Django操作cookie

①:设置cookie

image-20241203151336883

②:删除和获取cookie

image-20241203161551926

3. Django操作session

①:session的使用

image-20241203164442494

image-20241203170250263

②:session的存储机制

image-20241203170905328

4. CSRF攻击(扩展)

image-20241203202112630

七、博客项目

1. qq验证码实现

配置文件

1
tylcwlidkpoviibb
1
2
3
4
5
6
7
8
9
# 发送验证码的配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.qq.com' # 这个地方目前是使用的qq进行发送
EMAIL_PORT = 587
EMAIL_HOST_USER = 'zhyoulove@qq.com'
EMAIL_HOST_PASSWORD = 'tylcwlidkpoviibb'
DEFAULT_FROM_EMAIL = 'zhyoulove@qq.com'

发送逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 发送注册邮箱验证码
# xxx ?email=xxx
def send_email(request):
email = request.GET.get('email')
print(email)
# 如果邮箱为空,表示需要传递邮箱这个参数
if email is None:
return JsonResponse({"code":400 , "message":"必须传递邮箱!"})
# 表示获取到了需要发送的邮箱 先随机生成四位数字
captcha = "".join(random.sample(string.digits , 6))
print(captcha)
# 进行发送
send_mail("注册博客验证码" , message=f'您的注册验证码是:{captcha}' , recipient_list=[email] , from_email=None)

return JsonResponse({"code":200 , "message":"邮箱验证码发送成功!"})

  • Title: 再学-Djiago
  • Author: Mr.zh
  • Created at : 2024-11-30 21:28:53
  • Updated at : 2024-12-06 16:11:18
  • Link: https://github.com/zhyoulove/2024/11/30/再学-Djiago/
  • License: This work is licensed under CC BY-NC-SA 4.0.