0% found this document useful (0 votes)
155 views

Django Part-1: Creating The Basic Web App Using Django Web Framework

The document provides an introduction to creating a basic web app using the Django web framework. It discusses Django's MVT (Model-View-Template) architecture and how Models represent and manage data, Views handle requests and responses, and Templates define the structure and layout of files like HTML pages. It also outlines the steps to install and configure Django, create a project and app, and write the code for a simple view to render a template.

Uploaded by

abhinav rai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Django Part-1: Creating The Basic Web App Using Django Web Framework

The document provides an introduction to creating a basic web app using the Django web framework. It discusses Django's MVT (Model-View-Template) architecture and how Models represent and manage data, Views handle requests and responses, and Templates define the structure and layout of files like HTML pages. It also outlines the steps to install and configure Django, create a project and app, and write the code for a simple view to render a template.

Uploaded by

abhinav rai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Django Part-1

Creating the basic web app using Django Web Framework


Introduction

 Django is a high-level Python web framework that enables rapid development


of secure and maintainable websites.
 It is free and open source, has a thriving and active community, great
documentation, and many options for free and paid-for support.
 High-profile sites that use Django include: Disqus, Instagram, Knight
Foundation, MacArthur Foundation, Mozilla, National Geographic, Open
Knowledge Foundation, Pinterest, and Open Stack

Ashok Sengupta
MVC Architecture (Model-View-Controller)
The MVC architecture is a popular
framework for web development
Model represents shape of the data and
business logic. It maintains the data of the
application. Model objects retrieve and
store model state in a database.

View is a user interface. View display data


using model to the user and also enables
them to modify the data.
Controller handles the user request.
Typically, user interact with View, which
in-turn raises appropriate URL request, this
request will be handled by a controller.
The controller renders the appropriate
Ashok Sengupta view with the model data as a response.
MVT Web Framework (Model-View-Template)
• Django web applications typically group
the code that handles each of these
steps into separate files.

• The Model-View-Template (MVT) is


slightly different from MVC. In fact the
main difference between the two
patterns is that Django itself takes care
of the Controller part (Software Code
that controls the interactions between
the Model and View), leaving us with the
template. The template is a HTML file
mixed with Django Template Language
(DTL).

Ashok Sengupta
MVT Architecture
 URLs: While it is possible to process requests from every single URL via a
single function, it is much more maintainable to write a separate view
function to handle each resource. A URL mapper is used to redirect HTTP
requests to the appropriate view based on the request URL. The URL mapper
can also match particular patterns of strings or digits that appear in an URL,
and pass these to a view function as data.
 View: A view is a request handler function, which receives HTTP requests and
returns HTTP responses. Views access the data needed to satisfy requests
via models, and delegate the formatting of the response to templates.
 Models: Models are Python objects that define the structure of
an application's data, and provide mechanisms to manage (add, modify,
delete) and query records in the database.
 Templates: A template is a text file defining the structure or layout of a file
(such as an HTML page), with placeholders used to represent actual content.
A view can dynamically create an HTML page using an HTML template,
populating it with data from a model. A template can be used to define the
structure of any type of file; it doesn't have to be HTML!
Ashok Sengupta
Note: Django
Installing and configuring Django comes with SQLlite
and that can be
 Step 1 : Install virtualenvwrapper – c:\>pip install virtualenvwrapper-win used to create a
 Step 2 : Create a directory projects in your hard drive. I am using the location database driven
“C:\users\myname\” folder to create the projects folder. C:\users\myname\>md website. We are
projects Now move to the directory “projects” by CD command going to learn to
develop websites
 Step 3 : Create virtual environment – C:\users\myname\projects\>mkvirtualenv using MySQL and
