Introduction to Django Framework:
Django is a high-level python web framework used to build secure, scalable, database-driven websites quickly.
Think of Django as: a complete toolbox to build websites using python
Why Django?
Fast development
Built-in security(SQL,injection,CSRF,XSS Protection)
Built-in ORM(no need to write sql manually)
Read-made admin panel
Django feature:(user Authentication, admin dashboard, database ORM,URL routing,
forms & validation, security(CSRF,SQL injection).
MVT (Module view template)
Layer Meaning Role
Model database database & data logic
view Logic Business logic
Template UI User Interface(HTML)
Step1: Create the Environment and activate
PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38> python -m venv ccitenv
PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38> Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38> ccitenv/Scripts/activate
Step2: Install Django
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38> pip install django
Check environment using pip freeze command
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38> pip freeze
asgiref==3.11.0
Django==6.0.1
sqlparse==0.5.5
tzdata==2025.3
Step3: Start the project
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38> django-admin startproject ccitproject1
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38>
Below screen automatically created these files, you have create view,templates ..etc
You have to go inside the directory
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38> cd .\ccitproject1
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38\ccitproject1>
Flask File structure
Projectname
app.py
templates /.html
static /css /.css
static/image/.img/.png
static/js/.js
Step4: in Django run the project use manage.py , just like we use in flask app.py
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38\ccitproject1> python manage.py runserver
This default page home for django
Step5: need to create view.py page here you need write business logic
view.py
def home():
return "Welcome to Homepage!"
Step6:
You have register the page in urls.py file , we have import views and route to home page give name to that 'home'
from .import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home')
]
Output getting error: response issue
C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38\ccitenv\Lib\site-packages\django\core\handlers\exception.py, line 55, in inner
response = get_response(request
Step7: We have inform which of response, you have to send the home page,
we have different type of response
html "<h1>Welcome to ccit</h1>"
json - {"course":"AWS"}
http
Step8: see below first line is the http response
from django.http import HttpResponse
def home(request):
return HttpResponse("<h1>Welcome to the Home page!</h1>")
Output :
(ccitenv) PS C:\Users\Administrator\Desktop\CCIT\PythonCourse\Day38\ccitproject1> python manage.py runserver
Step9: here urls.py page you have registered every page where you writing business logic (view.py)
Task2: json response planning to send response to home page
Step1:
view.py
from django.http import HttpResponse
from django.http import JsonResponse
def home(request):
return HttpResponse("<h1>Welcome to the Home page!</h1>")
def course(request):
data = {
'course_name': 'Django Web Development',
'duration': '6 weeks',
'level': 'Beginner'
}
return JsonResponse(data)
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('course/', views.course, name='course')
]
Output:
Task3: Step1:
home.html
<h1>Welcome to the Home page CCIT!</h1>
view.py
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
def course(request):
data = {
'course_name': 'Django Web Development',
'duration': '6 weeks',
'level': 'Beginner'
}
return JsonResponse(data)
Step2: We have define our project 'ccitproject1', and give templates DIRS 'DIRS': ['templates'],
We have to register the hom.html in settings.py file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ccitproject1',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('course/', views.course, name='course')
]
Output: We have return html page
Task4:
We have copy the index.html which was worked earlier copy the text to home.html
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cloud Computing In Telugu</title>
<link rel="stylesheet" href="static/css/style.css">
</head>
<body>
<nav >
<ul class="menu">
<li><a href = "/">Home</a></li>
<li><a href="/aboutus">About Us</a></li>
<li><a href="/contactus">Contact Us</a></li>
</ul>
</nav>
<!-- Header Section -->
<header>
<h1>AWS Cloud Computing</h1>
<section>
<p>Learn how websites are built from scratch</p>
</header>
<main>
<section id="sec4">
<form method="post" >
<button type="submit">Enroll Now</button>
{% if msg %}
{{ msg|safe }}
{% endif %}
</form>
<h2>Join Our Cloud Computing Course Today!</h2>
</section>
</main>
<!-- Footer -->
<footer>
<p>Regards,</p>
<p>
<a href="https://www.Google.com" target="_blank">
Google Page
</a>
</p>
</footer>
</body>
</html>
contactus.html same aboutus.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cloud Computing In Telugu</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<nav >
<ul class="menu">
<li><a href = "/">Home</a></li>
<li><a href="/aboutus">About Us</a></li>
<li><a href="/contactus">Contact Us</a></li>
</ul>
</nav>
<!-- Header Section -->
<header>
<h1>AWS Cloud Computing</h1>
<section>
<p>Learn how websites are built from scratch</p>
</header>
<main>
<h2>Join Our Cloud Computing Course Today! Contact us</h2>
</main>
<!-- Footer -->
<footer>
<p>Regards,</p>
<p>
<a href="https://www.Google.com" target="_blank">
Google Page
</a>
</p>
</footer>
</body>
</html>
view.html
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
def course(request):
data = {
'course_name': 'Django Web Development',
'duration': '6 weeks',
'level': 'Beginner'
}
return JsonResponse(data)
def aboutus(request):
return render(request, 'aboutus.html')
# register the contactus view
def contactus(request):
return render(request, 'contactus.html')
setting.py no change same as above
Output:
Aboutus Page , it will automatically route to aboutus.page
Contactus page:http://127.0.0.1:8000/contactus/
Home page: http://127.0.0.1:8000/
Task5:
Multiple html page repeated code, to over come need to write base.html extended the base.html to
all html page for main script
base.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cloud Computing In Telugu</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
<nav >
<ul class="menu">
<li><a href = "/">Home</a></li>
<li><a href="/aboutus">About Us</a></li>
<li><a href="/contactus">Contact Us</a></li>
</ul>
</nav>
<!-- Header Section -->
<header>
<h1>AWS Cloud Computing</h1>
<section>
<p>Learn how websites are built from scratch</p>
</header>
<main>
{% block content %}
{% endblock %}
</main>
<!-- Footer -->
<footer>
<p>Regards,</p>
<p>
<a href="https://www.Google.com" target="_blank">
Google Page
</a>
</p>
</footer>
</body>
</html>
home.html
{% extends 'base.html' %}
{% block content %}
<h2>Join Our Cloud Computing Course Today! Home</h2>
{% endblock %}
contactus.hmtl
{% extends 'base.html' %}
{% block content %}
<h2>Join Our Cloud Computing Course Today! Contactus</h2>
{% endblock %}
aboutus.html
{% extends 'base.html' %}
{% block content %}
<h2>Join Our Cloud Computing Course Today! aboutus</h2>
{% endblock %}
Advantage if anything adding page just add the line in base.html it will effect home page
<li><a href="/course">Course</a></li>
course.html
{% extends 'base.html' %}
{% block content %}
<h2>Join Our Cloud Computing Course Today! Course </h2>
<p>Course Name: {{ course_name }}</p>
<p>Duration: {{ duration }}</p>
<p>Level: {{ level }}</p>
{% endblock %}
Output:
--Thanks Tomorrow session
Model (M) --Data Layer
- A Model is a python class that represents a database table
- Attributes inside the class define the table columns and their data types.
- Models store and manage application data.
- UI pages are generated using model data via view and templates.
Example: view.py
from django.db import models
class student(models.Model):
name= models.CharField(Max_length=100)
age= models.IntegerField()
email= models.EmailField()
Each class -->table
Each field -->column
Django ORM handles SQL for you
You can able Write url way also just for refernce
<li><a href="{/aboutus">About Us</a></li>
<li><a href="/contactus">Contact Us</a></li>
<li><a href="/course">Course</a></li>
OR
<li><a href="{% url 'aboutus' %}">About Us</a></li>
<li><a href="{% url 'contactus' %}">Contact Us</a></li>
<li><a href="{% url 'course' %}">Course</a></li>
--Thanks
No comments:
Post a Comment