What is wrong with this procedure when saving a form?

4

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)
        )
    
asked by anonymous 16.09.2016 / 19:08

1 answer

1

To "register" users we have the option of using the django authentication framework. This fw includes the following models:

  • User: Model for users with the main fields: username, password, email, first_name, last_name and is_active.

  • group: Group names for user categorization

  • permission: Autoexplicative

To answer the question, let's develop an example in which we make user registrations, taking advantage of this framework. To focus on the email problem and make it simple, we'll only register "username" and your email.

I'll report on this response only to the form, view, and template development strategy. I've developed a complete example that you can download here .

First let's develop our form, which uses the FWD User model of django authentication, as mentioned before, in this form we will only include username and email.

from django import forms
from django.contrib.auth.models import User

class UserRegistrationForm(forms.ModelForm):

    class Meta:
        model = User
        fields = ('username', 'email')

Now our view (in core / views.py)

from django.shortcuts import render, redirect
from .forms import UserRegistrationForm

def create_account(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)

        if user_form.is_valid():
            new_user = user_form.save(commit=False)
            new_user.save()
            return render(request, 'core/register_done.html', {'new_user': new_user})
    else:
        user_form = UserRegistrationForm()
    return render(request, 'core/register.html', {'user_form': user_form})

The view is quite simple. If it is "called" by a GET, it tries to render a template register.html, if called by POST and the form has been validated, it will try to render the template register_done.html, both of the core app, let's create them

core / registe.html

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
    <link href="{% static "css/base.css" %}" rel="stylesheet">
    <title>Cadastro de usuários com email</title>
</head>
<body>
    <h1>Criando uma conta</h1>
    <p>Digite os dados solicitados abaixo:</p>
    <form action="." method="post">
        {{ user_form.as_p }}
        {% csrf_token %}
        <p><input type="submit" value="Criar minha conta" </p>
    </form>
</body>
</html>

core / register_done.html

{% load staticfiles %}
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <link href="{% static "css/base.css" %}" rel="stylesheet">
    <title>Cadastro de usuários com email</title>
</head>
<body>
    <h1>Registro Efetuado</h1>
    <p>Para criar outra conta <a href="{% url "create_account" %}"> Clique aqui</a>  </p>
</body>
</html>

With this you can register emails in a model, through a form. If you want, you can extend the User from Django to another model, such as a Profile. To test the complete code for this solution, clone the repository in git-hub

    
10.03.2017 / 13:35