why the request post query return null in my code???
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from web.models import User,Token,Expense,Income
from datetime import datetime
# Create your views here.
@csrf_exempt
def submit_expense(request):
###user submit expense###
#TODO:
if request.method == 'POST':
this_token = request.POST.get('token')
else:
this_token = request.GET['token']
# this_user = User.objects.get(token__token=this_token)
print(this_token)
return JsonResponse({
"request":request.POST
} )
Side note: When posting code here, you need to use the backtick characters - `, not the apostrophe - ' or any “smart quotes”. The lines of three backticks also need to be lines by themselves. They cannot be parts of other lines.
(I have corrected your post this time.)
You will get a null response on any GET
to this view. If the request
is a GET
, then there’s nothing in request.POST
.
this request is post beacuse retun none
I don’t understand what you’re trying to say here.
If a GET
request is made to this view, or if a POST
is made without any POST
data being supplied, the results are going to be the same.
How is this view being invoked? Is this a link from a page? Are you making a request using JavaScript?
Please provide more details and be more explicit about what exactly is happening here.
I’m post form to URL ، and I’m going to take the values and compare it to the token get user from data base and creat one expense in database
but this requst post retun is none
this json for show return only
Hey guys,
I’m getting the same issue.
Ok I’m begginer and trying to do the tutorials from the website, part3 now (3 - Class based views - Django REST framework)
This time I was trying the post method, and I realise that when I was passing values in the post method it wouldn’t saving the data in url, then I put some prints into the code to see and yes the ‘request’ are empty.
Just the class with the post method:
class SnippetList(APIView):
"""
List all snippets, or create a new snippet
"""
def get(self, request, format=None):
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data)
print(request.body)
print(serializer.initial_data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.erros, status=status.HTTP_400_BAD_REQUEST)
the prints in the server log
[04/Jun/2023 15:46:29] "POST /snippets/?language=Lua&title=Brazilian_Language HTTP/1.1" 201 85
b''
{}
I’m using the postman to make tests
Thanks
I’ve trying to call in differents forms (with ’ - " - ; - etc)but nothing works
b''
None
{}
[04/Jun/2023 16:06:58] "POST /snippets/?language=Lua;title=Brazilian_Language HTTP/1.1" 201 85
b''
None
{}
[04/Jun/2023 16:26:41] "POST /snippets/?language=Lua;title=%22Brazilian_Language%22 HTTP/1.1" 201 85
b''
None
{}
[04/Jun/2023 16:27:26] "POST /snippets/?language=%27lua%27 HTTP/1.1" 201 85
b''
None
{}
[04/Jun/2023 16:27:44] "POST /snippets/?language=%27lua%27&title=%27brazilian_language%27 HTTP/1.1" 201 85
``
@abtin1238
After all day researching this I find a solution but a not complete explanation haha
Look this:
request.data returns the parsed content of the request body.
This is similar to the standard request.POST and request.FILES attributes
request.query_params is a more correctly named synonym for request.GET.
For clarity inside your code, we recommend using request.query_params instead of the Django's standard request.GET.
Doing so will help keep your codebase more correct and obvious - any HTTP method type may include query parameters, not just GET requests.
So in my case I just turned my “request.data” (or request.POST in your case, although I’ve tryed with this too) for request.query_params and works well.
But I still don’t know why even I’m sending a POST url method the values are stored in request.GET and not in request.data or request.POST that are empty.
Unless I’m doing something wrong (and it has a large probability) the tutorials have some issues.