一、MVC,MTV架构

  models(数据库,模型) views(html模板) controllers(业务逻辑处理) --> MVC

  models(数据库,模型) templates(html模板) views(业务逻辑处理) --> MTV

  Django -> MTV

二、中间件

在settings配置文件中
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',
]
在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法。
在django项目的settings模块中,有一个 MIDDLEWARE_CLASSES 变量,其中每一个元素就是一个中间件 自定制两个中间件类,创建md.py
from django.utils.deprecation import MiddlewareMixin class M1(MiddlewareMixin):
def process_request(self,request):
print('m1.proccess_request') def process_view(self, request, callback, callback_args, callback_kwargs):
print('m1.process_view')
response = callback(request,*callback_args,**callback_kwargs)
return response def process_exception(self, request, exception):
pass def process_response(self,request,response):
print('m1.process_response')
return response class M2(MiddlewareMixin):
def process_request(self,request):
print('m2.process_request') def process_view(self, request, callback, callback_args, callback_kwargs):
print('m2.process_view') def process_exception(self, request, exception):
pass def process_response(self,request,response):
print('m2.process_response')
return response 去settings里添加一下
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.M1',
'md.M2',
] 运行结果:
m1.proccess_request
m2.process_request
m1.process_view
test
m2.process_response
m1.process_response 当用户请求进来的时候,会先从MIDDLEWARE里从上往下走完proccess_request,接着再返回顶部再重新往下走process_view,遇到了process_view,就会直接跳到视图函数里执行视图函数,不再执行其他的process_view。
从视图函数返回的时候会从M2向上先执行每一个process_exception,然后再返回来M2再向上执行每一个process_response,如果process_exception有返回值Httpresponse被处理了,则不再执行其他process_exception,而是直接从M2的process_response向上执行。 process_request process_response(...response)
必须有返回值
return response process_view
process_exception
process_template_view 应用:对所有请求或一部分请求做批量处理

三、初识Form组件

views.py中
class LoginForm(Form):
username = fields.CharField(max_length=8,min_length=4,required=True,
error_messages={
'max_length':'用户名太长',
'min_length':'用户名太短',
'required':'用户名不能为空'
})
passwd = fields.CharField(max_length=12,required=True,
error_messages={
'max_length':'密码太长',
'required':'密码不能为空'
}) def login(request):
if request.method == 'GET':
return render(request,'login.html')
else:
obj = LoginForm(request.POST)
if obj.is_valid(): # 如果验证成功
print(obj.cleaned_data) # 字典类型
return HttpResponse('successful done')
else:
print(obj.errors) # 字典类型
return render(request, 'login.html',{'obj':obj}) login.html模板中
<form action="login.html" method="POST">
{% csrf_token %}
<p><input type="text" name="username" placeholder="用户名"> {{ obj.errors.username.0 }}</p>
<p><input type="password" name="passwd" placeholder="密码"> {{ obj.errors.passwd.0 }}</p>
<input type="submit" value="提交">
</form> 要点:Form组件类中的字段名称要和相应的html模板文件的name属性值对应才可以检测成功 # html标签name属性 = Form类字段名
# 正确信息
obj.cleaned_data
# 所有错误信息
obj.errors
05-11 11:30