Ch-10 Introduction To DJango
Ch-10 Introduction To DJango
Ch-10 Introduction To DJango
Introduction to DJango
10.1 What is DJango
10.2 DJango and Python
10.3 DJango MVT
10.4 How to get and install DJango
References:
https://simpleisbetterthancomplex.com/series/2017/09/04/a-complete-
beginners-guide-to-django-part-1.html#introduction
https://www.w3schools.com/django/django_getstarted.php
What is Django
● Django is a Web framework written in Python.
● A Web framework is a software that supports the development of dynamic
Web sites, applications, and services.
● It is based on the MVT (Model View Template) design pattern.
● Django is very demanding due to its rapid development feature.
● It takes less time to build an application after collecting client requirements.
● This framework uses a famous tagline:”The web framework for
perfectionists with deadlines.”
● By using Django, we can build web applications in very less time.
● Django is designed in such a manner that it handles much of the
configuration automatically, so we can focus on application development
only.
History
Django was designed and developed by Lawrence journal world in 2003 and
publicly released under BSD license in July 2005. Currently, DSF (Django Software
Foundation) maintains its development and release cycle.
Popularity
Django is widely accepted and used by various well-known sites such as:
● Instagram
● Mozilla
● Disqus
● Pinterest
Features of Django
● Rapid Development
to the database.
● Scalable: ability to quickly and flexibly switch from small to large scale
application projects.
● Fully loaded: Django includes various helping task modules and libraries
Django’s batteries are located in the “contrib” packages, which are as follows:
1. Admin
2. Auth
3. Contenttypes
4. Flatpages
5. Gis
6. humanize
7. Messages: to manage session and cookies
8. Postgres
9. Redirects
10.Sessions: to manage anonymous sessions
11.Sites
12.Sitemaps
13.syndication
Django MVT
● The MVT (Model View Template) is a software design pattern.
● It is a collection of three important components: Model, View and
Template.
● The Model helps to handle databases. It is a data access layer which
handles the data.It is the logical data structure behind the entire
application and is represented by a database (generally relational
databases such as MySql, Postgres).
● The Template is a presentation layer which handles the User Interface part
completely.
● The View is used to execute the business logic and interact with a model to
carry data and render a template. what you see in your browser when you
render a website. It is represented by HTML/CSS/Javascript files.
10.4 How to get and install DJango
The basic setup consists of installing Python, Virtualenv, and Django.
PIP
To install Django, you must use a package manager like PIP, which is included in
Python from version 3.4.
To check if your system has PIP installed, run this command in the command
prompt:
pip --version
With venv, you can create a virtual environment by typing this in the command
prompt, remember to navigate to where you want to create your project:
py -m venv myproject
This will set up a virtual environment, and create a folder named "myproject"
with subfolders and files, like this:
myproject
Include
Lib
Scripts
pyvenv.cfg
Once the environment is activated, you will see this result in the command
prompt:
Note: You must activate the virtual environment every time you open the command
prompt to work on your project.
Install Django
Finally, we can install Django.
Remember to install Django while you are in the virtual environment!
Django is installed using pip, with this command:
pip install Django
After we run the command above, it will generate the base folder structure for a
Django project.
Right now, our myproject directory looks like this:
It’s important to note that you can’t run a Django app without a project.
To create our first app, go to the directory where the manage.py file is and
executes the following command:
django-admin startapp HelloWorld
This will give us the following directory structure:
myproject/
|-- myproject/
| |-- HelloWorld/ <-- our new django app!
| | |-- migrations/
| | | +-- __init__.py
| | |-- __init__.py
| | |-- admin.py
| | |-- apps.py
| | |-- models.py
| | |-- tests.py
| | +-- views.py
| |-- myproject/
| | |-- __init__.py
| | |-- settings.py
| | |-- urls.py
| | |-- wsgi.py
| +-- manage.py
+-- venv/
● migrations/: here Django stores some files to keep track of the changes you
create in the models.py file, so to keep the database and the models.py
synchronized.
● admin.py: this is a configuration file for a built-in Django app called Django
Admin.
● 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: this file is used to write unit tests for the app.
● views.py: this is the file where we handle the request/response cycle of our
Web application.
Now that we created our first app, let’s configure our project to use it.
To do that, open the settings.py and try to find the INSTALLED_APPS variable:
Settings.py (Before adding out new app HelloWorld)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Settings.py (After adding out new app HelloWorld)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'Django.contrib.staticfiles',
‘HelloWorld’,
]
Let’s write our first view.
Open the views.py file inside the HelloWorld app, and add the following code:
Views.py
Views are Python functions that receive an HttpRequest object and return an
HttpResponse object. Receive a request as a parameter and return a response as a
result.
Now we have to tell Django when to serve this view. It’s done inside the urls.py
file:
Urls.py
urlpatterns = [
url('', views.home, name='home'),
url('admin/', admin.site.urls),
]