0% found this document useful (0 votes)
27 views17 pages

0 Introduction

Uploaded by

umadataengg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

0 Introduction

Uploaded by

umadataengg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Django

Python
1
Django
• Django is a back-end server side web
framework.
• Django is free, open source and written in
Python.
• Django makes it easier to build web pages
using Python.
How does Django Work?

• Django follows the MVT design pattern (Model View


Template).
• Model - The data you want to present, usually data from a
database.
• View - A request handler that returns the relevant template
and content - based on the request from the user.
• Template - A text file (like an HTML file) containing the layout
of the web page, with logic on how to display the data.
Model

• The model provides data from the database.


• In Django, the data is delivered as an Object
Relational Mapping (ORM), which is a
technique designed to make it easier to work
with databases.
Model

• The most common way to extract data from a database is


SQL. One problem with SQL is that you have to have a
pretty good understanding of the database structure to be
able to work with it.
• Django, with ORM, makes it easier to communicate with
the database, without having to write complex SQL
statements.
• The models are usually located in a file called models.py.
View

• A view is a function or method that takes http


requests as arguments, imports the relevant
model(s), and finds out what data to send to
the template, and returns the final result.
• The views are usually located in a file
called views.py.
Template

• A template is a file where you describe how the


result should be represented.
• Templates are often .html files, with HTML code
describing the layout of a web page, but it can
also be in other file formats to present other
results, but we will concentrate on .html files.
Template

• Django uses standard HTML to describe the


layout, but uses Django tags to add logic:
• <h1>My Homepage</h1>
• <p>My name is {{ firstname }}.</p>
• The templates of an application is located in a
folder named templates.
URLs

• Django also provides a way to navigate around


the different pages in a website.
• When a user requests a URL, Django decides
which view it will send it to.
• This is done in a file called urls.py.
What is Going On?
• When you have installed Django and created you first Django web
application, and the browser requests the URL, this is basically what happens:
• 1.Django receives the URL, checks the urls.py file, and calls the view that
matches the URL.
• 2. The view, located in views.py, checks for relevant models.
• 3. The models are imported from the models.py file.
• 4. The view then sends the data to a specified template in
the template folder.
• 5. The template contains HTML and Django tags, and with the data it returns
finished HTML content back to the browser.
• Django can do a lot more than this, but this is basically what you will learn in
this tutorial, and are the basic steps in a simple web application made with
Django.
Django Environemt

• To install Django, you must have Python


installed, and a package manager like PIP.
• PIP is included in Python from version 3.4.
• python –version
• pip --version
Virtual Environment
• It is suggested to have a dedicated virtual environment
for each Django project, and one way to manage a
virtual environment is venv, which is included in
Python.
• 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
Virtual Environment
• 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
Virtual Environment
• 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>
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:
• >py -m pip install Django
Windows, Mac, or Unix?

• You can run this project on either one. There


are some small differences, like when writing
commands in the command prompt, Windows
uses py as the first word in the command line,
while Unix and MacOS use python:
• py --version
Check Django Version

• You can check if Django is installed by asking


for its version number like this:
• django-admin --version

You might also like