Django提交post表单出现403错误提示
向django的服务器提交post请求的代码如下:
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.views.decorators import csrf
from django.shortcuts import render_to_response
# # 表单
# 接收POST请求数据
def search_post(request):
ctx ={}
if request.POST:
ctx['rlt'] = request.POST['q']
return render(request, "post.html", ctx)
else:
return render_to_response('post.html')
当采用客户端向django的服务器提交post请求时,出现403,权限异常。如下图所示:
解决办法:
导入模块from django.views.decorators.csrf import csrf_exempt
在函数前面添加修饰器@csrf_exempt
具体代码如下:
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.views.decorators import csrf
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
# # 表单
@csrf_exempt
# 接收POST请求数据
def search_post(request):
ctx ={}
if request.POST:
ctx['rlt'] = request.POST['q']
return render(request, "post.html", ctx)
else:
return render_to_response('post.html')
结果如下图所示: