admin管理员组

文章数量:1425717

I have these two codes, i'm working with django, it's a request that asks for a specific project, and the python is supposed to give it back with the return function

def project(request):
    request_body = json.loads(request.body.decode('utf-8'))
    queryID = request_body['queryID']

    with open(os.path.join(BASE_DIR, 'projects//') + queryID + ".json", 'r') as f:
        response = json.loads(f.read())
        return response
const url = "/project"
let id_request = {"queryID":projectID}

let data = fetch(url, {
    method: 'POST',
    mode: "same-origin",
    headers: {
        "Accept": "network/json",
        "Content-Type": "network/json",
        "X-CSRFToken": csrftoken,
    },
    body: JSON.stringify(id_request)
    }
    )
    .then(response=>response.text())
    .then(data=>{ 
        console.log(data); 
    })

But when reading the json file I want to return, it throws an error

AttributeError: 'dict' object has no attribute 'headers'

I have these two codes, i'm working with django, it's a request that asks for a specific project, and the python is supposed to give it back with the return function

def project(request):
    request_body = json.loads(request.body.decode('utf-8'))
    queryID = request_body['queryID']

    with open(os.path.join(BASE_DIR, 'projects//') + queryID + ".json", 'r') as f:
        response = json.loads(f.read())
        return response
const url = "/project"
let id_request = {"queryID":projectID}

let data = fetch(url, {
    method: 'POST',
    mode: "same-origin",
    headers: {
        "Accept": "network/json",
        "Content-Type": "network/json",
        "X-CSRFToken": csrftoken,
    },
    body: JSON.stringify(id_request)
    }
    )
    .then(response=>response.text())
    .then(data=>{ 
        console.log(data); 
    })

But when reading the json file I want to return, it throws an error

AttributeError: 'dict' object has no attribute 'headers'
Share Improve this question edited Oct 12, 2022 at 12:23 deceze 523k88 gold badges800 silver badges943 bronze badges asked Oct 12, 2022 at 12:10 user18071724user18071724 2
  • 3 You must return a Response from a view, not a plain dict. – deceze Commented Oct 12, 2022 at 12:24
  • So it's crashing on json.loads(f.read())?- Might be a Json error?? Have you double checked that 'headers' in actually in the json (I don't even see you trying to fetch that key) – Nealium Commented Oct 13, 2022 at 16:36
Add a ment  | 

2 Answers 2

Reset to default 2

In Django, you shall return a response not Dict

from django.http import HttpResponse
with open(os.path.join(BASE_DIR, 'projects//') + queryID + ".json", 'r') as f:
    return HttpResponse(f.read())
   

The view is supposed to return a HttpResponse, in this case you likely want to use a JsonResponse [Django-doc], so:

from django.http import JsonResponse


def project(request):
    request_body = json.loads(request.body.decode('utf-8'))
    queryID = request_body['queryID']
    with open(os.path.join(BASE_DIR, 'projects//') + queryID + '.json') as f:
        return JsonResponse(json.load(f))

beware that using allowing to work with an arbitrary path is however very unsafe. Imagine that the person makes a request with ../../../../../secret.txt, then one could try to obtain a secret file that the user is not supposed to see. This is a school example of an insecure system.

本文标签: javascriptAttributeError 39dict39 object has no attribute 39headers39Stack Overflow