在某些情况下或直接阻止用户访问页面

时间:2017-12-08 11:30:10

标签: django redirect django-views

我有一个帐户模型,默认情况下有一个字段is_active(布尔值), 以及具有外键账户的公司模型:

class Account(Meta):
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=False)

在CBV的get方法中,我检查用户是否处于活动状态,如果不是,我将其重定向到不同的网页。

        if not account.is_active:
            return redirect('accounts:inactive')
        return account

地址:

path('inactive/', AccountInactiveView.as_view(), name='inactive'),

重定向视图:

class AccountInactiveView(TemplateView):
    template_name = 'accounts/account_inactive.html'

我不希望任何人直接访问重定向页面/网址,特别是如果帐户处于有效状态字段为True。

1 个答案:

答案 0 :(得分:1)

SELECT seller_id, COUNT(volume) AS volume FROM table GROUP BY seller_id HAVING COUNT(volume) >= 10 ORDER BY volume DESC 方法应返回get()。在那里返回HttpResponse是没有意义的。也许你想打电话给account

super()

最好覆盖def get(self, request, *args, **kwargs): account = self.request.user.account if not account: raise Http404 if not account.is_active: return redirect('accounts:inactive') return super(MyView, self).get(request, *args, **kwargs) ,以便对dispatchget请求进行检查。

相关问题