Django user authentication part 1

Django user authentication part 1

ยท

4 min read

Hey guys, if you're reading this post you should already have knowledge on how to use django models and rendering context variables to the browser . This is a two part post. I'll be covering everything you will need to know to get started with user registration on Django while in the next part I'll be covering how to make your login and logout views, let's get started

DJANGO USER REGISTRATION

On various registration forms we see today the popular fields that we see are usually username, email, password and a password confirmation fields. While working with forms in our Django app it's best practice to create a new module in your app called form.py in your form module this is where all the form related code will be. You should have created your form.py file now, when working with django forms there's an easier way to create a user registration form, this is inbuilt in django module that helps with user registration called UserCreationForm this form helps create a user and add the user to the db automatically after being registered. Let's write some code to see what I'm talking about. This code should be in your form.py file:

from django.contrib.auth.forms import UserCreationForm

from django.contrib.auth.models import from User

This allows you to import the user creation form from django, you'll see why we imported User in a minute. Now let's see how to use these modules we've imported, well create a class called UserRegistrationForm this class will inherit from UserCreationForm. This should go under your imports

class UserRegistrationForm(UserCreationForm):
     class Meta:
           model = User
           fields = ["username", "email", "password1", "password2"]

Let me explain the logic applied above, within our user registration form we wanted to show the model used in this case is the User model which is an inbuilt django model, and the fields of the form we want to render are username, email, password1 and password2, here password1 is your normal password field while password2 is your password confirmation field. We now have a registration form we would want to render it to the browser right, it's not hard all we have to do is go to our views.py file and import UserRegistrationForm from our form.py file. Let's move to our views.py file and add this code :

from .form import UserRegistrationForm

Why were using a . for our import here is to show that were importing from a file within the same folder as our views.py and not to confuse django to incase were making an import from a module with a similar name. In our views.py file we should already have a view to render the form ready. If you don't have no need to worry just creat one. Within your html file you should have your form tag this way :

<form method="POST">
     {% csrf_token %}
      {{ registration_form }}
      <input type"submit" value="submit" />
<\form>

You need to have {%csrf_token%} in any of your form tag that is sending a POST request to your server so as to prevent illegal activities to take place on your site

Your view should have these codes:

# What's happening here is checking if the method on your form is POST

if request.method == 'POST':

    # if the method is POST, the we'll make a form instance and pass in the method type 
     form = UserRegistrationForm (request.POST)

     # if the form is valid it should save the form 
     if form.is_valid():
          form.save()
          form = UserRegistrationForm ()

else:
      form = UserRegistrationForm ()

# This is what should be in your context 
context = {
    "registration_form": form
}

I hope the explanation in the comments is clear enough. If not what is happening basically is that when your form gets submitted it sends a POST request to the server which will then check, if the form is valid it should save the form as a new user, but if the method on your form is a GET method it shouldn't get save to the backend even if the form is valid. Thank you for reading ๐Ÿ˜Š