【问题标题】:Django - server code response in JsonResponseDjango - JsonResponse 中的服务器代码响应
【发布时间】:2017-03-05 06:37:32
【问题描述】:

是否可以将服务器响应代码添加到 JsonResponse ?在某些情况下,我需要服务器回复 404。

我有以下看法

def CreateOrAuth(request):
try: 
    username = request.POST.get("username")

    queryset = User.objects.get(username=username)

except Exception as e:
    return JsonResponse({'status': 'user with {} not exist'.format(username)})

我想在这里添加404服务器代码

【问题讨论】:

标签: django


【解决方案1】:

是的,你可以。只需将附加参数status 传递给JsonResponse

return JsonResponse({'status': 'user with {} not exist'.format(username)}, status=404)

【讨论】:

    猜你喜欢
    • 2012-01-31
    • 2012-08-10
    • 2020-10-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2018-05-29
    相关资源
    最近更新 更多