Django Part-1: Creating The Basic Web App Using Django Web Framework
Django Part-1: Creating The Basic Web App Using Django Web Framework
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.
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
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.
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)
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')),
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.
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