Best Python Django Tutorial For Beginners – With Project Structure PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Best Python Django Tutorial For Beginners – With Project Structure


1
1
Python Django Tutorial - What is Django?
1
2
2
Python Django Tutorial - What is
Django? Contents Python Django Tutorial - What is
Django?...........................................
..................................................
..1 Python Django Tutorial - Install
Django............................................
..................................................
.....3 Python Django Tutorial - Serving a Request
for a Website.....................................
.................................4 Python Django
Tutorial - History................................
..................................................
............................5 Python Django
Tutorial - MVT Pattern............................
..................................................
.......................5 Python Django Tutorial -
Features of Django................................
..................................................
.........7 a. Scalability......................
..................................................
..................................................
..................8 b. Portability.............
..................................................
..................................................
...........................8 c.
Security..........................................
..................................................
..................................................
.8 d. Versatility..............................
..................................................
..................................................
..........8 e. Packages........................
..................................................
..................................................
.................8 f. Ease of
Use...............................................
..................................................
.........................................9 Prerequ
isites to Creating a Project in
Django............................................
...............................................9 a
. Starting Project.............................
..................................................
..................................................
....9 b. Running the Server....................
..................................................
..................................................
.......9 c. Setting up a database..............
..................................................
..................................................
........11 d. Web Server.......................
..................................................
..................................................
............11 Django Project Structure...........
..................................................
..................................................
..........11 a. manage.py......................
..................................................
..................................................
...............12 b. Project
folder............................................
..................................................
.......................................13 c.
db.sqlite3........................................
..................................................
.................................................1
4 Creating an Application.........................
..................................................
................................................14
Python Django Admin Interface....................
..................................................
........................................16 How to
Create Simple Project in Django?..................
..................................................
...........................18 Conclusion..........
..................................................
..................................................
.................................21
2
3
3
Django is a high-level Python framework. It is
free and open-source, written in Python itself,
and follows the model-view-template architectural
pattern. We can use it to develop quality web
applications faster and easier. Since developing
for the web needs a set of similar components,
you can use a framework. This way, you dont have
to reinvent the wheel. These tasks include
authentication, forms, uploading files,
management panels, and so.
Best Python Django Tutorial For Beginners
Python Django Tutorial - Install Django To work
with Django on your system, C\Users\lifeigtpip
install Django Collecting Django
3
4
4
Lets Explore Unique Features of Python
Programming Language
Downloading
https//files.pythonhosted.org/packages/ab/15/cfde
97943f 0db45e4f999c60b696fbb4df59e82bbccc686770f4e
44c909 4/Django-2.0.7-py3-none-any.whl (7.1MB)
100
7.1MB 610kB/s
Requirement already satisfied pytz in
c\users\lifei\appdata\localVprogramsVpython\pytho
n36\lib\ site-packages (from django) (2018.5)
Installing collected packages django
Successfully installed django-2.0.7
Python Django Tutorial - Serving a Request for a
Website Lets first find out, in laymans terms,
what happens when your server receives a request
for a website. The request is passed to Django
and that tries to analyze this request. The
urlresolver tries to match the URL against a list
of patterns. It performs this match from top to
bottom. If it can find a match, it passes the
request to the view, which is the associated
function. The function view can check if the
request is allowed. It
4
5
5
also generates a response, and then Django sends
it to the users web browser.
Python Django Tutorial - History Adrian
Holovaty and Simon Willison created Django in the
fall of 2003 at the Lawrence Journal-World
newspaper Django publicly released under a
BSD license in July 2005 named after guitarist
Django Reinhardt Today, Django is an
open-source project with contributors around the
world
Python Django Tutorial - MVT Pattern MVC stands
for Model-View-Controller. We use this when we
want to develop applications with user
interfaces. MVT stands for Model-View-Template. A
template is an HTML file mixed with DTL (Django
Template Language). Django takes care of the
Controller part, which is the software code and
controls the interaction between the other two
parts- Model and View. When a user requests for a
resource, Django acts as a controller and checks
if it is available. If the URL maps, View
interacts with the Model and renders a Template.
Python Django sends back a Template to the user
as a response.
5
6
6
Read about Python Decision Making Statements with
Syntax and Examples
Template
Best Python Django Tutorial For Beginners
The model helps us handle database. View executes
business logic and interacts with Model to carry
data. It also renders Template. Template handles
the user interface and is a presentation
layer. The Model class holds essential fields and
methods. For each model class, we have a table in
the database. Model is a subclass of
django.db.models.Model. Each field here denotes a
database field. With Django, we have a
database-abstraction API that lets us perform
CRUD (Create-Retrieve-Update-Delete) operations
on mapped tables.
6
7
7
Python Django Tutorial - Features of Django
When working with Python Django, you can expect
the following Django Features-
Security
Versatility
Portability
Packages
Python
Scalability
Ease of Use
Features of
django
Data
Flair
Best Python Django Tutorial - Features of Django
7
8
8
a. Scalability When you need to scale your
system, you can simply add more web nodes to your
Django. That is, you can scale it horizontally.
Two products that use Djangos scalability are
Disqus and Instagram.
b. Portability The portability of Python makes
for a portable Django too. Various platforms
include Windows, Linux, and MacOS. c.
Security Python Django ensures some arrangements
for security too. One of these is that it stores
hashed passwords in cookies. d.
Versatility Python Django will work with formats
like HTML, JSON, XML, among others. It also
supports many different client-side frameworks.
So, we can use it to build anything including
regular websites and social networks. e.
Packages
8
9
9
Django Programming has the foundation of
thousands of additional packages. f. Ease of
Use Features like the built-in admin interface
make it easy to build with Django. It is also
fully functional and finds it easy to switch
databases. Prerequisites to Creating a Project in
Django In this Python Django Tutorial, we will
study the prerequisites to create a project in
Python Django. a. Starting Project Use the
following command in the command prompt to begin
your project- C\Users\lifei\Desktopgtdjango-admin
startproject projectO Then move to this
folder. C\Users\lifei\Desktopgtcd project0
C\Users\lifei\Desktop\project0gt b. Running
the Server You could apply these migrations
before you start your server- C\Users\lifei\Deskt
op\project0gtpython manage.py migrate
9
10
10
Now, start the server- C\Users\lifei\Desktop\proj
ect0gtpython manage.py runserver Performing system
checks... Have a look at Python Function
Arguments with Types, Syntax, and Examples
System check identified no issues (0 silenced).
July 08, 2018 - 215139 Django version 2.0.7,
using settings projectO.settings Starting
development server at http//127.0.0.18000/ Quit
the server with CTRL-BREAK. If you see something
like this, it means you have successfully
installed Django
10
11
11
Python Django Tutorial - Prerequisites to
Creating a Project
c. Setting up a database For our project, we
have set up MySQL http//www.mysql.com/ Python
also supports other database products like
Oracle, SQLite 3, MongoDB, PostgreSQL, and
GoogleAppEngine Datastore. d. Web Server While
Django has its own lightweight web server, you
can also use your own- like Apache. Django
Project Structure This is what your project
directory will look like
11
12
12
Python Django Tutorial - Project Structure
Do you know the difference Between Python
Generator vs Iterator
a. manage.py This script lets us interact with
our project using the command line. Facilities
include starting up the server and syncing to the
database.
12
13
13
Python Django Tutorial - manage.py b. Project
folder
This holds the packages for our project-
A 1 Name Date modified Type
, _pycache_ 08-Jul-18 940 PM File folder
, wl_init_.py 08-Jul-18 836 PM PY File
settings.py 08-Jul-18 836 PM PY File
- J urls.py 08-Jul-18 836 PM PY File
t t w) wsgi.py 08-Jul-18 836 PM PY File
Python Django Tutorial - Project folder
init_.py- A Python package.

