WARNING log 228 Forbidden (CSRF cookie not set.): /user/login/
WARNING basehttp 154 "POST /user/login/ HTTP/1.1" 403 2840
post提交需携带token问题三种解决方法
第一种:
在html文件form表单中添加{% csrf_token %}
<form action="" method="post">
{% csrf_token %}
<form>
第二种:
view中使用method_decorator装饰器
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie
class LoginView(View):
@method_decorator(ensure_csrf_cookie)
def get(self, request)
pass
def post(self, request)
pass
第三种:
自定义一个中间件
from django.utils.deprecation import MiddlewareMixin
from django.middleware.csrf import get_token
class CsrfTokenMiddleware(MiddlewareMixin):
def process_request(self, request):
get_token(request)
# settings.py文件中间件添加一行
'utils.csrfTokenMiddleware.CsrfTokenMiddleware'