In this tutorial I will cover how to create Django user sign up/registration, login, logout. Usually I have done it from scratch. You can checkoutout.

For the examples I will use an empty Django project named college. Inside the college folder I created an app student core. So every time you see college and/or core, change to the suitable project name and app name.

Basic Sign Up

The most simple way to implement a user sign up is by using the normal as it is. This strategy is suitable in case you are using the default Django user, using username to authenticate and is interested only in setting the username and password upon sign up.

This urls.py is route college folder
urls.py

urlpatterns = [
    path('', include('pages.urls')),
    path('student/', include('student.urls')),
    path('admin/', admin.site.urls),
]

Student urls.py

from .views import loginStudentView, registerStudentView, logoutStudent

	urlpatterns = [
	    path('login', loginStudentView, name='login'),
	    path('register', registerStudentView, name='register'),
	    path('logout', logoutStudent, name='logout'),
	    ]

Views.py(Student)

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages

def loginStudentView(request):
        if request.method == 'POST':
            username = request.POST['username']
            password = request.POST['password']
            user = auth.authenticate(username=username, password=password)

            if user is not None:
                    auth.login(request, user)
                    return redirect('/')
            else:
                    messages.info(request,'invalid credentials')
                    return redirect('login')
        else:
            return render(request, "login.html")


def registerStudentView(request):
        
        if request.method == 'POST':
            first_name = request.POST['first_name']
            last_name = request.POST['last_name']
            username = request.POST['username']
            password1 = request.POST['password1']
            password2 = request.POST['password2']
            email = request.POST['email']
            
            if password1 == password2:
                    if User.objects.filter(username=username).exists():
                        #     print('username is already register')
                            messages.info(
                                request, 'username is already register')
                            return redirect('/student/register')
                          
                    elif User.objects.filter(email=email).exists():
                            messages.info(
                                request, 'email is already register')
                            return redirect('/student/register')
                    else:
                        user = User.objects.create_user(
                        username=username, password=password1, first_name=first_name, last_name=last_name, email=email)
                        user.save()
                        messages.info(
                            request, 'user created')
                        return redirect('login')
                        

            else:
                 print('password not matching')
            return redirect('/student/register')
        else:
                return render(request, "register.html")
        # return render(request, "register.html")


def logoutStudent(request):
    auth.logout(request)
    return redirect('/')

Html part index.html

{% load static %}
<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>Corp Group Corporate Category Bootstrap Responsive Web Template | Home :: W3layouts</title>
    <!-- Meta tag Keywords -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8" />
    <meta name="keywords" content="Corp Group Responsive web template, Bootstrap Web Templates, Flat Web Templates, Android Compatible web template, Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
    <script>
        addEventListener("load", function() {
            setTimeout(hideURLbar, 0);
        }, false);

        function hideURLbar() {
            window.scrollTo(0, 1);
        }

    </script>
    <!-- //Meta tag Keywords -->

    <!-- Custom-Files -->
    <link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
    <!-- Bootstrap-Core-CSS -->
    <link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css" media="all" />
    <!-- Style-CSS -->
    <!-- font-awesome-icons -->
    <link href="{% static 'css/font-awesome.css' %}" rel="stylesheet">
    <!-- //font-awesome-icons -->
    <!-- /Fonts -->
   <link href="//fonts.googleapis.com/css?family=Raleway:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,800" rel="stylesheet">
    <!-- //Fonts -->

</head>

