我TM一直用vue的post提交数据,一直403,然后测试了N种方法,最后居然是被rest_rest_framework的认证系统 给坑惨了,我没定义认证,它却自动的给我默认上拒绝了
setting配置:
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [],DEFAULT_PERMISSION_CLASSES
接口文件:
from rest_framework.views import APIView
from django.shortcuts render,redirect,HttpResponse
from api models
from django.http JsonResponse
class Register(APIView):
def get(self,request):
return render(request,login.html)
post(self,request):
username = str(request.data.get(register_username))
password = str(request.data.get(register_password))
message = {}
try:
models.UserInfo.objects.create(username=username,password=password)
message['code'] = 200
message[message'] = 注册成功"
return HttpResponse(message)
except Exception as e:
message['] = 444注册失败return JsonResponse(message)
vue 文件:
// 注册
register() {
if (this.register_password != this.register_password_re) {
alert(两次输入的密码不一致)
} else if (this.register_username === "" || this.register_password === "" || this.register_password_re === "") {
alert(输入框不能为空else {
axios.post(/register/',{
register_username: this.register_username,register_password: this.register_password
})
.then(response => {
if (response.data['] ==200) {
console.log(response.data)
alert()
} '] == 444) {
alert(用户名已存在)
}
}).catch(error => {
console.log(error)
alert(请求异常)
})
}
},