I am going to get whitebg_url
result from sqlite database according to matched items and made the api request using django like this.
@api_view(['POST'])
def getUrlFromAttributes(request):
try:
print('>>>>>>>>>>>>>>>>>>>>>', request)
hairstyle = request.data.get("hairstyle")
haircolor = request.data.get("haircolor")
skincolor = request.data.get("skincolor")
print("error>>1", str(hairstyle), str(haircolor), str(skincolor))
basic_query = BasicSetup.objects.all().filter(skin_color=skincolor, hair_color=haircolor, hair_style=hairstyle, isDeleted= 0)
print('returned basic query : ', basic_query)
lists_basicSetup = list(basic_query.values( 'whitebg_url'))
print('returned lists_basicSetup : ', lists_basicSetup)
return JsonResponse({'result': lists_basicSetup})
except Exception as error:
print("error", str(error))
return JsonResponse({'result': str(error)})
But as you can see the result at the below image, response is always None. I tried to find the solution from google, but can't do it.
I already add rest_framework
at INSTALLED_APPS in settings.py
file.
and path is like defined.
path('getUrlFromAttributes', views.getUrlFromAttributes, name='getUrlFromAttributes'),
I tried to do this using Postman, but the result was same.
Anyone knows why I was getting None
as result?
CodePudding user response:
The GET parameters are not stored in request.data
. Check request.GET
. See
Take a look at the (your) image above. Do you notice the Hairstyle, haircolor skincolor, etc parameters in the url?
That happens with a GET request. Yes, the log shows POST, meaning a POST event did happen.
There's all likeness that your data aren't actually POSTed, that's why you don't get them in the view function.
Suggestions:
- Cross check your JS file responsible for the POSTing (you can share here if you want).
- Try grabbing the parameters off the url instead (GET method, if non-sensitive information) if Your POSTing isn't working, just to test my theory.
request.query_params
- Read again Django RestFrameworks oc
CodePudding user response:
You can get request body as bytes and parse using JSONParser.
I just edited your code as below:
import io
from rest_framework.parsers import JSONParser
# Create your views here.
@api_view(['POST'])
def getView(request, *args, **kwargs):
try:
# you can get byte data from request body
print('>>>>>>>>>>>>>>>>>>>>>', request.body)
# get bytes and parse using json
# do these before
byte_body = io.BytesIO(request.body)
parsed_body = JSONParser().parse(byte_body)
hairstyle = parsed_body.get("hairstyle")
haircolor = parsed_body.get("haircolor")
skincolor = parsed_body.get("skincolor")
print("error>>1", str(hairstyle), str(haircolor), str(skincolor))
basic_query = BasicSetup.objects.all().filter(skin_color=skincolor, hair_color=haircolor, hair_style=hairstyle, isDeleted= 0)
print('returned basic query : ', basic_query)
lists_basicSetup = list(basic_query.values( 'whitebg_url'))
print('returned lists_basicSetup : ', lists_basicSetup)
return JsonResponse({'result': lists_basicSetup})
except Exception as error:
print("error", str(error))
return JsonResponse({'result': str(error)})
Just as a friend if you can, use class-based views and serializers.