Django FBV/CBV、中间件、GIT使用
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 文件的路径
Django FBV/CBV、中间件、GIT使用的更多相关文章
- Django FBV CBV以及使用django提供的API接口
FBV 和 CBV 使用哪一种方式都可以,根据自己的情况进行选择 看看FBV的代码 URL的写法: from django.conf.urls import url from api import v ...
- django FBV +CBV 视图处理方式总结
1.FBV(function base views) 在视图里使用函数处理请求. url: re_path('fbv', views.fbv), # url(r'^fbv' ...
- [oldboy-django][2深入django]FBV + CBV + 装饰器
FBV django CBV & FBV - FBV function basic view a. urls 设置 urls(r'^test.html$', views.test) b. vi ...
- django——FBV与CBV
引言 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class bas ...
- Django的CBV与FBV
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...
- Django的 CBV和FBV
FBV CBV 回顾多重继承和Mixin 回到顶部 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以 ...
- Django 之 CBV & FBV
FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django随笔中,一直使用的是这种方式,不再赘述. CBV CBV(class base views) ...
- Django之CBV和FBV
Django之CBV和FBV CBV和FBV是C和F的区别: C是Class,F是Function 在请求中,有GET请求和POST请求. 在写CBV时,url是可以对应一个类的,在类中,分别写出GE ...
- Django 路由视图FBV/CBV
路由层 url路由层结构 from django.conf.urls import url from django.contrib import admin from app01 import vi ...
随机推荐
- etcd 删除
vim /etc/sysconfig/flanneld FLANNEL_ETCD_ENDPOINTS="https://192.168.30.241:2379,https://192.168 ...
- 适合自己的adblock过滤列表
轻微完美主义,极简主义 已屏蔽广告: 1.CSDN的广告 2.百度侧栏热点搜索 3. 知乎广告 4.stackoverflow的推送广告 5.LeetCode的推送的是否见过这个题 bbs.csdn. ...
- PATtest1.3:最大子列和
题目源于:https://pintia.cn/problem-sets/16/problems/663 题目要求:输入一个数列,求其最大子列和. 问题反馈:1.部分C++代码不是很熟练 2.没有仔细读 ...
- 网络流$1$·简单的$EK$与$Dinic~of~Net-work ~ Flow$学习笔记
\(2333\)这是好久之前学的了,不过一直在咕咕咕咕. 一般来讲,正常的网络流笔记一开始都是要给网络流图下定义的.那么我们不妨也来先进行一波这种操作. 那么网络流图,类似于有向图,边上带权,但是这个 ...
- pyhton 面向对象之 小明左右手换牌
'''#左右手交换牌 案列#小明手里有俩张牌,左手红桃♥K,右手黑桃♠A,小明交换俩手的牌后,手里分别是什么? 人类: 属性:小明,左手,右手 行为:展示手里的牌, 交换手里的牌手类: ...
- 关于"为什么说Arduino是玩具?"的回答
最开始从51入门.之后MSP.ARM.FriendARM等等和使用keil(MDK).iar等工具.之后Arduino.Raspberry Pi的人想说: "说'Arduino是玩具,和Ar ...
- win10安装OpenSSL及简单的使用
学习IdentityServer过程中需要使用OpenSSL,OpenSSL是什么东西?百度百科的解释:在计算机网络上,OpenSSL是一个开放源代码的软件库包,应用程序可以使用这个包来进行安全通信, ...
- 【转】使用nginx搭建高可用,高并发的wcf集群
原文:http://www.cnblogs.com/huangxincheng/p/7707830.html 很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk ...
- 兼容性/pollyfill/shim/渐进增强/优雅降级
http://ued.ctrip.com/blog/browser-compatibility-testing-tools-in-firefox-compatibility-detector.html ...
- angularjs-ui-router-animation
html <!DOCTYPE html> <html ng-app="APP"> <head> <title></title& ...