Chapter 5_Web Development Using Django
Chapter 5_Web Development Using Django
Web Development:
Web development refers to the process of creating, building, and maintaining websites and
web applications that run on the internet or intranet. It involves various tasks, from designing
a website's user interface to implementing its functionality and ensuring it performs well.
Web development is the backbone of the modern internet, enabling businesses, organizations,
and individuals to create a digital presence and provide services to users worldwide.
Frontend (Client-Side) Development : The part of a website users interact with directly,
often referred to as the UI (User Interface).
Backend (Server-Side) Development : The backend processes and delivers the data
requested by the frontend.
Django is a high-level Python web framework that enables developers to build robust,
scalable, and secure web applications rapidly. Known for its "batteries included"
philosophy, Django comes with a wide range of built-in features that simplify web
development tasks like authentication, database management, and URL routing.
What is Django?
1. Scalability:
o Ideal for small projects and enterprise-level applications alike.
2. Security:
o Protects against common vulnerabilities like SQL injection, cross-site
scripting (XSS), and cross-site request forgery (CSRF).
3. ORM (Object-Relational Mapping):
o Simplifies database interactions using Python classes instead of SQL queries.
4. Built-in Admin Interface:
o Automatically generates a web-based admin panel to manage application data.
5. Rapid Development:
o Streamlines repetitive tasks with pre-built tools.
6. Extensibility:
o Provides flexibility to integrate third-party packages or custom functionalities.
Django uses the Model-View-Template (MVT) pattern to separate concerns within a web
application.
Model:
Handles the business logic and communicates with the model and template.
Example:
Template:
Defines the presentation layer (HTML with placeholders for dynamic data).
Example (home.html):
Setting Up Django
Cmd:
D:\django-projects>> django-admin
Copy URL with port number and paste on browser- web application run
Project Folder structure:
Views.py
Import HttpResponse: It display user response on browser (display text msg on browser)
http:\\localhost:8000\about-us/
def about_us(request):
return HttpResponse("Welcome in Python Django")
def courese(request):
return HttpResponse("MCA")
Step2:add path
Urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('about/', views.about_us),
path('course/', views.course),
]
Browser: http://127.0.0.1:8000/about/
Int-> 2
Urls.py:
# path('course/<int:courseid>', views.courseDetails)
# path('course/<str:courseid>', views.courseDetails)
# path('course/<slug:courseid>', views.courseDetails)
Views.py
def courseDetails(request,courseid):
return HttpResponse(courseid)
Settings.py
TEMPLATES = [
{
'DIRS': [BASE_DIR,"templates"],
}
]
Argument 1: BASE_DIR : provides project folder path
Argument 2:template folder name
Urls.py
from TestProject import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.HomePage), #1st argument empty- open home page direct
path('about/', views.about_us),
]
def HomePage(request):
return render(request,"index.html")
def about_us(request):
return HttpResponse("Welcome in Python Django")
Passing Data from Django view to template: Dynamic
Views page- render(request,htmlpage,data)
Views.py
from django.shortcuts import render
def HomePage(request):
data={
'title':'Home Page',
'bdata':'welcome in DYP | Pune'
}
return render(request,"index.html",data)
index.html
<html>
<head>
<title>{{title}}</title>
<style>
.main{
width:800px;
background-color:red;
color:white;
font-size:30px;
padding:20px;
}
</style>
</head>
<body>
<div class="main">
<h1>{{bdata}}</h1>
</div>
</body>
</html>
Django Template using forloop:
For loop provides following properties:
forloop.counter : starts index from 1
forloop.counter0 : starts index from 0
forloop.revcounter : reverse index upto 1
forloop.revcounter0 : reverse index upto 0
forloop.first : first element active- True, remaining false
forloop.last : last element active- True, remaining false
Views.py
from django.shortcuts import render
def Home_page(request):
data={
'title':'Home Page',
'bdata':'Welocme in Django',
'course_list':['Python','Java','PHP'],
'stud_details':[{'name':'AAA','age':23},
{'name':'BBB','age':25},
{'name':'CCC','age':24} ],
'num':[12,3,4,56,67,45]
}
return render(request,"index.html",data)
index.py
<body>
<div class="main">
<h1>{{bdata}}</h1>
{% for n in course_list %}
<div>{{forloop.counter0}} {{n}}</div>
{% endfor %}
{% for n1 in num %}
{% if n1>20 %}
<div>{{n1}}</div>
{% endif %}
{% endfor %}
</div>
<table border='1' cellpadding='10'>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
{% for d in stud_details %}
<tr>
<td>{{d.name}}</td>
<td>{{d.age}}</td>
</tr>
{% endfor %}
</table>
</body>
Settings.py
STATICFILES_DIRS=[
BASE_DIR,"static"
]
OR
{% load static %}
.
.
<link href="{% static ‘default.css’ %} " rel="stylesheet" type="text/css"
media="all" />