0

I have issue with retrieving data from POST in my django view. I send React form values with axios to my django backend. I believe data gets put into POST but somehow it seems that there isn't any data in POST and i can't access it in my django view. What could be issue here? (I can also see in my console that values are successfully submitted)

Source code:
Django views.py:

@csrf_exempt
def send(request):
    if request.method == "POST":
        data = request.body('name')
        send_mail('Test 1', data, 'austin.milk1@gmail.com', ['lowoko9513@gomail4.com',], fail_silently=False)
    return redirect('/api/')

React form handling:

handleFormSubmit = (event) => {
        const name = event.target.elements.name.value;
        const email = event.target.elements.email.value;
        event.preventDefault();
        axios.post('http://127.0.0.1:8000/api/send', {
            name: name,
            email: email
        })
        .then(res => console.log(res))
        .catch(error => console.err(error));
    };

New error:

File "C:\Users\austi\PycharmProjects\Fitex#1\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\austi\PycharmProjects\Fitex#1\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\austi\PycharmProjects\Fitex#1\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\austi\PycharmProjects\Fitex#1\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\austi\PycharmProjects\Fitex5\backend\src\training\api\views.py", line 78, in send
    data = request.body('name')
TypeError: 'bytes' object is not callable
Austin Roose
  • 69
  • 2
  • 11
  • This is because you're sending JSON and not FormData, the POST field is for forms. You want `request.body`. https://stackoverflow.com/a/3020756/490790 – SeedyROM Jul 24 '20 at 19:26

3 Answers3

1

This is because you're sending JSON and not FormData, the POST field is for forms. You want request.body.

Here's duplicate question: https://stackoverflow.com/a/3020756/490790

Further Update

You're trying to call a string (or bytes in this case) as a function. request.body is not a function. If you want to get a field you'll have to parse it as JSON and then access it like a dictionary.

data = json.loads(request.body)
name = data['name']
SeedyROM
  • 2,203
  • 1
  • 18
  • 22
0

parameters send in axios post request can be retrieved from request.body. Parameters = json.loads(request.body)

Now you can retrieve your parameters from this dictionary.

arti gupta
  • 109
  • 1
  • 5
-1

You can also directly access by:

email = request.data['email']
name = request.data['name']
Abhishek Patel
  • 258
  • 3
  • 10
  • **This will only work for if you have `djangorestframework`** installed. By default Django has no idea how to unserialize JSON, you need to either do this manually or use DRF helpers. – SeedyROM Aug 09 '20 at 10:57