s5day82
内容回顾:
1. Http请求本质
Django程序:socket服务端
a. 服务端监听IP和端口
c. 接受请求
\r\n\r\n:请求头和请求体
\r\n
&
request.POST
request.GET
d. 响应:
响应头: location:www.oldboyedu.com
和
响应体
e. 断开连接
浏览器: socket客户端
b. 浏览器发送:
GET请求:
"GET /index.html http1.1\r\nUser-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x6..\r\n\r\nAccept-Encoding:gzip\r\n\r\n"
POST请求:
"POST /index.html http1.1\r\nUser-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x6..\r\n\r\nAccept-Encoding:gzip\r\n\r\nuser=cqz&hobby=lihao"
e. 断开连接
COOKIE: 请求头和响应头中存在
2. Django请求的生命周期
wsgi -> 中间件 -> 路由系统 -> 视图函数(ORM,Template,渲染)
- wsgiref
- uwsgi
PS: 中间件,FBV&CBV
3. FBV和CBV
function base view, URL对应函数
class base view, URL对应类
PS:
form表单提交: GET,POST ***
Ajax提交数据: GET,POST
['get'获取, 'post'创建, 'put'更新, 'patch'局部更新, 'delete'删除, 'head', 'options', 'trace'] -> restful规范
a. 基本使用
b. 基于dispatch和继承实现用户登录代码
c. 装饰器
get,post方法上
class LoginView(View):
def dispatch(self, request, *args, **kwargs):
return super(LoginView,self).dispatch(request, *args, **kwargs)
def get(self,request):
return render(request,'login.html')
@method_decorator(test)
def post(self,request):
# request.GET
# request.POST # 请求头中的:content-type
# request.body
user = request.POST.get('user')
pwd = request.POST.get('pwd')
if user == 'alex' and pwd == "alex3714":
# 生成随机字符串
# 写浏览器cookie: session_id: 随机字符串
# 写到服务端session:
# {
# "随机字符串": {'user_info':'alex}
# }
request.session['user_info'] = "alex"
return redirect('/index.html')
return render(request, 'login.html')
dispatch方法上
class LoginView(View):
@method_decorator(test)
def dispatch(self, request, *args, **kwargs):
return super(LoginView,self).dispatch(request, *args, **kwargs)
def get(self,request):
return render(request,'login.html')
def post(self,request):
# request.GET
# request.POST # 请求头中的:content-type
# request.body
user = request.POST.get('user')
pwd = request.POST.get('pwd')
if user == 'alex' and pwd == "alex3714":
# 生成随机字符串
# 写浏览器cookie: session_id: 随机字符串
# 写到服务端session:
# {
# "随机字符串": {'user_info':'alex}
# }
request.session['user_info'] = "alex"
return redirect('/index.html')
return render(request, 'login.html')
类上
@method_decorator(test,name='get')
class LoginView(View):
def dispatch(self, request, *args, **kwargs):
return super(LoginView,self).dispatch(request, *args, **kwargs)
def get(self,request):
return render(request,'login.html')
def post(self,request):
# request.GET
# request.POST # 请求头中的:content-type
# request.body
user = request.POST.get('user')
pwd = request.POST.get('pwd')
if user == 'alex' and pwd == "alex3714":
# 生成随机字符串
# 写浏览器cookie: session_id: 随机字符串
# 写到服务端session:
# {
# "随机字符串": {'user_info':'alex}
# }
request.session['user_info'] = "alex"
return redirect('/index.html')
return render(request, 'login.html')
特殊:CSRF Token只能加到dispatch
from django.views.decorators.csrf import csrf_exempt,csrf_protect
class LoginView(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(LoginView,self).dispatch(request, *args, **kwargs)
def get(self,request):
return render(request,'login.html')
def post(self,request):
# request.GET
# request.POST # 请求头中的:content-type
# request.body
user = request.POST.get('user')
pwd = request.POST.get('pwd')
if user == 'alex' and pwd == "alex3714":
# 生成随机字符串
# 写浏览器cookie: session_id: 随机字符串
# 写到服务端session:
# {
# "随机字符串": {'user_info':'alex}
# }
request.session['user_info'] = "alex"
return redirect('/index.html')
return render(request, 'login.html')
4. 中间件
a. 中间件是一个类
- process_request
- 有,直接执行当前中间件和上方中间件的process_response
- 无
应用: 用户登录授权(排除不需要登录的url)
- process_response
- 必须有返回值
- process_view
- process_exception
- process_tempalte_response
- 必须有返回值
- 必须对象中要有render方法
b. 流程
c. 什么时候用中间件?所有请求统一做处理时使用
- 登录验证
d. 中间件中的方法,可以有任意个
实例:
settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'md.middleware.M1',
'md.middleware.M2',
]
Project/md/middleware.py
from django.shortcuts import HttpResponse,redirect
class MiddlewareMixin(object):
def __init__(self, get_response=None):
self.get_response = get_response
super(MiddlewareMixin, self).__init__()
def __call__(self, request):
response = None
if hasattr(self, 'process_request'):
response = self.process_request(request)
if not response:
response = self.get_response(request)
if hasattr(self, 'process_response'):
response = self.process_response(request, response)
return response
"""
class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.process_request')
def process_view(self,request, view_func, view_func_args, view_func_kwargs):
print('m1.process_view')
def process_exception(self,request,exception):
print('m1.process_exception')
def process_response(self,request,response):
print('m1.process_response')
return response
def process_template_response(self,request,response):
print('m1.process_template_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
def process_view(self, request, view_func, view_func_args, view_func_kwargs):
print('M2.process_view')
def process_exception(self,request,exception):
print('m2.process_exception')
return HttpResponse('开发的程序员已经被打死')
def process_response(self, request, response):
print('m2.process_response')
return response
def process_template_response(self,request,response):
print('m2.process_template_response')
return response
"""
class M1(MiddlewareMixin):
def process_response(self, request, response):
print('m2.process_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('m2.process_request')
今日内容概要:
1. Django内容补充
- FBV&CBV
- 中间件
2. Git使用
- 什么是GIT,
- 张开的故事:资源共享网站
- 小弟弟系列
- 小芳系列
- 第一个版本:进入程序目录:
git init 初始化
git add . 当前目录中所有文件添加到【某个地方】
git commit -m '描述信息' 第一版本git已经生成
git status
- 新功能: 李浩专区
开发到了一半,紧急修复线上BUG
git stash
解决bug
git add .
git commit -m '修复bug完毕'
git stash pop
可能出现冲突,出现之后不要着急,手动解决冲突
Auto-merging templates/index.html
CONFLICT (content): Merge conflict in templates/index.html
开发功能完毕
git add .
git commit -m '解决冲突后,继续开发后续功能'
PS:
git stash 将当前工作区所有修改过的内容存储到“某个地方”,将工作区还原到当前版本未修改过的状态
git stash pop 将第一个记录从“某个地方”重新拿到工作区(可能有冲突)
git stash list 查看“某个地方”存储的所有记录
git stash clear 清空“某个地方”
git stash apply 编号, 将指定编号记录从“某个地方”重新拿到工作区(可能有冲突)
git stash drop 编号,删除指定编号的记录
-
git log 查看版本历史
git reset --hard 3ee80517425148b9d87591c9bd29a77e3db89ff2 返回历史版本
git reflog
1. Git
工作区 版本库 远程仓库
未修改 已修改【红色】 暂存【绿色】 分支
上节回顾Git:【master】
git status
git add 文件名
git add .
git commit -m 'xxxxx'
git log
git reflog
git reset --hard xxxxdfasdf
git stash
git stash pop
分支:
提交master分支已经修改的问题,或者回到原始状态
git branch dev
git checkout dev
Dev
# 开发一半...
# git branch
git add .
git commit -m 'xasdf'
# 继续开发
git add .
git commit -m 'xasdf'
Master:
git checkout master
git merge dev
通过Bug分支紧急修复Bug
GitHub(代码托管)
GitLab(公司自己搭建代码托管)
公司:
创建远程仓库
写readme
git add .
git commit ...
git push origin master
下班
家:
git clone https://github.com/WuPeiqi/s5day83.git 从无到有
git add .
git commit ...
git push origin master
到公司:
git pull origin master 从旧到新
# 继续开发,开发到一半,留点代码回家做
git add .
git commit -m 'xxx'
# git push github master
家:
根据自己的记忆继续写代码:
在家完成后续功能
git add .
git commit -m 'xxx'
git push origin master
公司:
git pull github master
git fetch github master
git merge github/master <=> git rebase github/master
解决冲突
# 继续写代码
git add .
git commit -m 'xxx'
git push github master
协同开发:【点点点】
分支:
master
review
dev
协同:
3个人:dev
组长:代码review,合并完成后,
你有没有给不牛逼的代码修改过Bug?【点点点】
- 么有
常用点点点操作:
- 合作开发
- 项目:添加合作者
- 组织:创建人,开发项目
- Fork
- pull request
头疼问题:连接远程仓库
Https
- 手动输入用户名密码
SSH
- 生成一对秘钥 cd ~/.ssh/
- 公钥拷贝到GitHub
- git add remote origin git@github.com:WuPeiqi/Tyrion.git
git pull origin master
git push oriing master
忽略文件 : .gitignore
a.*
test/[abc].py
[abc]
test/*
!test/a.py
.idea/*
*.pyc
*.excel
PS: git命令只能在: .git 文件的路径