How do I use the The login_required decorator in my URL?

前端 未结 4 1951
暗喜
暗喜 2021-02-18 19:22

I want to check that the user is authorized for certain URLs. I\'m using generic views.

The docs here say the login_required can be passed as an optional arguments but I

4条回答
  •  旧时难觅i
    2021-02-18 19:57

    In Django 1.11+, at least, you can do it directly as you want. For example:

    # urls.py
    
    from django.contrib.auth.decorators import login_required
    
    urlpatterns = [
        # Home path
        path('', login_required(TemplateView.as_view(template_name='core/home.html')), name='home'),
        # Another paths
        # ...
    ]
    

    In this case, each time you try to enter the homepage, you must be logged in, otherwise you will go to the login screen and then return to your homepage.

提交回复
热议问题