/  Technology   /  How to Create Web application using Django
How-to-Create-Web-application-using-Django1-i2tutorials

How to Create Web application using Django

Django

Django is a free and open-source web application framework written in python. It provides a lot of features to the developers, so development can be rapid.

It also provides a set of tools and functionalities that solves many common problems associated with web development, such as security features, database access, template processing, URL routing, and much more.


Let’s see some feature of Django framework :

  • Fast, simple and rapid development
  • Secure of clickjacking and SQL injection
  • Scalable
  • Database integration


Django is the best Python library for web development. You can create a website within a few hours. It’s fully loaded with all the required tools to create websites and it is very secure. Internet giants such as YouTube, Instagram, Google, etc. rely on Django.


Installation

Before installing Django, python is the prerequisite, so you need to install python first. You can download it from the official python page and walk through the installation process.

pip install virtualenv


whenever you are starting a new web development project it is recommended to create a virtual environment to manage dependencies. We will create a folder and then we will set up our virtualenv in that folder. It can be done by using the following commands :

mkdir myproject
cd myproject
virtualenv venv

To activate the corresponding environment, you can use:

venv\Scripts\activate

Now that you have created a virtual environment it’s time to install Django. You can do this by using:

pip install Django


Create a Django Project

Create a Django Project Once after you completed installing Django we are ready to create our first Django project. To create Django project you have to use the command:

django-admin startproject project1


So now project1 has been created inside my project directory. A new directory project1 will also be created. Your directory structure should look something like this

 project1/
 manage.py
 project1/
 _init_.py
 setting.py
 urls.py
 wsgi.py


init_.py :-It is an empty directory which is considered to be a package.

setting.py :- This consist of all the setting that are necessary for the current project.

urls.py :- It consists of all the URLs declaration for the current project.

wsgi.py :- wsgi stands for web server gateway interface. It is calling convection for webservers to forward requests to web application or other servers.


Now to check if we can able to connect from our project to web server, run the following command :

python manage.py runserver


It will return an IP address. When you go to that IP address in your browser, you should see the following :


Now we are all set to create our website.


Create a Django Application

To create the app, run the following command:

python manage.py startapp app1


This will create another directory called app1. When we created a new app Django created necessary files. Django writes all the necessary codes and you have to write only your website specific code and modify the files that are present. The following files will be created.


_init_.py :- It is an empty directory which is considered to be a package

admin.py :- Contains setting for the Django admin pages

models.py :- Contains a series of classes that Django’s ORM converts to database tables

apps.py :- Contains setting for the application configuration

tests.py :- Contains test classes

views.py :- Contains functions and classes that handle what data is displayed in the HTML templates.


The first file that we are going to open is views.py

from django.shortcuts import render
from django.http import HttpResponse

def home(request):
return HttpResponse(‘<h1> Welcome to Django Project </h1>’)


Now we have created views for our project. To connect views.py to urls.py in the project1 directory, you have to create another urls.py in the app1 directory and connect this urls.py to the urls.py which is present in project1 directory.


Now open urls.py in app1 directory use the following code to connect views.py to it.

from django.urls import path
from . import views
urlpatterns = [ 
path(‘   ’, views.home, name=‘home-page’),
]


Now we have to map this urls.py to urls.py which is present in project1, and make sure to do the following changes in urls.py (project1)

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘   ’, include(‘app1.urls’))
]


Now you are ready to run our first page. So go to terminal and use.

python manage.py runserver


Click on the IP address and you will get your first page.


Now to write HTML code within this project, you need to write create a new directory within your app1 directory called templates. Inside this templates directory create a new directory with the same name as your app(app1).


  

It seems confusing but it is the Django convention, which helps you write project-specific code.


You can write your HTML code within this directory. For that create a HTML file as tutorial.html

<!DOCTYPE html>
<html lang=‘en’>
<head>
<meta charset = ‘UTF-8’>
<title> Info</title>
</head>
<body>
<h1><center><i><b>StudentInformation</b></i></h1>
<form>
<table align="center"><br>
<tr>
<td>First Name:</td>
<td><input type ="text" placeholder="fname" first name=""></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" placeholder="lname" last name=""></td>
</tr>
<tr>
<td>Course:</td>
<td><input type="text" placeholder="course" course=""></td>
</tr>
<tr>
<td>Branch:</td>
<td><select>
<option>other</option>
<option>CSE</option>
<option>EXTC</option>
<option>MECHANICAL</option>
<option>ELECTRICAL</option>
<option>IT</option>
</select>
</td>
</tr>
<td>Year:</td>
<td><select>
<option>1st</option>
<option>2nd</option>
<option>3rd</option>
<option>4th</option>
</select>
</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" placeholder="addr" address=""></td>
</tr>
<tr>
<td>Admission number:</td>
<td><input type="" placeholder="admn" admission=""></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" placeholder="p no" phone=""></td>
</tr>
<tr>
<td>Email No:</td>
<td><input type="text" placeholder="eid" email=""></td>
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" placeholder="uname" user=""></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" placeholder="pword" password=""></td>
</tr>
<tr>
<td>Reenter Password:</td>
<td><input type="password" placeholder="rword" repassword=""></td>
</tr>
<tr>
<td>Upload Resume:</td>
<td>
<input type="text" placeholder=""><input type="file" value="browse"></td>
</tr>
<tr>
<td><align="center"><a href="login1.html">Student Login</a></td><br>
<td><a href="login1.html">Admin login</a> <button type="submit">Submit</button></td>
</tr>
</form>
</table>
</body>


After this open the apps.py copy the name of the class from there and then go to setting.py present in project1, where you will look for installed apps. Whenever you create a new app you will have to add it here then add the following in installed apps

INSTALLED "APPS" = [
‘app1.app.app1Config’,
‘django.contrib.admin’,
 ………
 ]
	


From there go to views.py and use render function, use the following code

from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, ‘app1/tutorial.html’)


And finally run

  python manage.py runserver


and you will see the output of the HTML file.

If you want to do modification you can modify your HTML file to create your website.

Leave a comment