13
14
14
settings.py- This holds website settings
like database configuration details.
urls.py- This holds links in your project and the
function(s). wsgi.py- This deploys our
project over WSGI and helps the app communicate
with the web server. WSGI stands for Web Server
Gateway Interface. _pycache_- This directory
holds files like _init_.cpython-36.pyc,
settings.cpython-36.pyc, urls.cpython-36.pyc, and
wsgi.cpython-36.pyc. In settings.py, DEBUG is set
to True. This is okay at the time of deployment,
but you should set it to False when working with
a live project. This is because this gives out
information about errors in your project. Another
construct you will find in this file is- 1.
DATABASES 2. default 3. ENGINE
django.db.backends.sqlite3, 4. NAME
os.path.join(BASE_DIR, db.sqlite3), 5. 6.
c. db.sqlite3 This is the database file for
your project and has the extension
.sqlite3. Creating an Application
14
15
15
Lets learn how to create an application in
Django by this python Django Tutorial. Projects
are modular. A contact form is one such
application, and you can borrow it from one
project for another. You can start an application
this way C\Users\lifei\Desktop\project0gtpython
manage.py startapp app0 This creates the
following contents in the directory project0
Python Django Tutorial - Creating an Application
gt projectO gt appO gt
v O Search appO
I I Name migrations _init_.py m admin.py 0
apps.py models.py 4 tests, py m views.py
Date modified 09-Jul-18 734 PM 09-Jul-18 734 PM
09-Jul-18 734 PM 09-Jul-18 734 PM 09-Jul-18
734 PM 09-Jul-18 734 PM 09-Jul-18 734 PM
Type File folder PY File PY File PY File PY File
PY File PY File

