“Connexion requise Django” Réponses codées

Comment importer la connexion requise dans Django

from django.contrib.auth.decorators import login_required

@login_required(login_url='/example url you want redirect/')
Energetic Echidna

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

Connexion requise Django

from django.conf import settings
from django.shortcuts import redirect

def my_view(request):
    if not request.user.is_authenticated:
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
    # ...
Puzzled Puffin

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

LoginrequiredMixin Django

from django.contrib.auth.mixins import LoginRequiredMixin

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

Réponses similaires à “Connexion requise Django”

Questions similaires à “Connexion requise Django”

Plus de réponses similaires à “Connexion requise Django” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code