Académique Documents
Professionnel Documents
Culture Documents
Django?
By:
Madhu Singh(PGT Computer Science)
DPSG, Meerut Road.
Django pronounced as JANG-HO
Secure
Django helps developers avoid many common security
mistakes by providing a framework that has been engineered
to "do the right things" to protect the website automatically.
Django enables protection against many vulnerabilities by
default, including SQL injection, cross-site scripting, cross-
site request forgery and clickjacking (see Website
security for more details of such attacks).
Why Django? contd…
Scalable
Django uses a component-based “shared-nothing” architecture (each part of the architecture
is independent of the others, and can hence be replaced or changed if needed). Having
a clear separation between the different parts means that it can scale for increased traffic by
adding hardware at any level: caching servers, database servers, or application
servers. Some of the busiest sites have successfully scaled Django to meet their demands
(e.g. Instagram and Disqus, to name just two).
Maintainable
Django code is written using design principles and patterns that encourage the creation of
maintainable and reusable code. In particular, it makes use of the Don't Repeat Yourself
(DRY) principle so there is no unnecessary duplication, reducing the amount of code.
Django also promotes the grouping of related functionality into reusable "applications" and,
at a lower level, groups related code into modules (along the lines of the Model View
Controller (MVC) pattern).
Why Django? contd…
Portable
Django is written in Python, which runs on many platforms. That
means that you are not tied to any particular server platform, and can
run your applications on many flavours of Linux, Windows, and Mac
OS X. Furthermore, Django is well-supported by many web hosting
providers, who often provide specific infrastructure and documentation
for hosting Django sites.
History
Where did it come from?
The main tools that Django itself provides are a set of Python scripts for creating
and working with Django projects, along with a simple development
webserver that you can use to test local (i.e. on your computer, not on an external
web server) Django web applications on your computer's web browser.
What operating systems are supported?
Install the Python Package Repository (PyPi) the pip tool (only for the
first time)
go to the command prompt and get back to root
c:\users\madhu>cd \
c:\> python -m pip install -U pip
3. Install the Python Virtual Environment (only for the first time)
c:\> pip install virtualenv
Now install Django
Creating a project
First open a command prompt/terminal, make sure you are in your virtual environment,
navigate to where you want to store your Django apps (make it somewhere easy to find
like inside your documents folder), and create a folder for your new website (in this
case: django_projects). Then enter into the folder using the cd command:
c:\>md cbseworkshop
c:\>cd cbseworkshop
Create a virtual environment (only for the first time)
c:\workshop>virtualenv django
If you get inside the django folder, you will see the following:
\Include
\Lib
\Scripts
pip-selfcheck.json
Creating a project contd…
def home(request):
return HttpResponse('Welcome to CBSE Training’)
(django) c:\workshop\pj2\pj2>
Edit views.py
from django.http import HttpResponse
from django.template import engines
from django.template.loader import render_to_string
def home(request):
title='welcome page in HTML'
author=‘Madhu Singh'
about_template = ‘’’<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
Project 2 contd…
<body>
<center><h1>About ''' + title + '''</h1></center>
<center><p>Welcome to the <font color=red>First Page</font> in
HTML</p></center>
<center><table border=2 bgcolor=yellow>
<tr><th>Roll</th><th>Name</th><th>Age</th></tr>
<tr><td>101</td><td>Amit</td><td>15</td></tr>
<tr><td>102</td><td>Sahil</td><td>17</td></tr>
</table>
<center><p><h1>Thank You</h1></center>
</body>
</html>’’’
django_engine = engines['django']
template = django_engine.from_string(about_template)
html = template.render({'title': title, 'author': author})
return HttpResponse(html)
Output of project 2
•Type http://127.0.0.1:8000/ with your Web browser.
Output
•.
GET https://website.com/path/to/image.jpg
The HTTP POST method is used to send user-generated data to the web server. For
example, a POST method is used when a user comments on a forum or if they upload a
profile picture. A POST method should also be used if you do not know the specific
URL of where your newly created resource should reside. In other words, if a new forum
thread is created and the thread path is not specified then you could use some like:
GET AND POST METHOD contd…
GET is suitable for things like a web search form because the URLs that
represent a GET request can easily be bookmarked, shared or resubmitted.
Django handles three distinct parts of the work involved in forms.
Preparing and restructuring data to make it ready for rendering
Creating HTML forms for data.
Receiving and processing submitted forms and data from the client.
Creating project using GET and POST
method (project 3)
C:\cbse\ django-admin startproject finalproject
C:\cbse\ finalproject\ python manage.py runserver
C:\cbse\ finalproject\ finalproject
Edit urls.py
urlpatterns = [
url(r'^getdata/', views.index),
url(r'^$', views.index), ]
Project 3 contd…
Create a file views.py in the same folder type the following code
C:\Users\madhusingh\Anaconda3\lib\site-
packages\django\contrib\admin\templates\index.html
Project 3 contd…
<head>
<title>use of post</title>
</head>
<center>
<h2>use of Post method </h2>
<form method="post" action="\getdata\">
<table>
<tr><td> Enter your name :</td>
<td><input name="name" type="text" value=""></td>
</tr><tr><td>
<br>
Enter your title<input name="title" type="text" value=""></td>
</tr><tr><td>
Enter the first value
<input name="v1" type="text" value=""><br>
Project 3 contd…