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

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=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...")
 
(Create a view)
Line 33: Line 33:
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 
from .forms import UserRegistrationForm
 
from .forms import UserRegistrationForm
 +
from django.contrib import messages
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 46: Line 47:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
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.  
+
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=
 
=Check the data=

Revision as of 19:02, 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()
   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.