鍍金池/ 問答/Python  HTML/ Django無法得到POST請求的參數(shù)?

Django無法得到POST請求的參數(shù)?

在使用axios向后端發(fā)送POST請求的時候,服務(wù)器無法得到POST中帶有的參數(shù)。
前端的發(fā)送代碼如下:

Http({
    method:'POST',
    url:'signup',
    data:{
        username:this.signUpform.username,
        password:this.signUpform.password,
        email:this.signUpform.email,
        cellphone:this.signUpform.cellphone
    }
})

Axios設(shè)置如下:

import axios from 'axios'
export default axios.create({
  baseURL: 'http://127.0.0.1:8000/api',
  timeout: 20000,
  headers: {'Content-Type': 'application/json'}
})

后臺接受代碼如下:

@require_http_methods(["POST"])
def signup(request):
    response = {}
    try:
        print(request.POST.get('username'))
        WorkUser.objects.get(username=request.POST.get('username'))
        response['error'] = 'username already exist'
    except WorkUser.DoesNotExist:
        wu=tools.toWorkUser(request)
        wu.save()
        response['error'] = 'success'
        
    return JsonResponse(response)

輸出,顯示:

None
Internal Server Error: /api/signup
Traceback (most recent call last):
  File "C:\Users\fengfanfan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\datastructures.py", line 77, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'username'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\fengfanfan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\fengfanfan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\fengfanfan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\fengfanfan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\decorators\http.py", line 40, in inner
    return func(request, *args, **kwargs)
  File "E:\WorkSpace\GraduationThesis\GPServer\server\views.py", line 36, in signup
    WorkUser.objects.get(username=request.POST['username'])
  File "C:\Users\fengfanfan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\datastructures.py", line 79, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'username'

然而,使用Postman接口卻可以使用,這是為什么呢

回答
編輯回答
久不遇

axios使用axios.post(url, params)的方式調(diào)用更合適

2017年4月5日 01:10
編輯回答
心癌

后端,你打印一下:request.body,看看你的參數(shù)是不是在里面。

2018年3月2日 20:25
編輯回答
尐飯團(tuán)

建議用postman先測試下接口

2017年3月24日 20:43
編輯回答
有你在

終于找到可以用的辦法了,似乎只要在前端對data處理一下就可以了,用qs:

data:qs.stringify({
        'username':this.signUpform.username,
        'password':this.signUpform.password,
        'email':this.signUpform.email,
        'cellphone':this.signUpform.cellphone
})
2018年6月20日 07:22