Personalized login in Django, what is the best way to improve?

Asked

Viewed 418 times

0

At the moment I need to log in django.admin and login that I myself have created in a models.py | Class Pessoa. In this way it is not functional, because the admin needs to be logged in for a normal user to log on the main screen(Dashboard).

At the moment my Login is done this way:

    def login(request):
        form = PessoaForm(request.POST, None)
        if form.is_valid():
            return redirect('dashboard')
        return render(request, 'login.html', {'form': form})

I haven’t figured out a way to log out yet. I’m trying to develop a raw login, for example:

def login(request):
    i = 0
    counter = 10
    exitloop = False
    form = PessoaForm(request.POST, None)
    user = Pessoa.cnpj
    pwd = Pessoa.senha
    while i < counter or exitloop == 1:
        if user == Pessoa.cnpj and pwd == Pessoa.senha:
            exitloop = 1
            return redirect('dashboard')
    i = i+1
    return render(request, 'login.html', {'form': form})

But I still don’t think it’s the best way. Note: I need you to have a filter @login_required but with this custom login and a way to log out.

I appreciate any direction or help for both coding and syntax.

1 answer

1


From what I understand, you want to do a Custom Login, so you will have to study "Abstractbaseuser".

And for logout it’s simple:

1.Use these commands in views.py

2.There are libraries for Logout and redirect URLS; (It will be in the code below)

3.Be happy.

#bibliotecas
from django.contrib.auth import logout
from django.http import HttpResponseRedirect, HttpResponse


#Exija que o usuário esteja logado para executar o comando, isso evitará futuros problemas.
@login_required
#coloque o nome diferente que logout para não gerar conflito com o comando importado logout
def logout_view(request):
    #executarará logout
    logout(request)
    # Realizará o logout e irá para pagina desejada,neste caso INDEX.
    return HttpResponseRedirect(reverse('index'))

Link: https://docs.djangoproject.com/en/2.1/topics/auth/default/

Browser other questions tagged

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