A Web Development Python Framework: Presented By: Saqib Saud C Ii Mca
A Web Development Python Framework: Presented By: Saqib Saud C Ii Mca
PRESENTED BY :
SAQIB SAUD C
II MCA
OUTLINE
• Introduction to Django
• MVT Architecture
• Model layer
• View layer
• Template Layer
• Forms
• Automated admin interface
• Django Security
INTRODUCTION TO DJANGO
• Django is a high-level python web framework which was created for quick web
project development.
• It delivers transparent and high-quality code writing, making it important for
developers, and equally important for customers.
• It’s free and open source.
• Fast and Secure.
• Dynamic and database driven content based websites.
HISTORY
MVT Architecture :
• Models - Describes your data structure/database schema
• Views - Controls what a user sees
• Templates – How a user sees it
• Controller – Controls request and responses
MVT Architecture :
MVT Architecture :
• URLs: A URL mapper is used to redirect HTTP requests to the appropriate
view based on the request URL.
• View: A view is a request handler function, which receives HTTP requests
and returns HTTP responses.
• Models: Models are Python objects that define the structure of
an application's data, and provide mechanisms to manage (add, modify,
delete) and query records in the database.
• Templates: A template is a text file defining the structure or layout of a
file(such as an HTML page). A template can be used to define the
structure of any type of file; it doesn't have to be HTML!
INSTALLING PYTHON:
Once you've created a virtual environment, you can use pip3 to install Django.
- pip3 install django
You can test that Django is installed by running the following command.
- py –m django –version
Now you can create your website using django
- django-admin startproject myfirstproject
- cd myfirstproject
WORKING OF DJANGO:
We can run the development web server from within this folder
using manage.py and the runserver command.
- python3 manage.py runserver
Once the server is running you can view the site by navigating to the following
URL on your local web browser: http://127.0.0.1:8000/
DJANGO MODEL LAYER :
Django uses Field Types for data validation in automatically generated forms.
E.G: AutoField() - An interger field that automatically increments
BooleanField() - Store true/false value and generally used for
checkboxes
CharField() - A string field for small to large-sized strings.
DateField() - A date field represents python datetime.date
FIELD OPTIONS:
• Field options are used to customize and put constraint on table rows.
• Each Field takes certain field specific arguments.
E.g:- name = models.CharField(max_length=60)
Here “max_length” specifies the size of the VARCHAR field.
The following are some common and mostly used field options:
a) null – To store empty values as NULL in database
b) blank - If True, the field is allowed to be blank. Default is False
c) default – stores default value for a field
d) primary_key – This field will be the primary key for the table
e) unique_key – Puts unique key constraint for a column
MODEL FIELD RELATIONSHIPS:
• We created a model Student that contains the following code in models.py file.
//models.py
class Student(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=30)
contact = models.IntegerField()
email = models.EmailField(max_length=50)
age = models.IntegerField()
DJANGO MODEL EXAMPLE
It will create a table myapp_student. The table structure looks like the below.
DJANGO VIEW LAYER:
• A view is a place where we put our business logic of the application. The view is a
python function which is used to perform some business logic and return a response
to the user.
• This response can be the HTML contents of a Web page, or a redirect, or a 404
error.
• All the view function are created inside the views.py file of the Django app.
DJANGO VIEW EXAMPLE:
//views.py
import datetime
# Create your views here.
from django.http import HttpResponse
def index(request):
now = datetime.datetime.now()
• Users authentication.
• Permissions to user: Binary (yes/no)
• Groups: A generic way of applying labels and permissions to more
than one user.
• A configurable password hashing system.
• Forms and view tools for logging in users, or restricting content.
CACHING:
• To cache something is to save the result of an expensive calculation so that you
don’t have to perform the calculation next time.
• Django comes with a robust cache system that lets you save dynamic pages so
they don’t have to be calculated for each request.
Example:
LOGGING: