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
2 Answers
Reset to default 2In 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
版权声明:本文标题:javascript - AttributeError: 'dict' object has no attribute 'headers' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745454597a2659042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论