Search . . .
Understanding
Views, Templates
& URL
Session-3
simrantechmentor
Search . . .
Current Situation
We have used simple static data to display on website with the help of HttpResponse
01 class.
What if we have to create complex output with multiple different tags which can be
02
static as well as dynamic.
03 So now we will be creating html pages.
simrantechmentor
Search . . .
Lets Create HTML Page
Step 1. Create a folder which will contain our html web Search . . .
pages.
We will be creating a folder with the name of templates in our project. This folder should
be outside app and within project.
02
01
simrantechmentor
Step 2. Create an html file within template folder Search . . .
For example we will create basic.html file .
02
01
simrantechmentor
Step 3. Write html content Search . . .
simrantechmentor
Step 4. Go to settings file and update DIRS list Search . . .
simrantechmentor
Step 5. Go to views.py file of your project and update it Search . . .
simrantechmentor
Search . . .
Let’s Show dynamic
content.
Step 1. Go to views.py file of your project and update it Search . . .
simrantechmentor
Step 2. Go to html file of your project and update it Search . . .
simrantechmentor
Step 1. Create an html file within template folder Search . . .
For example we will create basic.html file .
02
01
simrantechmentor
Step 2. Write your layout code in base.html Search . . .
simrantechmentor
Step 3. Update basic.html Search . . .
simrantechmentor
Step 4. Go to browser and run your project. Search . . .
simrantechmentor
Search . . .
Let’s Create Multiple
Views
Step 1. Create a new view (Example course_detail) Search . . .
simrantechmentor
Step 2. Create a new html file inside template folder Search . . .
For example we will create course_detail.html
simrantechmentor
Step 3. Go to browser and run your project. Search . . .
simrantechmentor
Search . . .
Let’s Create Layout
Search . . .
Layout
01 In a website we see that all web pages have same layout with different data.
In Django we can create layout that will decrease our code , bring consistency and save
02
time
simrantechmentor
Step 3. Update urls.py file in your app and set new Search . . .
path
simrantechmentor
Step 4. Go to browser and run your project. Search . . .
Type : 127.0.0.1:8000/dm
simrantechmentor
Search . . .
Calculation using
Forms
Step 1. Create a new html file inside template folder Search . . .
(Example addition.html)
simrantechmentor
Step 2. Create a new view in your app Search . . .
For example calc_form in our app.
simrantechmentor
Step 3. Update path for in urls.py file of your app. Search . . .
simrantechmentor
Step 4. Go to browser and run your project. Search . . .
Run server : py manage.py runserver
Type : 127.0.0.1:8000/add
simrantechmentor
Step 5. Update addition.html file Search . . .
Add action in form
simrantechmentor
Step 6. Create new view that will calculate result. Search . . .
simrantechmentor
Step 7. Create new html file that will display your Search . . .
result.
For example (result.html)
simrantechmentor
Step 8. Update urls.py file Search . . .
simrantechmentor
Step 9. Go to browser and run your project. Search . . .
Run server : py manage.py runserver
Type : 127.0.0.1:8000/add
Add values in first number and second number.
Click Addition button
simrantechmentor
Search . . .
HTTP METHODS
GET VS POST
UNDERSTANDING HTTP Search . . .
01 Hypertext transfer Protocol is used to enable communication between client and server.
02 HTTP use “request and response” protocol
A client sends and Http request to the server and it return server send response to the
03 client.
In its response server send status information about the request resource as well as it
04 contain request content if the response is OK.
simrantechmentor
HTTP METHODS Search . . .
01 Hypertext transfer Protocol is used to enable communication between client and server.
02 HTTP use “request and response” protocol
A client sends and Http request to the server and it return server send response to the
03 client.
In its response server send status information about the request resource as well as it
04 contain request content if the response is OK.
simrantechmentor
HTTP METHODS Search . . .
simrantechmentor
GET REQUEST Search . . .
01 GET is used to request data from a specified resource and it is by default method.
02 Data is sent in the URL of a GET request in key/value pair
Should not be used while dealing with sensitive data as it is visible in URL of browser
03
04 GET request are only used to request data and you cannot modify.
05 There is a restriction on the length of the data. (URL length is 2048 characters max)
simrantechmentor
POST REQUEST Search . . .
01 GET is used to send data to a server to create or update a resource.
02 Data is sent to server with POST is stored in request body of the Http request.
There is no restriction on data length
03
04 It cannot be bookmarked.
05 It doesnot remain in browser history.
simrantechmentor
Search . . .
POST DEMO
Step 1. Update addition.html file and add method POSTSearch . . .
in form tag
simrantechmentor
Step 2. Update view method Search . . .
simrantechmentor
Step 3. Go to browser and run your project. Search . . .
Run server : py manage.py runserver Error
Type : 127.0.0.1:8000/add
Add values in first number and second number.
Click Addition button
simrantechmentor
CROSS SITE REQUEST FORGERY Search . . .
(CSRF)
01 Web security vulnerable
02 It allows an attacker to induce users to perform action that they do not need to perform.
It forces an authenticated user to submit a request to server against which they are
03 authenticated.
simrantechmentor
Django protects you from CSRF Search . . .
Open settings.py file of your project. You will see a middleware already installed that
takes care of CSRF attack. You just have to implement it.
simrantechmentor
Step 1. Update addition.html file and add method POSTSearch . . .
in form tag
Add {% csrf_token %} in form block
simrantechmentor
Step 2. Go to browser and run your project. Search . . .
Run server : py manage.py runserver
Type : 127.0.0.1:8000/add
Add values in first number and second number.
Click Addition button
simrantechmentor
Search . . .
RECAP MVT MODEL
Search . . .
simrantechmentor
Search . . .
simrantechmentor
Search . . .
THANK YOU
Kindly perform all in class exercise before coming to lab
END SLIDE
simrantechmentor
Contact No : 9878138788
Email : [email protected]