Python Django Tutorial - How to Create an
Application Migrations- This holds
another_init_.py file.
15
16
16
_init_.py- This is a Python package.
admin.py- This lets us make the app modifiable in
the admin interface. apps.py- This holds the
names of all your applications. models.py-
This holds all application models. tests.py-
This holds unit tests for the project.
views.py- This holds the application views for
the project. Lets prepare our self with Top 35
Python Interview Questions and Answers In your
settings.py, you can add your app name in the
INSTALLED_APPS construct- 1. INSTALLED_APPS
2. django.contrib.admin, 3.
django.contrib.auth, 4. django.contrib.conte
nttypes, 5. django.contrib.sessions, 6.
django.contrib.messages, 7.
'django.contrib.staticfiles', 8. 'app0' 9.
Python Django Admin Interface
16
17
17
Here, in this Python Django Tutorial, we are
going to see how to use Django Admin
Interface? Using the Django admin interface, you
can perform CRUD operations on the model. This
interface depends on the django.countrib model
it is enabled by default. You can find it as
django.contrib.admin, in INSTALLED_APPS in
settings.py. To access this interface, you can
try one of two methods-
127.0.0.18000/admin localhost8000/admi
n You get something like this
Python Django Tutorial - Admin Interface
Do you know What is Serialization in Python with
Example
17
18
18
a. Creating a Superuser To create a superuser,
you can push in the following command- python
manage.py createsuperuser Once youre done
creating a superuser, use this username and
password to login to the dashboard.
Python Django Tutorial - How to Create a Super use
How to Create Simple Project in Django? We can
create a simple view using the following code- 1.
gtgtgt from django.http import HttpResponse 2.
gtgtgt def hello(request) 3. return
HttpResponse(MHelloM)
18
19
19
We save this file as views.py in the inner
projectO directory. This is how we can import
it- 1. gtgtgt os.chdir('CVVUsersVVlifeiV\DesktopV
Vproject0') 2. gtgtgt from projectO.views import
hello 3. a. Mapping to a URL Now in the inner
projectO directory, you have a file urls.py. It
looks something like this- project0 URL
Configuration The 'urlpatterns' list routes URLs
to views. For more information please
see https//docs.djangoproject.com/en/2.0/topics/
http/urls/ Examples Function views Add an
import from my_app import views Add a URL
to urlpatterns path(, views.home,
namehome) Class-based views Add an
import from other_app.views import Home Add
a URL to urlpatterns path(, Home.as_view(),
namehome) Including another URLconf
Import the include() function from django.urls
import include, path
19
20
20
Add a URL to urlpatterns path(blog/,
indude(blog.urls))
1. gtgtgt from django.contrib import admin 2. gtgtgt
from django.urls import path 3. gtgtgt
urlpatterns 4. path('admin/,
admin.site.urls), 5. In this, the urlpatterns
tuple maps URLs to views. Add this line of import
to urls.py from project0.views import hello And
then add this value to your urlpatterns tuple 1.
path('hello/',hello), urls.py - Notepad File Edit
Format View Help II IV IV from django.contrib
import admin from django.urls import path from
project.views import hello urlpatterns
path(1 admin/1, admin.site.urls), path(1
hello/1,hello), Best Python Django Tutorial
Mapping to a URL
20
21
21
Now, go to the following address-http//127.0.0.1
8000/hello/ O C !gt r lt D 127.0.0.18000/hello/ He
llo
Best Python Django Tutorial For Beginners So,
this was all about Python Django Tutorial. Hope
you like our explanation. Conclusion Hence, in
this Python Django Tutorial, we get started with
Django, a very common framework with Python.
Here, we studied History Features of Django. In
addition, we cover the MVT Pattern, Prerequisites
to create a Project, and many more. Still, you
have a query regarding Python Django Tutorial,
leave comments below.
21
Write a Comment
User Comments (0)
About PowerShow.com