Difference between revisions of "Create a sign up page in Django"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Create a view)
(Check the data)
Line 61: Line 61:
 
   messages.success(request, 'Your account has been created! Now you can login!')
 
   messages.success(request, 'Your account has been created! Now you can login!')
 
else:
 
else:
   form = UserRegistrationForm()
+
   form = UserRegistrationForm(request.POST)
 +
  messages.success(request, 'Error in creating your account, the form is not valid!')
 
   return render(request, 'users/register.html', {'form':form})         
 
   return render(request, 'users/register.html', {'form':form})         
 
</syntaxhighlight>
 
</syntaxhighlight>
  
This code will check the form is valid, if it is then the data is cleaned from the form and finally saved. A message is created to tell you the account was created.
+
This code will check the form is valid, if it is then the data is cleaned from the form and finally saved. A message is created to tell you the account was created. If the form isn't valid it will retain the form data and display a message to say the form was invalid.

Revision as of 20:05, 3 May 2019

Creating a form

Django already has a User model which can create, remove and edit users. We will use all the built in functionality. We therefore only need to create a form. So in a new python ('.py') document enter the following:

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

class UserRegistrationForm (UserCreationForm):
    email = forms.EmailField(required=True)
    first_name = forms.TextField(required=True)
    last_name = forms.TextField(required=True)

    class Meta:
        model=User
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
            'password1',
            'password2',
            ]

Now save this in the 'users' app folder, and call it 'forms.py'.

Create a view

Now in the 'views.py' file within your 'users' app folder, add the following import:

from .forms import UserRegistrationForm
from django.contrib import messages

Now we need to declare the view, enter the following:

def register(request):
    if request.method=='POST':
        form = UserRegistrationForm(request.POST)
    else:
        form = UserRegistrationForm()
        return render(request, 'users/register.html', {'form':form})

The if statement is to check if the page has form data or not. If it doesn't the form will be empty, and if it does we grab the data and put it into the form.

Check the data

Now we have a form, and have the form data we need to check the form. So after the line 'form = UserRegistrationForm(request.POST)' add the following code:

if form.is_valid():
   username = form.cleaned_data.get('username')
   email = form.cleaned_data.get('email')
   first_name = form.cleaned_data.get('first_name')
   last_name = form.cleaned_data.get('last_name')
   form.save()
   messages.success(request, 'Your account has been created! Now you can login!')
else:
   form = UserRegistrationForm(request.POST)
   messages.success(request, 'Error in creating your account, the form is not valid!')
   return render(request, 'users/register.html', {'form':form})

This code will check the form is valid, if it is then the data is cleaned from the form and finally saved. A message is created to tell you the account was created. If the form isn't valid it will retain the form data and display a message to say the form was invalid.