<body>

    <!-- mian-content -->
    <div class="banner-content" id="home">
        <!-- header -->
        <header class="header">
            <div class="container-fluid px-lg-5">
                <!-- nav -->
                <nav class="py-4">
                    <div id="logo">
                        <h1> <a href="">Corp Group</a></h1>
                        <h1> <a href="">From: {{page.name}}</a></h1>
                    </div>
                   
                    <label for="drop" class="toggle">Menu</label>
                    <input type="checkbox" id="drop" />
                    <ul class="menu mt-2">
                        <li class="active"><a href="">Home</a></li>
                        <li><a href="about">About</a></li>
                        {% if user.is_authenticated %}
                        <li>Hello, {{user.first_name}}</li>
                        <li><a href="student/logout">Logout</a></li>
                        {% else %}
                        <li><a href="student/register">Student Login</a></li>
                        <li><a href="student/login">Student Register</a></li>
                        {% endif %}
                        <li><a href="contact">Contact</a></li>
                    </ul>
                </nav>
                <!-- //nav -->
            </div>
        </header>
        <!-- //header -->

        <!--/banner-->
        <div class="banner-info-w3pvt">
            <h3>To Grow Your Business</h3>
            <p class="my-4">Lorem Ipsum is simply dummy text of the printing and typesetting industry has been the industry's standard dummy text ever since the 1500s, when an unknown printer.</p>
            <div class="ban-buttons">
                <a href="single.html" class="btn">Read More</a>
                
            </div>
        </div>
        <!--// banner-inner -->
    </div>
    <!-- //header -->
	 <!-- banner-bottom-wthree -->
    <section class="banner-bottom-wthree py-5" id="about">
        <div class="container py-md-3">
            <div class="row banner-grids">
                <div class="col-lg-6 content-left-bottom text-left pr-lg-5">
				<h3 class="tittle text-capitalize text-left my-md-4"> Welcome To Our Website</h3>
                  
                    <p class="mt-2 text-left">Integer pulvinar leo id viverra feugiat.<strong class="text-capitalize"> Pellentesque libero justo, semper at tempus vel, ultrices in sed ligula. Nulla uter sollicitudin velit.</strong> Sed porttitor orci vel fermentum elit maximus. Curabitur ut turpis massa in condimentum libero. Pellentesque maximus Pellentesque libero justo Nulla uter sollicitudin velit. Sed porttitor orci vel ferm semper at vel, ultrices in ligula semper at vel.Curabitur ut turpis massa in condimentum libero.</p>

                </div>
                <div class="col-lg-6 content-right-bottom text-left">
                    <img src="{% static 'images/g1.jpg' %}" alt="news image" class="img-fluid">
                </div>
            </div>
			<div class="row service-grid-grids text-center pt-lg-5 mt-3">
			<div class="col-lg-4 col-md-6 service-grid service-grid1">
				<div class="service-icon">
					<span class="fa fa-pencil-square-o" aria-hidden="true"></span>
				</div>
				<h4 class="mt-3">Best Solutions</h4>
				<p class="mt-3">Perspiciatis unde omnis iste natus doloret ipsum volupte ut accusal ntium dolor remque laudantium, totam dolor.</p>
			</div>
			<div class="col-lg-4 col-md-6 service-grid service-grid2 mt-md-0 mt-5">
				<div class="service-icon">
					<span class="fa fa-bullseye " aria-hidden="true"></span>
				</div>
				<h4 class="mt-3">Creative Work</h4>
				<p class="mt-3">Perspiciatis unde omnis iste natus doloret ipsum volupte ut accusal ntium dolor remque laudantium, totam dolor.</p>
			</div>
			
			<div class="col-lg-4 col-md-6 service-grid service-grid3 mt-lg-0 mt-5">
				<div class="service-icon">
					<span class="fa fa-database" aria-hidden="true"></span>
				</div>
				<h4 class="mt-3">Great Designs</h4>
				<p class="mt-3">Perspiciatis unde omnis iste natus doloret ipsum volupte ut accusal ntium dolor remque laudantium, totam dolor.</p>
			</div>
		</div>
        </div>
    </section>
    <!-- //banner-bottom-wthree -->

	<!-- facts -->
