Django login view only works with users created in createsuperuser

0

I've been busting my head with this problem for a few days. I am doing a project for college and created a custom user, I can add users normally in my template, they are committed in the bd, but in the login template, they do not log in. I can only log in if the user is created by the createsuper user command or if I create them through the admin panel.

This is my forms.py

class UserForm(forms.ModelForm):
    password1 = forms.CharField(label='Senha')
    password2 = forms.CharField(label='Confirmar Senha')

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError('Senhas não conferem')
            return password2

    def save(self, commit=True):
        user = super(UserForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1'])
        if commit:
            user.save()
        return user

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email']

class UserAdminCreationForm(UserCreationForm):
    class Meta:
        model = UserAdmin
        fields = ['first_name', 'last_name', 'email', 'active', 'is_staff', 'is_admin']

class UserAdminForm(forms.ModelForm):
    class Meta:
        model = UserAdmin
        fields = ['first_name', 'last_name', 'email']

Here I have my views.py

from django.shortcuts import render, redirect
from .forms import UserForm
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout, update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib import messages

def add_user(request):
    if request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            u = form.save()
            u.set_password(u.password)
            u.save()
            return HttpResponse('Usuario Cadastrado!')
    else:
        form = UserForm()
    return render(request, 'auth/registro.html', {'form': form})

def user_login(request):
    if request.method == 'POST':
        email = request.POST.get('email')
        password = request.POST.get('password')

        user = authenticate(email=email, password=password)

        if user:
            login(request, user)
            return redirect(request.GET.get('next', 'financas:home'))
        else:
            messages.error(request, 'Usuário ou senha inválidos')
    return render(request, 'auth/login.html')

I've already tried some solutions, but I did not get anything that could help me.

    
asked by anonymous 27.05.2018 / 01:46

1 answer

0

Felipe, in your view you are passing an email on authenticate, looking at the documentation ( link ) it only accepts USERNAME.

    
15.06.2018 / 01:43