Django (1) - 1641346478120

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 40

Django

Python Web framework


Django – Introduction

Django is a web application framework written in Python


programming language. It is based on MVT (Model View
Template) design pattern. It takes less time to build application
after collecting client requirement. This framework uses a
famous tag line: The web framework for perfectionists with
deadlines.
Starting a Project
Starting a Project
• Choose a python interpreter
• Make sure you have your interpreter Python 3.7.4 or higher

Note: While choosing interpreter use relevant idea as discussed in class


Django Installation
• Open terminal
• Type following command to install Django

pip install django==3.0.2

• if this version is not supported try lower version


Start a Django Project
• To start a Django project use following command

django-admin startproject project_name .

• . Refers to parent directory and avoid folder repetition (but if you


forget to put ( .)a space and dot after project name it doesn’t affect in
any other way)
Start a Django Project
• Following files can be seen after creating a Django project
• __init__.py refers a python package
• asgi.py and wsgi.py are useful while
deploying a website to a live domain
• setting.py refers setting file
• urls.py helps in URL mapping
• manage.py is used to perform many
actions like run server, migration,
create super user etc.
Identify your projects modules
• Identify the different modules as per requirements of a web
application
• To create a Django module use following command

django-admin startapp module_name

For Example: products, bookings, services, appointments


Prepare your modules
• After creating a module you can see a folder containing following files
• __init__.py refers a python package
• admin.py is used to assign the module to
admin panel
• app.py refers to module configuration file
• models.py is used to deal with database
• tests.py is used for testing
• views.py refers to deal with presentation layer
Prepare your modules
• Better to make a python file urls.py for managing current module
paths
Right click to module folder
Go to “New”
Go to “Python File”
Name the file “urls” (don’t avoid convention)
Run the server
• Use following command to run server
python manage.py runserver
• You will be able see a server URL as seen below:
Run the server
• Use the address in web browser
• You should be able to view a Django (automatic built home page of a)
website for the first time. However it may disappear after starting
some URL mapping
URL Mapping
• Decide URL for your project according to modules
• For example
BASE_URL/products
BASE_URL/products/new
BASE_URL/services
• In this example BASE_URL is 127.0.0.1:8000 and our module is
products so we will map first two URLs i.e.
127.0.0.1:8000/products
127.0.0.1:8000/products/new
URL Mapping Contd.
• You should be able to view urls.py in Django project folder as:

• Make following change to define URLs start with BASE_URL/products/


URL Mapping Contd.
• Now in urls.py inside the products module:

• So, we have defined two URLs


• 127.0.0.1:8000/products/
• 127.0.0.1:8000/products/new
URL Mapping Contd.
• As we can see response of two URLs are returned by the function
show_products and show_new in views.py:

• Define these two functions in views.py file of the current module


URL Mapping Contd.
• Render in used to return a html page on HttpRequest
• As you can see two different pages are returned for two different
URLs
URL Mapping Contd.

Where to add html files??

Where to add locally linked files for html pages


like CSS, JS or images??
URL Mapping Contd.
• Make a folder/directory named “templates” to add html pages
• Make a folder/directory named “static” to add locally linked files
• Make both folders/directories in your root
(project) folder
{ otherwise you will have to define their
path differently than in this slide }
• As you can see you can manage different
types of files by making different folders in
static
URL Mapping Contd.
• Add path for “templates” and “static” in Django project “setting.py”
file as shown below:
URL Mapping Contd.
• Add any linked files in static folder and to load static links use the
following format :
MySQL Client Installation
• Open terminal one more time and use the given command to install
MySQL client
pip install mysqlclient
• If this doesn’t work get mysqlclient wheel file
( https://www.lfd.uci.edu/~gohlke/pythonlibs/
You can get the file in this link ) & place it in root
directory and use the following command
pip install mysqlclient-1.4.6-cp38-cp38-win32.whl
MySQL Client Installation
• If it still doesn’t work just run the following command
python -m pip install pymysql

• Add the following codes in “__init__.py” of Django project


Database Connection
• Create a database in MySQL for example “myproject”

• Add the following codes in “settings.py” of Django project (as


discussed in class)
Migration
• To migrate default tables for admin panel provided by Django, run the
command
python manage.py migrate
• You will be able to see various tables in your database
Migration
• To create table according to your module go to “models.py” in
respective module and define attributes for columns of database
table
Migration
• Go to “settings.py” in the list of installed apps add the configuration
file of your module
products
• Do accordingly for apps.py

all your modules


Migration
• Use this command to make a file ready for migration
python manage.py makemigrations
• Once again run following command to create
table in database
python manage.py migrate
• You will be able see a new table named as
<modulename_modelclass> ( in this example
“products_product ”)
Admin Panel
• Default admin panel provided by Django can be accessed in URL
“BASE_URL/admin” i.e. “127.0.0.1:8000/admin”
• To create a super user run this command:
python manage.py createsuperuser
• With username and password you will be
able to login in this admin panel
Admin Panel
• Django administrative panel
Admin Panel
• To add your module in Django administrative panel
• Go to “admin.py” of the relevant module and add following code:

Refresh your admin panel in browser to see the change


Admin Panel
Add this class to “admin.py” to make administrative panel looks clear:
Fetching Data
• Pass object to view and retrieve.
Example for products:
User Registration & Login
Add two html pages with registration and login form in templates folder
& define a module (“allusers”) and required URL mapping
User Registration & Login
User Registration & Login
User Registration & Login
• Add proper URL mapping for registration & login pages. Example:
(new module name is “allusers”)
New module’s “views.py”
Main project’s “urls.py”

New module’s “urls.py”


User Registration & Login
• In “views.py” we can get the form data & proceed them further (for
registration) on post request like:

These should match


exactly as name
attribute of the
form elements
User Registration & Login
• Similarly, for login:

Redirecting to
dashboard, do
according to your
URL mapping
A new module for logged in users
• Try a similar process to create any module and perform CRUD
functions:

You might also like