Django包含特定的函数,包括中间组件,用于验证中间产品,并使用会话将用户与应用程序关联。
from django.utils.deprecation import MiddlewareMixin class MyMW(MiddlewareMixin): def process_request(self, request): print("MyMW process_request..") def process_view(self, request, callback, callback_args, callback_kwargs): print("MyMW process_view..") def process_response(self, request, response): print("MyMW process_response..") return response
from django.contrib import admin from django.urls import path from . import views urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘test_cache‘, views.test_cache), path(‘test_mw‘, views.test_mw), path(‘test_csrf‘, views.test_csrf), ] <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/test_csrf" method="post"> <input type="text" name="username"> <input type="submit" value="提交"> </form> </body> </html> def test_csrf(request): if request.method == ‘GET‘: return render(request, ‘test_csrf.html‘) elif request.method == ‘POST‘: return HttpResponse(‘test_csrf‘)
原文:https://www.cnblogs.com/java0011/p/14770933.html