'Always get None from Django api request as result
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?
Solution 1:[1]
The GET parameters are not stored in request.data. Check request.GET. See documentation
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vinkomlacic |
| Solution 2 | Michael Amadi |


