重定向匿名用户登录(不显示任何内容) [英] Redirect anonymous users to log in (don't show them anything)

查看:104
本文介绍了重定向匿名用户登录(不显示任何内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.9.6。



我想绝对禁止整个网站被匿名用户查看。匿名用户将始终被重定向到登录页面。



我已经创建了一个普通视图。问题在于GeneralView的子类可能不仅会呈现模板,而且还会执行一些计算或只是不同种类的计算:DetailView,ListView等。

  class GeneralView(View):
def get(self,request,template):
如果不是request.user.is_authenticated()和request.user.is_active:
return redirect( auth_login)
else:
return render(request,template)

如果我尝试继承,代码就会变得笨拙:

  class HomePageView(GeneralView):
def get(self,请求,模板):
super()。get(self,request)

好吧,我在这里可以做什么?我收到一条错误消息,提示我的get方法未返回HttpResponse。



我可以重写超类的get方法以返回状态代码。然后在子类中检查它。但这似乎是垃圾。



换句话说,我迷失在继承的云端。您能否在这里给我一个方便,如何始终将匿名用户重定向到登录页面,而让登录用户看到所有内容。



请先谢谢您。

解决方案

您可以使用 UserPassesTestMixin



<来自django.core.urlresolvers的pre> import reverse_lazy

类GeneralView(UserPassesTestMixin,View):

def test_func(self):
#我假设您错过了问题的括号,并且实际上想要如果没有(request.user.is_authenticated()和request.user.is_active),则
返回request.user.is_authenticated()和请求.user.is_active

login_url = reverse_lazy('auth_login')

The mixin会覆盖 dispatch ,因此当y时您不必调用 super() ou覆盖视图中的 get()

 类HomePageView(GeneralView ):
def get(自身,请求):
...


Django 1.9.6.

I want to absolutely disable the whole website from viewing by anonymous users. Anonymous users will always be redirected to login page.

I have created a general view. The problem is that subclasses of GeneralView may not just render a template but perform some calculations or just be of different kinds: DetailView, ListView etc.

class GeneralView(View):
    def get(self, request, template):
        if not request.user.is_authenticated() and request.user.is_active:            
            return redirect("auth_login")
        else:
            return render(request, template)

If I try to inherit, the code gets clumsy:

class HomePageView(GeneralView):
    def get(self, request, template):
        super().get(self, request)

Well, what can I do here? I get error message that my get method doesn't return HttpResponse.

I can rewrite get method of the superclass to return status code. Then check it in the subclass. But this seems to be garbage.

In other words I'm lost in clouds of inheritance. Could you give me a kick here how always to redirect anonymous users to login page, whereas let logged in users see everything.

Thank you in advance.

解决方案

You could use the UserPassesTestMixin for this.

from django.core.urlresolvers import reverse_lazy

class GeneralView(UserPassesTestMixin, View):

    def test_func(self):
        # I assume you missed out the brackets in your question and actually wanted 'if not (request.user.is_authenticated() and request.user.is_active)'
        return request.user.is_authenticated() and request.user.is_active

    login_url = reverse_lazy('auth_login')

The mixin overrides dispatch, so you won't have to call super() when you override get() in your view.

 class HomePageView(GeneralView):
     def get(self, request):
         ...

这篇关于重定向匿名用户登录(不显示任何内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