projenv need to do a few
 Step 4 : C:\users\myname\projects\>workon projenv things extra
 Step 5 : Install mysql-connector – c:\>pip install mysql-connector
 Step 6 : Install mysqlclient – c:\>pip install mysqlclient (if there is a Microsoft VC++
error then C:\>pip install --only-binary :all: mysqlclient
 Step 7 : Install django – (projenv) C:\users\myname\projects\>pip install django
 Step 8 : Create a django project - (projenv) C:\users\myname\projects\>django-
admin startproject webproj

Ashok Sengupta
The Django project environment
Inside the webproj subfolder we have
four more files __init.py__,
settings.py, urls.py and wsgi.py

django-admin startproject webproj


creates a project folder and another two
components (a folder myproj and a file
manage.py)
Ashok Sengupta
Test the django server
 (projenv) C:\users\<myname>\projects\webproj>python manage.py runserver

Open browser and type


http://127.0.0.1:8000/
Ashok Sengupta
Basic working of django environment
 manage.py - The manage.py script is used to create applications, work with
databases, and start the development web server. It uses a command line
approach (python manage.py <tool to run>). A detailed list of actions can be
obtained by running “<path>\python manage.py help”
 __init__.py - It is an empty file that instructs Python to treat this directory as a
Python package.
 settings.py - It contains all the website settings. This is where we register any
applications we create, the location of our static files, database configuration
details, etc.
 urls.py - It defines the site url-to-view mappings. While this could contain all the
url mapping code, it is more common to delegate some of the mapping to
particular applications, as you'll see later
 wsgi.py - It is used to help your Django application communicate with the web
server.
Ashok Sengupta
Remember that the myname should be
Creating first Django App replaced by your windows user name.
In my case its mahe

 Step 1 : Stop the Server, if running, by pressing CTRL+ C


 Step 2 : From inside the project folder (while still in virtual environment) type the following
command: (projenv) C:\Users\myname\projects\webproj>django-admin startapp firstapp . This
will create a folder named “firstapp” in the “myproj” with the following files:
 admin.py – This file helps you make the app modifiable in the admin interface.
 apps.py – This is a configuration file of the app itself
 models.py – here is where we define the entities of our Web application. The models are translated
automatically by Django into database tables.tests.py –
 views.py – his is the file where we handle the request/response cycle of our Web application.
 tests.py - this file is used to write unit tests for the app.

Ashok Sengupta
You can use any Python IDE like
Writing the code for first app PyCharm, PyScripter, etc. I am using
Visual Studio Code for my coding.

In the command window go to the


newly created project folder and type:
(projenv)
C:\Users\mahe\projects\webproj>code .

This will open the Visual studio code


IDE as shown here with the project
explorer tree visible on the left pane.

Ashok Sengupta
Register your app in settings.py Register your
app here. Do
not forget the ,

Ashok Sengupta
Open views.py and write the
Create a view given code. The function firstapp
is going to render the hello.html
(we are going to create this file
shortly)

In this piece of code, you’ve


defined a view function called
firstapp(). When this function is
called, it will render an HTML file
called hello.html. That file
doesn’t exist yet, but we’ll create
it soon.

from django.shortcuts import render

# Create your views here.


def firstapp(request):
return render(request,'hello.html',{})
Ashok Sengupta
Create the hello.html under templates
folder Create a folder named templates
in the firstapp folder. Create a
html file named hello.html and
type the given Code that display
Hello World

Ashok Sengupta
Also type include in the import
statement.
Hookup your URLs in urls.py
In the project(wbproj) folder
we have a urls.py. Open it and
add the following line to the
urlpatterns list.
path('', include('firstapp.urls')),

The urlpatterns looks for the


firstapp.urls module in the
firstapp folder.

Ashok Sengupta
One more step The firstapp.urls module is not
yet available in the firstapp
folder and we need to create it.
So create the file urls.py inside
the firstapp folder.

Inside this module, we need to


import the path object as well
as our app’s views module.
Then we want to create a list of
URL patterns that correspond to
the various view functions. At
the moment, we have only
created one view function, so
we need only create one URL

Ashok Sengupta
Run the app
Open the browser and type
http://127.0.0.1:8000

Ashok Sengupta
References
 https://realpython.com/get-started-with-django-1/
 https://developer.mozilla.org/en-US/docs/Learn/Server-
side/Django/Introduction
 https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-
beginners-guide-to-django-part-1.html

Ashok Sengupta
Thank you

Ashok Sengupta

You might also like