Ch-10 Introduction To DJango

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

10.

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.

Django was released on 21, July 2005.

Popularity
Django is widely accepted and used by various well-known sites such as:
● Instagram
● Mozilla
● Disqus
● Pinterest

Features of Django

● Rapid Development

● URL Routing: easy URL routing

● Database Integration: Has default database integration. In the Django

framework SQLite is the default database.

● Database Migration: uses ORM (Object-Relational Mapper) to map objects

to the database.

● Secure : Its user authentication system provides a secure way to

manage user accounts and passwords.

● Scalable: ability to quickly and flexibly switch from small to large scale

application projects.

● Fully loaded: Django includes various helping task modules and libraries

which can be used to handle common Web development tasks. Django


takes care of user authentication, content administration, site maps, RSS
feeds etc.

● Versatile: allows it to build applications for different-different domains.

Django is used to build various types of applications like: content


management systems, social networks sites or scientific computing
platforms etc.
● Open Source

● Vast and Supported Community

Django and Python:


● Django is a Python-based web framework that allows you to quickly create
efficient web applications.
● It is also called batteries included framework because Django provides
built-in features for everything including Django Admin Interface, default
database – SQLlite3, etc.
● Python has a huge library and features such as Web Scraping, Machine
Learning, Image Processing, Scientific Computing, etc.
● One can integrate it all with web applications and do lots and lots of
advanced stuff.

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.

● Using virtual environments is not mandatory, but it’s highly recommended.


● When developing Web sites or Web projects with Django, it’s very common
to have to install external libraries to support the development.
● Using virtual environments, each project you develop will have its isolated
environment.
● So the dependencies won’t clash.
● It also allows you to maintain local machine projects that run on different
Django versions.

1. Installing Python 3.6.2


2. Go to www.python.org click on the Python latest version from download
page.
3. Make sure you check the option Add Python 3.6 to PATH and click on the
Install Now option.
Now search for the Command Prompt program and open it:
python --version

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

Then you have to activate the environment, by typing this command:


myproject\Scripts\activate.bat

Once the environment is activated, you will see this result in the command
prompt:

(myproject) C:\Users\Your Name>

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

Check Django Version


django-admin --version

Starting a New Project


To start a new Django project, run the command below:
django-admin startproject myproject

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:

Our initial project structure is composed of five files:

● manage.py: a shortcut to use the django-admin command-line utility. It’s


used to run management commands related to our project. We will use it
to run the development server, run tests, create migrations and much
more.
● __init__.py: this empty file tells Python that this folder is a Python package.
● settings.py: this file contains all the project’s configuration. We will refer to
this file all the time!
● urls.py: this file is responsible for mapping the routes and paths in our
project. For example, if you want to show something in the URL /about/,
you have to map it here first.
● wsgi.py: this file is a simple gateway interface used for deployment. You
don’t have to worry about it. Just let it be for now.
Django comes with a simple web server installed. It’s very convenient during
development, so we don’t have to install anything else to run the project locally.
We can test it by executing the command:
python manage.py runserver
Now open the following URL in a Web browser: http://127.0.0.1:8000 and you
should see the following page:
Django Apps
In the Django philosophy we have two important concepts:
● app: is a Web application that does something. An app usually is composed
of a set of models (database tables), views, templates, tests.
● project: is a collection of configurations and apps. One project can be
composed of multiple apps, or a single app.

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

from django.http import HttpResponse


def home(request):
return HttpResponse('Hello, World!')

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

from django.conf.urls import url


from django.contrib import admin

from HelloWorld import views

urlpatterns = [
url('', views.home, name='home'),
url('admin/', admin.site.urls),
]

Now start the server by using command:


python manage.py runserver
In a Web browser, open the http://127.0.0.1:8000 URL:

You might also like