<section class="facts" id="facts">
	<div class="container">
		<div class="row">
		<div class="col-lg-4 p-lg-0 text-center">
				<img src="{% static 'images/2.png' %}" class="img-fluid" alt="">
			</div>
			<div class="col-lg-8 pt-5">
				<div class="row inner-heading">
					<h3 class="tittle text-capitalize text-left my-md-4"> Why Choose Us</h3>
					<p class="mt-md-0 mt-2">Donec cursus congue risus, quis varius velit accumsan aliquam. Morbi semper nunc. Perspiciatis unde omnis iste
					natus doloret ipsum volupte ut accusal ntium dolor remque laudantium, totam dolor</p>
				</div>
				<div class="row mt-5 fact-grid-main">
					<div class="col-sm-4 stats-grid">
						<span class="fa fa-tasks"></span>
						<span>250</span>
						<h4>Completed Work</h4>
					</div>
					<div class="col-sm-4 stats-grid">
						<span class="fa fa-copy"></span>
						<span>50+</span>
						<h4>Projects</h4>
					</div>
					<div class="col-sm-4 stats-grid">
						<span class="fa fa-smile-o"></span>
						<span>2000+</span>
						<h4>Satisfied clients</h4>
					</div>
				</div>
			</div>
			
		</div>
	</div>
