Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Django Python

Uploaded by

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

Django Python

Uploaded by

amna shahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Django

Django is a high-level Python web framework that encourages rapid development and clean,
pragmatic design. Django makes it easier to build better web apps quickly and with less code.
Note − Django is a registered trademark of the Django Software Foundation, and is licensed
under BSD License.
The BSD License, also known as the "Berkeley Software Distribution License," is a permissive
open-source software license used for various software projects, including the Django web
framework. This license allows users to use, modify, and distribute the software while providing
some basic requirements, such as retaining the original copyright notice and disclaimers. It is one
of the many open-source licenses used in the software development community.

History of Django

 2003 − Started by Adrian Holovaty and Simon Willison as an internal project at the
Lawrence Journal-World newspaper.
 2005 − Released July 2005 and named it Django, after the jazz guitarist Django
Reinhardt.
 2005 − Mature enough to handle several high-traffic sites.
 Current − Django is now an open source project with contributors across the world.

Django – Design Philosophies

Django comes with the following design philosophies −


 Loosely Coupled − Django aims to make each element of its stack independent of the
others.
 Less Coding − Less code so in turn a quick development.
 Don't Repeat Yourself (DRY) − Everything should be developed only in exactly one
place instead of repeating it again and again.
 Fast Development − Django's philosophy is to do all it can to facilitate hyper-fast
development.
 Clean Design − Django strictly maintains a clean design throughout its own code and
makes it easy to follow best web-development practices.

Advantages of Django

Here are few advantages of using Django which can be listed out here −
 Object-Relational Mapping (ORM) Support − Django provides a bridge between the
data model and the database engine, and supports a large set of database systems
including MySQL, Oracle, Postgres, etc. Django also supports NoSQL database through
Django-nonrel fork. For now, the only NoSQL databases supported are MongoDB and
google app engine.
 Multilingual Support − Django supports multilingual websites through its built-in
internationalization system. So you can develop your website, which would support
multiple languages.
 Framework Support − Django has built-in support for Ajax, RSS, Caching and various
other frameworks.
 Administration GUI − Django provides a nice ready-to-use user interface for
administrative activities.
 Development Environment − Django comes with a lightweight web server to facilitate
end-to-end application development and testing.
 As you already know, Django is a Python web framework. And like most modern
framework, Django supports the MVC pattern. First let's see what is the Model-View-
Controller (MVC) pattern, and then we will look at Django’s specificity for the Model-
View-Template (MVT) pattern.
 MVC Pattern
 When talking about applications that provides UI (web or desktop), we usually talk about
MVC architecture. And as the name suggests, MVC pattern is based on three
components: Model, View, and Controller. Check our MVC tutorial here to know more.
 DJANGO MVC - MVT Pattern
 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).
 The following diagram illustrates how each of the components of the MVT pattern
interacts with each other to serve a user request −

 The developer provides the Model, the view and the template then just maps it to a URL
and Django does the magic to serve it to the user.
MVC (Model-View-Controller) and MVT (Model-View-Template) are both architectural design
patterns used in software development to separate the concerns of an application. While they
serve similar purposes, they are commonly associated with different technologies and
programming paradigms. Let consider each of them in brief detail:

MVC (Model-View-Controller):

MVC is a design pattern commonly associated with traditional server-side web applications,
desktop applications, and some mobile app frameworks. It separates an application into three
interconnected components:

1. Model: This represents the application's data and business logic. It defines the data structure,
handles data storage, retrieval, and manipulation. In the context of a web application, the model
often interacts with a database.

2. View: The view is responsible for displaying the data to the user. It deals with the presentation
layer, such as HTML/CSS in web applications. The view receives data from the model and
presents it to the user.

3. Controller: The controller acts as an intermediary between the model and the view. It handles
user input and controls the flow of data between the model and view. It processes user actions
and updates the model or view accordingly.

MVT (Model-View-Template):

