Django login view only works with users created in createsuperuser

Asked

Viewed 356 times

0

I’ve been racking my brain with this problem for a couple of days. I’m doing a project for college and created a custom user, I can add users normally in my registration 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 by the administrator 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 looked for some solutions, but I couldn’t get anything that could help me.

  • Filipe shares the user definition in your models.py file, I also override the user and use email instead of username, however your create does not leave the user active by default, right? You have tried, in your add_user add the line u.is_active = True?

1 answer

0

  • Missing an example to complement the answer

Browser other questions tagged

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