1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Apply django authentication for all views

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to implement Django basic authentication for all of the views in my views.py file. Although I can add the authentication code snippet in every view, but it will not be easy to apply this to upcoming views. Is there any way that every view in my views.py will automatically check for the authentication?

    views.py


    def mgmt_home(request):
    ##############################################################
    # This code is repetitive
    ##############################################################
    if request.user.is_anonymous:
    return redirect("/login")
    ##############################################################

    test_name = Test.objects.all()[0].test_name

    metadata = {
    "test_name": test_name,
    }
    return render(request, "mgmt_home.html", metadata)


    Is there any way where I can avoid this repetitive code in all of my views?

    Continue reading...

Compartilhe esta Página