How to logout in Django?

Asked

Viewed 374 times

1

I managed to create and log a user into the system, but the logout does not work and I do not understand the reason.

View:

def logout_view(request):

     logout(request)
     return redirect('home')

html file that calls the url that in turn calls the view:

<div class="dropdown-menu ">

<a class="dropdown-item " href="#">Perfil</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'logout' %}">Logout</a>

</div>

Url:

path('home.html',views.logout_view,name='logout')

1 answer

2


Login to VIEWS

def pag_login(request):
if request.user.id:
    return HttpResponseRedirect('/index')
if request.POST:
    usuario = request.POST.get('usuario')
    senha = request.POST.get('senha')
    u = authenticate(username=usuario, password=senha)
    if u is not None:
        if u.is_active:
            login(request, u)
            next = request.GET.get('next')
            if next:
                return HttpResponseRedirect(next)
            else:
                return HttpResponseRedirect('/index')
        else:
            erro = 'Usuário inativo'
    else:
        erro = "Usuário ou senha inválido"
return render(request, 'login.html', locals())

Exit in VIEWS

@login_required
def sair(request):
    logout(request)
    return HttpResponseRedirect('/login')

URL

path('deslogar', sair), //você pode adequar isso com seu cod.

Move to HTML

<a href="deslogar">Sair</a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.