5
I have a registration form and I’m trying to save information on it.
When trying to register, it returns a form error saying the email already exists (even if registering different types of email). That is, it is saving before the checks whether the form is valid, but 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)
)
Dude, if you need to override a post, get, form_valid and invalid, review your idea of using class based view. This view can be greatly reduced if you go to Function based view.
– Puam Dias
And you can remove this implementation from your post and test, it’s not doing anything there. You can leave only form_valid
– Puam Dias
Right. Thank you very much.
– Rukado