“Django Login_Required Decorator” Réponses codées

Login_reQuired sur la classe Django

You can add the decorator in the urls.py

from django.contrib.auth.decorators import login_required
 url(r'^workers/$', login_required(views.RootWorkerView.as_view()))
BlueMoon

Login_reQuired sur la classe Django

now you can use Django builtin LoginRequiredMixin

from django.contrib.auth.mixins import LoginRequiredMixin

class MyView(LoginRequiredMixin, View):
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
BlueMoon

Django Connexion requise

def login_view(request):
    if request.method == 'GET':
        cache.set('next', request.GET.get('next', None))

    if request.method == 'POST':
        # do your checks here

        login(request, user)

        next_url = cache.get('next')
        if next_url:
            cache.delete('next')
            return HttpResponseRedirect(next_url)

    return render(request, 'account/login.html')
Gleaming Guanaco

Django Login_Required Decorator

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...
    


# login_required() does the following:

# If the user isn’t logged in, redirect to settings.LOGIN_URL,
# passing the current absolute path in the query string.
# Example: /accounts/login/?next=/polls/3/. If the user is logged in,
# execute the view normally. The view code is free to assume the user is logged in.
# By default, the path that the user should be redirected to upon successful authentication is stored in a query string parameter called "next".
# If you would prefer to use a different name for this parameter, 
# login_required() takes an optional redirect_field_name parameter:




from django.contrib.auth.decorators import login_required

@login_required(redirect_field_name='my_redirect_field')
def my_view(request):
    ...




# Note that if you provide a value to redirect_field_name,
# you will most likely need to customize your login template as well,
# since the template context variable which stores the redirect path will use the value of redirect_field_name as its key rather than "next" (the default).




login_required() also takes an optional login_url parameter. Example:

from django.contrib.auth.decorators import login_required

@login_required(login_url='/accounts/login/')
def my_view(request):
    ...




# Note that if you don’t specify the login_url parameter,
# you’ll need to ensure that the settings.LOGIN_URL and your login view are properly associated.
# For example, using the defaults,
# add the following lines to your URLconf:




from django.contrib.auth import views as auth_views

path('accounts/login/', auth_views.LoginView.as_view()),




# The settings.LOGIN_URL also accepts view function names and named URL patterns.
# This allows you to freely remap your login view within your URLconf without having to update the setting.
OHIOLee

Réponses similaires à “Django Login_Required Decorator”

Questions similaires à “Django Login_Required Decorator”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code