Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

Django UserCreationForm not working

This is my registration form view:

from django.contrib.auth.forms import UserCreationForm

class RegistroUsuario(FormView):
    template_name = "perfiles/registro_usuario.html"
    form_class = UserCreationForm
    success_url = reverse_lazy("cliente:mi_perfil") 

    def form_valid(self, form):
        print("form_valid")
        return super(RegistroUsuario, self).form_valid(form)

It prints "form_valid". But it doesn't actually create a user. It redirects to the success_url but when I check which user is "logged in", it is an AnonymousUser. I don't understand.

Upvotes: 0

Views: 1072

Answers (1)

danielcorreia
danielcorreia

Reputation: 2136

When using the FormView class the form_valid method doesn't save the form for you. You need to add it, like this:

def form_valid(self, form):
    form.save()
    return super(RegistroUsuario, self).form_valid(form)

That behaviour is used by CreateView and UpdateView.

Upvotes: 4

Related Questions