mohamed elgamal
mohamed elgamal

Reputation: 116

AttributeError after POST request in Django

I am making a post request to a Django server I am running locally. I am sending the request to http://127.0.0.1/login/. Here is the view

@csrf_exempt
def login(request):
    json_data = json.loads(request.body.decode('utf-8'))
    print(json_data)
    return request.body

I have @csrf_exempt for now only just so that I won't have to make a view to get the csrf token. When I send the POST request it works and prints out the json I sent with the request yet it also prints out this error.

Internal Server Error: /login/
Traceback (most recent call last):
  File "C:\Users\Moham\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Moham\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\deprecation.py", line 119, in __call__
    response = self.process_response(request, response)
  File "C:\Users\Moham\AppData\Local\Programs\Python\Python37\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'bytes' object has no attribute 'get'

The reason this is confusing me is because I have made no reference to any object called "bytes" or any attribute called "get". Anyone know what is going on here?

Upvotes: 0

Views: 271

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

View function should return HttpResponse object. Try this instead:

from django.http import JsonResponse

@csrf_exempt
def login(request):
    json_data = json.loads(request.body.decode('utf-8'))
    print(json_data)
    return JsonResponse(json_data)

Upvotes: 1

Related Questions