MVT is a design pattern commonly associated with web frameworks like Django, which is a
Python web framework. It's very similar to MVC but uses different terminology:

1. Model: In MVT, the model is responsible for managing the application's data and business
logic, much like in MVC. It interacts with databases and handles data-related operations.

2. View: The view in MVT is responsible for defining what data is presented to the user, similar
to the view in MVC. It defines the presentation layer.

3. Template: The template in MVT is responsible for how the data is presented to the user. It
defines the structure of the HTML and may include placeholders for dynamic data. It's more
focused on the presentation than the view in MVC.

Installing Django:

 To get started, we’ll have to install Django, which means you’ll also have to install pip if
you haven’t already done so.
 Once you have Pip installed, you can run pip3 install Django in your terminal to install
Django.
Create the Django project
1. In the VS Code Terminal where your virtual environment is activated, run the following
command:

2. django-admin startproject web_project .

This startproject command assumes (by use of . at the end) that the current folder is your
project folder, and creates the following within it:
o manage.py: The Django command-line administrative utility for the project. You
run administrative commands for the project using python manage.py
<command> [options].
o A subfolder named web_project, which contains the following files:
 __init__.py: an empty file that tells Python that this folder is a Python
package.
 asgi.py: an entry point for ASGI-compatible web servers to serve your
project. You typically leave this file as-is as it provides the hooks for
production web servers.
o In the context of a Python web application, "ASGI" stands for "Asynchronous Server
Gateway Interface." ASGI is a specification for asynchronous web servers and
frameworks in Python. It allows web applications to handle multiple simultaneous
connections and asynchronous operations, making it well-suited for handling real-time
or long-lived connections, such as WebSocket communication, chat applications, and
other scenarios where traditional synchronous web servers may not be efficient. ASGI is
commonly used with web frameworks like Django for building asynchronous web
applications.
 settings.py: contains settings for Django project, which you modify in the
course of developing a web app.
 urls.py: contains a table of contents for the Django project, which you also
modify in the course of development.
 wsgi.py: an entry point for WSGI-compatible web servers to serve your
project. You typically leave this file as-is as it provides the hooks for
production web servers.
3. In the context of a Python web application, "WSGI" stands for "Web Server Gateway
Interface." WSGI is a specification for a standard interface between web servers and
Python web applications or frameworks. It defines a way for a web server to
communicate with Python applications, allowing for the development of web applications
that can run on a variety of web servers without modification.
4. The wsgi.py file you mentioned is typically found in Django web applications. It's used
to set up the WSGI interface for Django applications. When a web server like Apache or
Nginx serves a Django application, it interacts with the application through WSGI, which
in turn communicates with the Django application, allowing it to handle HTTP requests
and responses.
o
5. Create an empty development database by running the following command:
6. python manage.py migrate

When you run the server the first time, it creates a default SQLite database in the
file db.sqlite3 that is intended for development purposes, but can be used in production
for low-volume web apps. For additional information about databases, see the Types of
databases section.
7. To verify the Django project, make sure your virtual environment is activated, then start
Django's development server using the command python manage.py runserver. The
server runs on the default port 8000, and you see output like the following output in the
terminal window:

Performing system checks...

System check identified no issues (0 silenced).

January 15, 2021 - 14:33:31

Django version 3.1.5, using settings 'web_project.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.

Django's built-in web server is intended only for local development purposes. When you
deploy to a web host, however, Django uses the host's web server instead.
The wsgi.py and asgi.py modules in the Django project take care of hooking into the
production servers.
If you want to use a different port than the default 8000, specify the port number on the
command line, such as python manage.py runserver 5000.
8. Ctrl+click the http://127.0.0.1:8000/ URL in the terminal output window to open your
default browser to that address. If Django is installed correctly and the project is valid,
you see the default page shown below. The VS Code terminal output window also shows
the server log.
9. When you're done, close the browser window and stop the server in VS Code
using Ctrl+C as indicated in the terminal output window.

You might also like