Django HttpResponse returning object instead of stated string

alvst :

I am using Django and React. I need to send a string from Django to React. I have tried using both HttpResponse and JsonResponse but both seem to be returning a response object that does not include the data.

The response object from React looks like this:

Response {type: "cors", url: "http://localhost:8000/post/", redirected: false, status: 200, ok: true, …}
   body: (...)
   bodyUsed: false
   headers: Headers {}
   ok: true
   redirected: false
   status: 200
   statusText: "OK"
   type: "cors"
   url: "http://localhost:8000/post/"
   __proto__: Response

Here is my Django

from django.http import HttpResponse, JsonResponse

def index(request):
    string1 = ""
    
    if request.method == 'POST':
        # ...processing upload...
        string1 = "Hello World"

    return HttpResponse(string1)

And my react request looks like this:

async function handleSubmit(event) {
    let formData = new FormData();
    formData.append(file)
    fetch("http://localhost:8000/post/", {
       method: "POST",
       body: formData,
       enctype: "multipart/form-data",
    }).then((res) => {
      console.log(res);
    });

How can I get the data I need (string1) included in the response object (or without the response object)? I've looked on StackOverflow and around the web and haven't found a solution. I am also not sure whether this is a problem with Django or React but it seems like a Django problem. (Also, I don't believe it is a CORS Problem as I have CORS are allowed.

Thanks

Willem Van Onsem :

It does include the data, as payload, but that makes it less accessible, especially if you later plan to pass multiple items. You can wrap the result in a JSON blob:

from django.http import JsonResponse

def index(request):
    string1 = ''
    
    if request.method == 'POST':
        # … processing upload …
        string1 = 'Hello World'

    return JsonResponse({'result': string1})

In the JavaScript part, you can then interpret the response as JSON and deserialize it:

async function handleSubmit(event) {
    let formData = new FormData();
    formData.append(file)
    fetch("http://localhost:8000/post/", {
       method: "POST",
       body: formData,
       enctype: "multipart/form-data",
    }).then(response => {
      response.json().then(data => {
        console.log(data.result);
      })
    })
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django views.register returning none instead of a HTTPresponse

From Dev

Returning python object in httpresponse

From Dev

View not returning an HttpResponse object

From Dev

Django View not returning httpresponse

From Java

Thymeleaf returning a String array instead of List<Object>

From Dev

Linq select returning string instead of object

From Dev

simple string split is returning object instead array

From Dev

jquery val returning object instead of string

From Dev

Returning SVG object or XML string instead of an Image

From Dev

How to fix a Django view that is not returning an HttpResponse Object? (CS50 Project 1)

From Dev

Django ValueError - didn't return an HttpResponse object. It returned None instead

From Dev

Python/Django: The view xxxxx didn't return an HttpResponse object. It returned None instead

From Dev

Django: View didn't return an HttpResponse object. It returned None instead

From Dev

Django says "didn't return an HttpResponse object. It returned None instead."

From Dev

How to fix Django's the view didn't return an HttpResponse object. It returned None instead?

From Dev

Django - The view accounts.decorators.wrapper_function didn't return an HttpResponse object. It returned None instead

From Dev

Value error: view didn't return an HttpResponse object. It returned None instead Django

From Dev

Django Class view didn't return an HttpResponse object. It returned None instead

From Dev

How to fix Django : didn't return an HttpResponse object. It returned None instead?

From Dev

Django: The view didn't return an HttpResponse object. It returned None instead

From Dev

The view ... didn't return an HttpResponse object. It returned None instead - django

From Dev

DJANGO,DRF:request id returning a NONE value instead the user object

From Dev

Foreign key value is returning as an object instead of id in datatables in django

From Dev

Typeahead returning object count instead of selectable strings in Django

From Dev

Django post request is returning querydict instead of object id for model form

From Dev

jQuery ajax call is returning response data as String instead of Object

From Dev

Why json.loads is returning a unicode object instead of string

From Dev

ng-options in select returning an Object instead of String

From Dev

jquery.data returning json string instead of object

Related Related

  1. 1

    Django views.register returning none instead of a HTTPresponse

  2. 2

    Returning python object in httpresponse

  3. 3

    View not returning an HttpResponse object

  4. 4

    Django View not returning httpresponse

  5. 5

    Thymeleaf returning a String array instead of List<Object>

  6. 6

    Linq select returning string instead of object

  7. 7

    simple string split is returning object instead array

  8. 8

    jquery val returning object instead of string

  9. 9

    Returning SVG object or XML string instead of an Image

  10. 10

    How to fix a Django view that is not returning an HttpResponse Object? (CS50 Project 1)

  11. 11

    Django ValueError - didn't return an HttpResponse object. It returned None instead

  12. 12

    Python/Django: The view xxxxx didn't return an HttpResponse object. It returned None instead

  13. 13

    Django: View didn't return an HttpResponse object. It returned None instead

  14. 14

    Django says "didn't return an HttpResponse object. It returned None instead."

  15. 15

    How to fix Django's the view didn't return an HttpResponse object. It returned None instead?

  16. 16

    Django - The view accounts.decorators.wrapper_function didn't return an HttpResponse object. It returned None instead

  17. 17

    Value error: view didn't return an HttpResponse object. It returned None instead Django

  18. 18

    Django Class view didn't return an HttpResponse object. It returned None instead

  19. 19

    How to fix Django : didn't return an HttpResponse object. It returned None instead?

  20. 20

    Django: The view didn't return an HttpResponse object. It returned None instead

  21. 21

    The view ... didn't return an HttpResponse object. It returned None instead - django

  22. 22

    DJANGO,DRF:request id returning a NONE value instead the user object

  23. 23

    Foreign key value is returning as an object instead of id in datatables in django

  24. 24

    Typeahead returning object count instead of selectable strings in Django

  25. 25

    Django post request is returning querydict instead of object id for model form

  26. 26

    jQuery ajax call is returning response data as String instead of Object

  27. 27

    Why json.loads is returning a unicode object instead of string

  28. 28

    ng-options in select returning an Object instead of String

  29. 29

    jquery.data returning json string instead of object

HotTag

Archive