I have a registration form and I'm trying to save information on it.
When trying to register, it returns a form error saying that the email already exists (even registering different types of e-mail). That is, it is saving before the checks if the form is valid, however there is no instruction clarifying this.
class CadastroCreateView(CreateView):
form_class = CadastroForm
success_url = '/?cadastro=1'
template_name = 'signup.html'
model = Cadastro
def get_context_data(self, **kwargs):
context = super(CadastroCreateView, self).get_context_data(**kwargs)
context.update({
'estados':U.STATE_CHOICES,
'culturas':Cultura.objects.all(),
'interesses': Topico.objects.all(),
})
return context
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance and its inline
formsets with the passed POST variables and then checking them for
validity.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
if (form.is_valid()):
return self.form_valid(form)
else:
return HttpResponse('oi')
return self.form_invalid(form)
def form_valid(self, form):
"""
Called if all forms are valid. Creates a Recipe instance along with
associated Ingredients and Instructions and then redirects to a
success page.
"""
self.object = form.save()
u = authenticate(username=self.object.email, password=self.object.senha)
authlogin(self.request, u)
return redirect('conteudos:home')
def form_invalid(self, form):
"""
Called if a form is invalid. Re-renders the context data with the
data-filled forms and errors.
"""
return self.render_to_response(
self.get_context_data(form=form)
)