Python - A Simple Login Form With Django Build In Login Function

In this tutorial we will create a Simple Login Form With Django Built In Login Function. Django is a free and open source web application framework, written in Python. A web framework is a set of components that helps you to develop websites faster and easier. Django makes developers life convenient and productive framework to all. So let's now do the coding. Getting started First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/. After Python IDLE's is installed, open the command prompt then type "pip install Django", and hit enter. tut1 Wait for the django to be downloaded and installed at the same time. Then After that type "python -m django version" and hit enter to check if django is installed and what version of django is. tut2Creating the App After setting django we will now create the web app for the web server. First create a new folder named "Python - A Simple Login Form With Django Built In Login Function", then cd to a newly created folder, then type "django-admin startproject webserver" and hit enter. A new folder will be created on the directory named 'webserver'. tut3Running The Server After creating a project, cd to the newly created directory, then type "manage.py runserver" and hit enter to start the server running. The "manage.py" is a command of django-admin that utilize the administrative tasks of python web framework. Here is the image of python web server: tut4 Note: Type '127.0.0.1:8000' in the url browser to view the server. When there is code changes in the server just (ctrl + C) to command prompt to stop the server from running, then start again to avoid errors. Creating The Website This time will now create the web app to display the web models. First locate the directory of the app via command prompt cd, then type "manage.py startapp login" and hit enter. A new directory will be create inside the app named "login". tut5Setting up The URL This time will now create a url address to connect the app from the server. First Go to webserver directory, then open urls via Python IDLE's or any text editor. Then import "include" module beside the url module. After that copy/paste the code below inside the urlpatterns.
  1. url(r'^login/', include('login.urls')),
It will be look like this:
  1. from django.conf.urls import include, url
  2. from django.contrib import admin
  3.  
  4. urlpatterns = [
  5. url(r'^admin/', admin.site.urls),
  6. url(r'^login/', include('login.urls')),
  7. ]
Creating The Path For The Pages Now that we set the connect we will now create a path for the web pages. All you have to do first is to go to logindirectory, then copy/paste the code below and save it inside login directory namely 'urls.py' The file name must be urls.py or else there will be an error in the code.
  1. from django.conf.urls import url
  2. from . import views
  3. from django.contrib.auth.views import login
  4.  
  5.  
  6. app_name = 'login'
  7.  
  8. urlpatterns = [
  9. url(r'^$', login, {'template_name': 'login/index.html'}),
  10. url(r'^home/$', views.home, name="home"),
  11. url(r'^$', views.logout, name="logout")
  12. ]
Creating The Views The views contains the interface of the website. This is where you assign the html code for rendering it to django framework. To do that first open the views.py, the copy/paste the code below.
  1. from django.shortcuts import render
  2. from django.contrib.auth import logout
  3.  
  4. # Create your views here.
  5.  
  6.  
  7. def home(request):
  8. return render(request, 'login/home.html')
  9.  
  10. def logout(request):
  11. logout(request)
Creating The Mark up Language Now we will create the html interface for the django framework. First go to login directory, then create a directory called "templates" and create a sub directory on it called login. base.html
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <title>Django Login</title>
  5. <meta charset = "utf-8" name = "viewport" content = "width=device-width, initial-scale=1" />
  6. {% load static %}
  7. <link rel = "stylesheet" type = "text/css" href = "{% static 'login/css/bootstrap.css' %}" />
  8. </head>
  9. <body>
  10. <nav class = "navbar navbar-default">
  11. <div class = "container-fluid">
  12. <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
  13. </div>
  14. </nav>
  15. <body>
  16. {% block body %}
  17. {% endblock %}
  18. </body>
  19. </body>
  20. </html>
Save it as "base.html" inside the login directory "sub directory of templates". index.html
  1. {% extends 'login/base.html' %}
  2. {% block body %}
  3. <div class = "col-md-3"></div>
  4. <div class = "col-md-6 well">
  5. <h3 class = "text-primary" style = "text-align:center;">Python - A Simple Login Form With Django Built In Login Function</h3>
  6. <hr style = "border-top:1px dotted #000;"/>
  7. <br />
  8. <form method = "POST">
  9. {% csrf_token %}
  10. <center>{{ form.as_p }}</center>
  11. <center><button class = "btn btn-primary" type = "submit" name = "submit">Login</button></center>
  12. </form>
  13. </div>
  14. {% endblock %}
Save it as "index.html" inside the login directory "sub directory of templates". home.html
  1. {% extends 'login/base.html'%}
  2. {% block body %}
  3. <div class = "col-md-3"></div>
  4. <div class = "col-md-6 well">
  5. <h3 class = "text-primary" style = "text-align:center;">Python - A Simple Login Form With Django Built In Login Function</h3>
  6. <hr style = "border-top:1px dotted #000;"/>
  7. <br />
  8. <h1 style = "text-align:center;">Welcome</h1>
  9. <a href = "{% url 'login:logout'%}">Logout</a>
  10. </div>
  11. {% endblock %}
Save it as "home.html" inside the login directory "sub directory of templates". Registering The App To The Server Now that we created the interface we will now then register the app to the server. To do that go to the sample directory, then open "settings.py" via Python IDLE's or any text editor. Then copy/paste this script inside the INSTALLED_APP variables 'login'. It will be like this:
  1. INSTALLED_APPS = [
  2. 'login',
  3. 'django.contrib.admin',
  4. 'django.contrib.auth',
  5. 'django.contrib.contenttypes',
  6. 'django.contrib.sessions',
  7. 'django.contrib.messages',
  8. 'django.contrib.staticfiles',
  9. ]
Registering The Login URL This script will register the login url when the user successfully login. While the settings.py is still open copy the code below and just paste it below the STATIC_URL.
  1. LOGIN_REDIRECT_URL = '/login/home'
Migrating The App To The Server Now that we done in setting up all the necessary needed, we will now then migrate the app to the server. To do that open the command prompt then cd to the "sample" directory, then type "manage.py migrate" and hit enter. tut6 Now try to run the server again, and see if all things are done. There you have it we successfully created a Simple Login Form With Django Built In Login Function. I hope that this simple tutorial help you understand to handle django web framework. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Tags

Add new comment