</section>
<!-- //facts -->
 <!-- Gallery -->
    <section class="gallery py-5" id="gallery">
        <div class="container py-md-5">
            <div class="header text-center">
                <h3 class="tittle mb-lg-5 mb-3">Our Gallery</h3>
            </div>
            <div class="row news-grids text-center gallery-wrap">
                <div class="col-md-4 gal-img">
                    <a href="#gal1"><img src="{% static 'images/g1.jpg' %}" alt="news image" class="img-fluid"></a>
                    <p>Gallery 1</p>
                </div>
                <div class="col-md-4 gal-img">
                    <a href="#gal2"><img src="{% static 'images/g2.jpg' %}" alt="news image" class="img-fluid"></a>
                    <p>Gallery 2</p>
                </div>
                <div class="col-md-4 gal-img">
                    <a href="#gal3"><img src="{% static 'images/g3.jpg' %}" alt="news image" class="img-fluid"></a>
                    <p>Gallery 3</p>
                </div>
                <div class="col-md-4 gal-img">
                    <a href="#gal4"><img src="{% static 'images/g4.jpg' %}" alt="news image" class="img-fluid"></a>
                    <p>Gallery 4</p>
                </div>
				<div class="col-md-4 gal-img">
                    <a href="#gal1"><img src="{% static 'images/g5.jpg' %}" alt="news image" class="img-fluid"></a>
                    <p>Gallery 5</p>
                </div>
                <div class="col-md-4 gal-img">
                    <a href="#gal2"><img src="{% static 'images/g6.jpg' %}" alt="news image" class="img-fluid"></a>
                    <p>Gallery 6</p>
                </div>
                
            </div>

            <!-- popup-->
            <div id="gal1" class="pop-overlay animate">
                <div class="popup">
                    <img src="{% static 'images/g1.jpg' %}" alt="Popup Image" class="img-fluid" />

                    <a class="close" href="#gallery">×</a>
                </div>
            </div>
            <!-- //popup -->

            <!-- popup-->
            <div id="gal2" class="pop-overlay animate">
                <div class="popup">
                    <img src="{% static 'images/g2.jpg' %}" alt="Popup Image" class="img-fluid" />

                    <a class="close" href="#gallery">×</a>
                </div>
            </div>
            <!-- //popup -->
            <!-- popup-->
            <div id="gal3" class="pop-overlay animate">
                <div class="popup">
                    <img src="{% static 'images/g3.jpg' %}" alt="Popup Image" class="img-fluid" />

                    <a class="close" href="#gallery">×</a>
                </div>
            </div>
            <!-- //popup3 -->
            <!-- popup-->
            <div id="gal4" class="pop-overlay animate">
                <div class="popup">
                    <img src="{% static 'images/g4.jpg' %}" alt="Popup Image" class="img-fluid" />

                    <a class="close" href="#gallery">×</a>
                </div>
            </div>
            <!-- //popup -->
			 <!-- popup-->
            <div id="gal4" class="pop-overlay animate">
                <div class="popup">
                    <img src="{% static 'images/g5.jpg' %}" alt="Popup Image" class="img-fluid" />

                    <a class="close" href="#gallery">×</a>
                </div>
            </div>
            <!-- //popup -->
			 <!-- popup-->
            <div id="gal4" class="pop-overlay animate">
                <div class="popup">
                    <img src="{% static 'images/g6.jpg' %}" alt="Popup Image" class="img-fluid" />

                    <a class="close" href="#gallery">×</a>
                </div>
            </div>
            <!-- //popup -->


        </div>
    </section>
    <!--// gallery -->

      <!--/testimonials-->
    <section class="testimonials" id="testimonials">
        <div class="layer tes">
            <div class="container py-5">
			<h3 class="tittle text-center">Testimonials</h3>
                <div class="test-info text-center">
                    <h3 class="my-md-2 my-3">Shane Watson</h3>

                    <ul class="list-unstyled w3ls-icons clients mb-md-4">
                        <li>
                            <a href="#">
                                <span class="fa fa-star"></span>
                            </a>
                        </li>
                        <li>
                            <a href="#">
                                <span class="fa fa-star"></span>
                            </a>
                        </li>
                        <li>
                            <a href="#">
                                <span class="fa fa-star"></span>
                            </a>
                        </li>
                        <li>
                            <a href="#">
                                <span class="fa fa-star-half-o"></span>
                            </a>
                        </li>
                    </ul>
                    <p><span class="fa fa-quote-left"></span> Lorem Ipsum has been the industry's standard since the 1500s. Praesent ullamcorper dui turpis.Nulla pellentesque mi non laoreet eleifend. Integer porttitor mollisar lorem, at molestie arcu pulvinar ut. <span class="fa fa-quote-right"></span></p>

                </div>
            </div>
        </div>
    </section>
    <!--//testimonials-->

     <!-- /hand-crafted -->
    <section class="hand-crafted py-5">
        <div class="container py-md-5">
            <div class="row banner-grids">
                <div class="col-lg-6 banner-image">
                    <div class="effect-w3">
                        <img src="{% static 'images/g3.jpg' %}" alt="" class="img-fluid image1">
                    </div>
                </div>
                <div class="col-lg-6 last-img pl-lg-5 p-3">
                    <h3 class="tittle mb-lg-5 mb-3">Features</h3>
                    <p class="mb-4"> Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla mollis dapibus nunc, ut rhoncus turpis sodales quis. Integer sit amet mattis quam, sit amet ultricies velit. Praesent ullamcorper dui turpis.</p>
                    <ul class="tic-info list-unstyled">
                        <li>

                            <span class="fa fa-check mr-2"></span> Integer sit amet mattis quam

                        </li>
                        <li>

                            <span class="fa fa-check mr-2"></span> Praesent ullamcorper dui turpis

                        </li>
                        <li>

                            <span class="fa fa-check mr-2"></span> Integer sit amet mattis quam

                        </li>
                        <li>
                            <span class="fa fa-check mr-2"></span> Integer sit amet mattis quam

                        </li>
                    </ul>

                </div>
            </div>
        </div>
    </section>
    <!-- //hand-crafted -->

    <!--footer -->
    <footer>
        <section class="footer footer_1its py-5">
            <div class="container py-md-4">
                <div class="row footer-top mb-md-5 mb-4">
                    <div class="col-lg-4 col-md-6 footer-grid_section_1its">
                        <div class="footer-title-w3ls">
                            <h3>Address</h3>
                        </div>
                        <div class="footer-text">
                            <p>Address : 1234 Lorem Street,Perth, Australia</p>
                            <p>Phone : +12 534894364</p>
                            <p>Email : <a href="mailto:[email protected]">[email protected]</a></p>
                            <p>Fax : +12 534894364</p>
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-6 mt-md-0 mt-4 footer-grid_section_1its">
                        <div class="footer-title-w3ls">
                            <h3>Quick Links</h3>
                        </div>
                        <div class="row">
                            <ul class="col-6 links">
                               <li><a href="about">About</a></li>
                        <li><a href="student/register">Student Login</a></li>
                        <li><a href="student/login">Student Register</a></li>
                        <li><a href="contact">Contact</a></li>
                            </ul>
                            <ul class="col-6 links">
                                <li><a href="#">Privacy Policy </a></li>
                                <li><a href="#">General Terms </a></li>
                                <li><a href="#faq">Faq's </a></li>
                                <li><a href="#">Knowledge </a></li>
                                <li><a href="#">Forum </a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-12 mt-lg-0 mt-4 col-sm-12 footer-grid_section_1its">
                        <div class="footer-title-w3ls">
                            <h3>Newsletter</h3>
                        </div>
                        <div class="footer-text">
                            <p>By subscribing to our mailing list you will always get latest news and updates from us.</p>
                            <form action="#" method="post">
                                <input type="email" name="Email" placeholder="Enter your email..." required="">
                                <button class="btn1 btn"><span class="fa fa-paper-plane-o" aria-hidden="true"></span></button>
                                <div class="clearfix"> </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="footer-grid_section text-center">
                   
                    <ul class="social_section_1info">
                        <li class="mb-2 facebook"><a href="#"><span class="fa fa-facebook mr-1"></span>facebook</a></li>
                        <li class="mb-2 twitter"><a href="#"><span class="fa fa-twitter mr-1"></span>twitter</a></li>
                        <li class="google"><a href="#"><span class="fa fa-google-plus mr-1"></span>google</a></li>
                        <li class="linkedin"><a href="#"><span class="fa fa-linkedin mr-1"></span>linkedin</a></li>
                    </ul>
                </div>
                <div class="move-top-w3layouts text-center mt-3">
                    <a href="#home" class="move-top"> <span class="fa fa-angle-up  mb-3" aria-hidden="true"></span></a>
                </div>

            </div>
        </section>
    </footer>
    <!-- //footer -->

    <!-- copyright -->
    <div class="cpy-right text-center py-3">
        <p class="">© 2019 Corp Group. All rights reserved | Design by
            <a href="http://w3layouts.com"> W3layouts.</a>
        </p>
    </div>
    <!-- //copyright -->

