Just working through the Django tutorials and playing around with stuff. Was going through the HttpResponse and HttpRequest docs and I'm just trying to print out data to see how things work.
However, when I try to print to console the request.body
I get nothing back.
def detail(request, question_id):
current_question_selected = Question.objects.get(pk=question_id)
choices_for_question = current_question_selected.choice_set.all()
context = {"choices_for_question":choices_for_question, "current_question_selected":current_question_selected}
#print(request.META["REMOTE_USER"])
print(request.body)
return render(request, 'polls/detailtext.html', context)
This is what gets pritned to the screen, the letter 'b' with an empty string
[28/Jun/2022 10:59:56] "GET /polls/1/ HTTP/1.1" 200 456
b''
Not sure what i'm missing
CodePudding user response:
The print displays an empty string because GET does not accept a body. Taken directly from the Mozilla Web APIs docs:
Note that a request using the GET or HEAD method cannot have a body and null is return in these cases.
If you want to pass data in a GET request, you need to pass them as parameters instead. Then, you can access the parameters using request.GET
(HttpRequest.GET) or access them individually with request.GET.get('key')
.