Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django HttpResponseForbidden不工作_Python_Django_Django Views - Fatal编程技术网

Python django HttpResponseForbidden不工作

Python django HttpResponseForbidden不工作,python,django,django-views,Python,Django,Django Views,我在is_send_allowed_interceptor中排除了这段代码,如果为true,则停止处理并重定向到forbidden。但是,它不会返回函数中的HttpResponseForbidden对象 如何让HttpResponseForbidden()在这个上下文中运行 @login_required def process_all(request): #If we had a POST then get the request post values. if request

我在is_send_allowed_interceptor中排除了这段代码,如果为true,则停止处理并重定向到forbidden。但是,它不会返回函数中的HttpResponseForbidden对象

如何让HttpResponseForbidden()在这个上下文中运行

@login_required
def process_all(request):
    #If we had a POST then get the request post values.
    if request.method == 'POST':
        batches = Batch.objects.for_user_pending(request.user)

        # Will redirect/cancel request if user does not meet requirements, funds, permissions etc
        is_send_permitted_interceptor(request)
        # stuff here if everything is ok


def is_send_permitted_interceptor(request):
    # Check user has required credits in account to these batches.
    balance = Account.objects.get(user=request.user).get_balance()
    cost_of_sending = Batch.objects.batches_cost_pending(user=request.user)
    if balance < cost_of_sending:

        return HttpResponseForbidden()
    else:
        pass
@需要登录\u
def过程_全部(请求):
#如果我们有一个POST,那么获取请求POST值。
如果request.method==“POST”:
batches=Batch.objects.for\u user\u pending(request.user)
#如果用户不满足要求、资金、权限等,将重定向/取消请求
是否允许发送截取器(请求)
#如果一切都好的话,把东西放在这里
def是允许发送的截取器(请求):
#检查用户帐户中是否有这些批次所需的积分。
balance=Account.objects.get(user=request.user).get\u balance()
发送成本=批处理。对象。批处理成本\u挂起(用户=请求。用户)
如果余额<发送成本:
返回HttpResponseForbidden()
其他:
通过

您在
进程\u all
视图中不返回“拦截器”的输出,因此它永远不会到达用户

只需在拦截器中实现您的逻辑,但仅在需要时从主视图返回

def is_send_permitted(request):
    # Check user has required credits in account to these batches.
    balance = Account.objects.get(user=request.user).get_balance()
    cost_of_sending = Batch.objects.batches_cost_pending(user=request.user)
    if balance < cost_of_sending:
        return False
    else:
        return True


@login_required
def process_all(request):
    #If we had a POST then get the request post values.
    if request.method == 'POST':
        batches = Batch.objects.for_user_pending(request)

        # Will redirect/cancel request if user does not meet requirements, funds, permissions etc
        if not is_send_permitted_interceptor(request):
            return HttpResponseForbidden()
        # stuff here if everything is ok

但是,在没有真正异常发生的情况下引发异常是一种不好的做法。

您没有在
process\u all
视图中返回“interceptor”的输出,因此它永远不会到达用户

只需在拦截器中实现您的逻辑,但仅在需要时从主视图返回

def is_send_permitted(request):
    # Check user has required credits in account to these batches.
    balance = Account.objects.get(user=request.user).get_balance()
    cost_of_sending = Batch.objects.batches_cost_pending(user=request.user)
    if balance < cost_of_sending:
        return False
    else:
        return True


@login_required
def process_all(request):
    #If we had a POST then get the request post values.
    if request.method == 'POST':
        batches = Batch.objects.for_user_pending(request)

        # Will redirect/cancel request if user does not meet requirements, funds, permissions etc
        if not is_send_permitted_interceptor(request):
            return HttpResponseForbidden()
        # stuff here if everything is ok

但在没有真正异常发生时引发异常是一种不好的做法。

您需要在调用者中添加
return
,因为您的check函数将返回调用者,它的值就是您想要返回到浏览器的值

更好的办法是:

def is_send_permitted_interceptor(user):
    # Check user has required credits in account to these batches.
    balance = Account.objects.get(user=user).get_balance()
    cost_of_sending = Batch.objects.batches_cost_pending(user=user)
    return balance < cost_of_sending

这样,视图方法就是所有重定向发生的地方;您不必传递
请求

您需要在调用者中添加
返回
,因为您的check函数将返回调用者,它的值就是您想要返回到浏览器的值

更好的办法是:

def is_send_permitted_interceptor(user):
    # Check user has required credits in account to these batches.
    balance = Account.objects.get(user=user).get_balance()
    cost_of_sending = Batch.objects.batches_cost_pending(user=user)
    return balance < cost_of_sending

这样,视图方法就是所有重定向发生的地方;并且您不必传递
请求

如果允许操作,这将有效地
返回None
。是的,这就是为什么建议使用第二种方法。如果允许操作,这将有效地
返回None
。是的,这就是为什么建议使用第二种方法。您可以尝试提高
PermissionDenied
。如果条件
balance
失败,则当前代码不会从
process\u all()
视图返回…您可以尝试提高
PermissionDenied
。如果条件
平衡失败,则当前代码不会从
过程_all()
视图返回。。。