Django中间件使POST请求为空

4
如果我使用中间件,发送的帖子数据会被捕获为空白,如果我从settings.py文件中删除中间件,则返回帖子数据。我做错了什么。

enter image description here

当我删除中间件时,这是正确的结果

enter image description here

这是中间件代码:缩进可能有误

class CheckAuthorization(object):
def process_request(self, request):
    getKey = request.POST.get('authKey')
    token = request.POST.get('token')
    full_path = request.get_full_path()
    if getKey is not None and getKey != '' and full_path != "/users/splash/":
        try:
            auth = TblAutherization.objects.get(secret_key = request.POST.get('authKey'))
        except TblAutherization.DoesNotExist:
            response = JsonResponse({'Status':'Error','Response code': 107,'Message':full_path})
            return HttpResponse(response, content_type='application/json')
    else:
        return None
    if token is not None and token != '' and  'settoken' in request.session and full_path != "/users/splash/":
        try:
            auth = TblLoginAuth.objects.get(token = request.POST.get('token'))
        except TblLoginAuth.DoesNotExist:
            response = JsonResponse({'Status':'Error','Response code': 107,'Message':'Invalid Request'})
            return HttpResponse(response, content_type='application/json')
    else:
        return None 

views.py

def splash(request):
if request.method == 'POST':
    try:
        appId = request.POST.get('appId')
    except: 
        return Response({'Status':appId})

    gcmToken = request.POST.get('gcmToken')
    deviceType = request.POST.get('deviceType')
    userId = request.POST.get('userId')
    loginStatus = request.POST.get('loginStatus')
    appType=request.POST.get('appType')
    return Response({'appId':appId,'gcmToken':gcmToken,'deviceType':deviceType})
else:
    return Response({'appId':appId,'gcmToken':gcmToken,'deviceType':deviceType})

除非我使用try except,否则它甚至不会进入。


process_request 看起来没问题。从截图中可以看到,json 响应有键,但值为 null。也就是说,逻辑进入了你编写这个 json response 的地方。也许你应该检查一下处理 /users/splash/ 视图的逻辑。在返回响应之前,你可以打印一下响应。顺便问一下,这个中间件里有 process_response 吗? - Han He
我在读取帖子参数后开始打印响应。我学到的是,如果我在中间件中使用request.POST.get方法,它会使我的请求为空,如果我不在中间件中使用它,它会让我的帖子参数保持不变。这真的很奇怪,我被卡住了,没有任何线索。 - ankush madaan
很奇怪。request.POST 是一个 MultiValueDict 实例,它是一个扩展的 python dictget 方法不会清除键和值。也许你可以发布处理 /users/splash/ 的视图代码,我可以看看是否有什么问题。 - Han He
你好,请检查问题,我已经编辑并添加了view.py文件。 - ankush madaan
2个回答

2

当我在实现日志中间件时,遇到了同样的问题。如果你在中间件中尝试访问POST字典中的数据,Django会将其全部删除。我不知道这是一个错误还是一个特性,但我想出了一个临时解决方案:你可以使用request.body来获取数据。

body = request.body.decode('utf-8')  # in python 3 json.loads only accepts unicode strings 
body = json.loads(body)
content = body['content']

你的回答不是很清楚。我不确定应该在中间件还是视图中使用上面的代码? - user1012513

0

我不是中间件方面的专家,但我知道我们在项目中使用它,我看到的主要区别是在成功的情况下你没有返回响应。所以我看到你有 try: auth = blah,但你没有使用它。因此,你总是通过 HttpResponse() 返回 None 或错误。那成功呢?


是的,我理解了,并且在这种情况下也会在末尾返回none,但是目前这个中间件的问题是对于此请求,两个if条件都为false,这意味着它将进入else条件并返回none,因此此请求不会发生成功的情况。 - ankush madaan
但是如果你传递None值,那么你的数据将去哪里呢? - Macainian
我不需要任何成功案例的数据,我只需要在失败的情况下进行检查,这就是文档中所提到的 process_request 的作用。它会返回 HttpResponse 或者 None,在后一种情况下,它会继续请求视图。 - ankush madaan
啊,我明白了。我刚才又看了一遍我们的代码,我们正在做同样的事情。我之前看错了。你发帖后还有什么新发现吗? - Macainian
暂无操作,还没有结果。等待有人帮忙处理。我不想把这段代码放在我的视图中。 - ankush madaan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接