</body>

</html>

Register.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Student Register</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Student Register</h2>
  <div>
    {% for message in messages %}
    <div class="alert alert-danger" role="alert">
     {{message}}
    </div>
    {% endfor %} 
  </div>
  <form action="{% url 'register' %}" method="post">
    {% csrf_token %}
    <div class="form-group">
      <label for="first name">First Name:</label>
      <input type="text" class="form-control" id="first_name" placeholder="Enter First Name" name="first_name">
    </div>
    <div class="form-group">
      <label for="last name">Last Name:</label>
      <input type="text" class="form-control"  placeholder="Enter Last Name" name="last_name">
    </div>
    <div class="form-group">
      <label for="Password">Username:</label>
      <input type="text" class="form-control" placeholder="Enter User Name" name="username">
    </div>
     <div class="form-group">
      <label for="Password">Password1:</label>
      <input type="password" class="form-control"  placeholder="Enter Password 1" name="password1">
    </div>
    <div class="form-group">
      <label for="password2">Password 2:</label>
      <input type="password" class="form-control"  placeholder="Enter Password 2" name="password2">
    </div>
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control"  placeholder="Enter Email" name="email">
    </div>
    
    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>

</body>
</html>

login.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Student Login</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Student Login</h2>
  <div>
    {% for message in messages %}
    <div class="alert alert-danger" role="alert">
     {{message}}
    </div>
    {% endfor %} 
  </div>
  <form action="{% url 'login' %}" method="post">
    {% csrf_token %}
   
    <div class="form-group">
      <label for="Password">Username:</label>
      <input type="text" class="form-control" placeholder="Enter User Name" name="username">
    </div>
     <div class="form-group">
      <label for="Password">Password:</label>
      <input type="password" class="form-control"  placeholder="Enter Password" name="password">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
  </form>
</div>

</body>
</html>
python manage.py runserver
Leave a Comment