Writing Your First Blog App In Django-Part15

We will continue to build our blog app where we left in the post writing-your-first-blog-app-in-django-part-14.

In this post we will see how we can take our user and log them into the application. Upto now in our blog app we have two forms,one is signup form and other is login form.In both cases we are handling the post request when we click up on signup or login,but all we are doing is redirecting the user at that point we are saving user there to the database.In the signup view we are not logging the user in,but we are only redirecting it to articles list page.In the login view we are also first validating the data and if the data is correct,we are again redirecting that user to articles list page but not logging the user in.

So in this post we would like to log the user in.So to achieve that goal change the accounts/views.py file as following:

from django.shortcuts import render,redirect
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
from django.contrib.auth import login

def signup_view(request):
    '''
    If the request is post then we will take the data that we've 
    got from the form and we want to somehow validate it and the way we do that
    is by following:
    '''
    if request.method == 'POST':
        '''
        What this instance is doing is essentially kind of valid in that data 
        for us.If this data is okay(like user is already exists or not,password in 
        long enough or not etc..).That's going to return us a instance of form 
        that is either valid or invalid.If it is valid save that form to database.
        '''
        form = UserCreationForm(request.POST)
        if form.is_valid():
            #log the user in
             #first of all we need to get what the user is,who's trying to log in 
            #save method will return the user,that we are saving in user variable
            user = form.save()
            #after getting user we will try to login that user
            login(request, user)
            #redirect the user to articles list page
            return redirect('articles:list')
    #if the request is get then create a blank instance of user creation form.
    else:
        #create a new instance of the form
        form = UserCreationForm()
    #send that form to template
    return render(request, 'accounts/signup.html',{'form':form})

def login_view(request):
    '''
    same as the signup view,in the login we can also have get request or post request
    if the request is post
    '''
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            #log the user in
            #first of all we need to get what the user is,who's trying to log in 
            user = form.get_user()
            #after getting user we will try to login that user
            login(request, user)
            #redirect the user to articles list page
            return redirect('articles:list')
    else:
        #this will be get request.When user clicks on login link or he will go to /accounts/login/ 
        #we will send them a login form to the template and render that form in the browser
        #create a new instance of the form
        form = AuthenticationForm()
    return render(request, 'accounts/login.html',{'form':form})
        

Now to check this first login or sign up and then in the same page go to admin site,you will be directed to admin page,it will not ask you the username and password again


This is all about adding user login in both login and sign up views.

Leave a Comment

Your email address will not be published. Required fields are marked *