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.
###user submit expense###
if request.method == 'POST':
this_token = request.POST.get('token')
this_token = request.GET['token']
# this_user = User.objects.get(token__token=this_token)
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
this request is post beacuse retun none
I don’t understand what you’re trying to say here.
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.
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
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:
List all snippets, or create a new snippet
def get(self, request, format=None):
snippets = Snippet.objects.all()
serializer = SnippetSerializer(snippets, many=True)
def post(self, request, format=None):
serializer = SnippetSerializer(data=request.data)
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
I’m using the postman to make tests
I’ve trying to call in differents forms (with ’ - " - ; - etc)but nothing works
[04/Jun/2023 16:06:58] "POST /snippets/?language=Lua;title=Brazilian_Language HTTP/1.1" 201 85
[04/Jun/2023 16:26:41] "POST /snippets/?language=Lua;title=%22Brazilian_Language%22 HTTP/1.1" 201 85
[04/Jun/2023 16:27:26] "POST /snippets/?language=%27lua%27 HTTP/1.1" 201 85
[04/Jun/2023 16:27:44] "POST /snippets/?language=%27lua%27&title=%27brazilian_language%27 HTTP/1.1" 201 85
After all day researching this I find a solution but a not complete explanation haha
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.