0

my from is not sending data to database here is my view.py and form.py And yet they are no error reported on my console

views.py

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        return redirect('../login/')
    else:
        form = RegistrationForm()

        args = {'form': form}
        return render(request, 'account/register.html', args)

forms.py


class RegistrationForm(UserCreationForm):
    # first_name forms.CharField(... that i cut here to win some space
    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']

        if commit:    
            user.save()

        return user
XetaWays
  • 23
  • 1
  • 5

1 Answers1

0

You forgot to call form.save() in your view. That's why your form is never saved.

Fix:

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        form.save()  # <- ATTENTION.
        return redirect('../login/')
    else:
        form = RegistrationForm()

    return render(request, 'account/register.html', {
        'form': form,
    })

Side notes

Don't hardcode the redirect path (i.e. ../login). In your urls.py file, give the url a name (e.g. path('login/', views.my_login, name='my_login')), and use the name to do the redirect (e.g. return redirect('my_login').

Flux
  • 9,805
  • 5
  • 46
  • 92
  • i already tried this before but an error appears at the submit : IntegrityError at /register/ UNIQUE constraint failed: auth_user.username – XetaWays Feb 17 '20 at 23:32
  • @XetaWays See if this helps: https://stackoverflow.com/questions/33093754/unique-constraint-failed-auth-user-username-with-registering-with-email Otherwise, ask a new question. – Flux Feb 17 '20 at 23:39