无法访问Django发布请求密钥

时间:2019-05-24 12:15:07

标签: python django post request postman

我编写了一个Django Web应用程序,该项目有一个功能可以向客户端发出请求,该请求有一个键,该键为“ tokene” 当我用Postman发送此请求时,我的函数无法找到“ tokene”键,因此我编写了一个小的python代码发送Request,此代码有效! 当我使用邮递员时,我的回答是“请发送令牌!”和 当我使用我的代码时,响应是一个Json 为什么?

我的邮递员网址

http://localhost:8000/submit/expensequery?tokene=123456789

# its my python code 
import requests
r = requests.post('http://localhost:8000/submit/expensequery/', data={'tokene':'123456789'})
print (r.url)





# its my webapplications function 
def query_expenses(request):
    if (request.POST.has_key('tokene')):
        this_token = request.POST['tokene']
        num = request.POST.get('num', 10)
        this_user = get_object_or_404(User, token__token=this_token)
        expenses = 
        expense.objects.filter(user_name=this_user).order_by('-date') 
        [:num]
        expenses_serialized = serializers.serialize("json", expenses)
        return JsonResponse(expenses_serialized, encoder=JSONEncoder, safe=False)
    else:
        return HttpResponse('please send token !')



    #its my application urls.py
urlpatterns = [
        url(r'^accant/',include('accant.urls')),
        url(r'',include('web.urls')),



    ]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)



    #its my web.urls.py

    urlpatterns = [
       url(r'^submit/expense/$',views.expense_func),
       url(r'^submit/income/$',views.income_func),
       url(r'^submit/expensequery/?$',views.query_expenses),
       url(r'^/test/',views.test),
    ]
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

1 个答案:

答案 0 :(得分:0)

不推荐使用has_key功能,而推荐使用'key' in request.POST。因此,您的if条件将不起作用。您应该if 'tokene' in request.POST: