Django, redirect all non-authenticated users to landing page

前端 未结 8 1459
挽巷
挽巷 2021-01-30 23:08

I have a django website with many urls and views. Now I have asked to redirect all non-authenticated users to a certain landing page. So, all views must check if user.is_a

8条回答
  •  春和景丽
    2021-01-31 00:09

    Maybe too late but in django 1.9+ it's too easy. Django introduced Login Required mixin for generic classes and this a great example here by William S. Vincent

    simply in your view add LoginRequiredMixin as parent class

    from django.contrib.auth.mixins import LoginRequiredMixin
    
    class BlogUpdateView(LoginRequiredMixin, UpdateView):
    model = Post
    template_name = 'post_edit.html'
    fields = ['title', 'body']
    

    Also you can use login_required decorator for method request

提交回复
热议问题