0% found this document useful (0 votes)
2 views45 pages

Djangotutorial (AutoRecovered)

The document outlines the basics of Django, a high-level Python web framework used for building dynamic websites, detailing its architecture, components, and features such as the admin panel and ORM. It explains the roles of backend and frontend in web applications, the structure of Django projects and apps, and the purpose of various files like models.py and views.py. Additionally, it emphasizes the importance of understanding the relationship between URLs, views, and models in the context of web development.

Uploaded by

Birendra Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views45 pages

Djangotutorial (AutoRecovered)

The document outlines the basics of Django, a high-level Python web framework used for building dynamic websites, detailing its architecture, components, and features such as the admin panel and ORM. It explains the roles of backend and frontend in web applications, the structure of Django projects and apps, and the purpose of various files like models.py and views.py. Additionally, it emphasizes the importance of understanding the relationship between URLs, views, and models in the context of web development.

Uploaded by

Birendra Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PHASE 2-Django Basics

Step 1: What is Backend?


Step 2: Install Django
Step 3: Create Your First Project
Step 4: Create Your First App
Step 5: Create Your First View
Step 6: Connect URL

[Link] is backened –
Real Life Example : Instagram Login

Let’s understand deeply.

You enter:

Username: ankita123
Password: 12345

Now what happens?

Step 1: Frontend sends data to backend

Browser sends this data to server.

Step 2: Backend checks database

Backend asks:

"Is this username and password correct?"

It checks in database.

Step 3: Backend response

If correct → Login success


If wrong → Show error

Then frontend displays result.

What is Database?
Database = Storage Room 📦
Example:
 Users
 Passwords
 Posts
 Comments
 Followers
All stored in database.
Backend talks to database.
Frontend NEVER talks directly to database.

🔥 Complete Flow Diagram


You (User)

Frontend (HTML/CSS/JS)

Backend (Django)

Database (SQLite / MySQL)

Response sent back

Frontend shows result

🏗 So What Is Django?
Django is:
 A Backend Framework
 Written in Python
 Used to build dynamic websites
It handles:
 Login system
 Data storage
 Security
 Admin panel
 API creation
🧩 Static Website vs Dynamic Website
🔹 Static Website
Example:
 Simple portfolio page
Content does NOT change automatically.

🔹 Dynamic Website
Example:
 Instagram
 Amazon
 Facebook
Content changes based on:
 User
 Data
 Database
Django builds dynamic websites.

🧠 Why Backend Is Powerful?


Because backend controls:
 Who can login
 Who can access data
 How data is stored
 What logic runs
 Security rules
Frontend is just design.
Backend is brain.

🔥 Framework

🧠 Technical Definition
A framework is:
A pre-written structured collection of tools, rules, and libraries that helps you build
applications faster.

🐍 Now What is Django?


Django is a high-level Python web framework used to build secure and scalable web
applications.
It follows:
MVT Architecture
(Model – View – Template)

🧩 Django Architecture (With Real Life Example)


🏫 School Management System Example
Let’s say we are building:
Student Record Website

🟢 Model = Database Structure


It defines:
 Student Name
 Roll Number
 Marks
 Class
Model tells Django:
"This is how data should be stored."

🟢 View = Logic Controller


Example:
 Calculate total marks
 Check pass/fail
 Fetch student data
View processes request.
🟢 Template = Frontend (HTML)
Shows:
 Student details
 Result page
 Marks sheet

Daily life example:-


You (Student)

URL (Reception)

View (Office Employee)

Model (Record Format)

Database (Record Room)

Data comes back

Template (Printed Result)

You see page
🧠 Why This Structure Is Important?
Because:
 URL decides where to send
 View decides what to do
 Model decides how to store
 Database stores permanently
 Template decides how to show
Each has one clear responsibility.
💡 Very Important Difference
URL does NOT do logic.
View does logic.
URL is like:
📍 Direction board
View is like:
👩‍💼 Worker doing actual job
🧠 Ultra Simple Version
If website is a mall:
 URL = Shop number
 View = Shopkeeper
 Product given = Response

 🚨 Common Confusion
 Many students think:
 URL shows page
 ❌ Wrong.
 URL only matches path and calls view.
 View decides what to show.

[Link]

path('marks/', views.show_marks)
[Link]
def show_marks(request):
return HttpResponse("Marks Page")
What happens when these two file runs :-
 Browser sends request
 Django checks [Link]
 URL matches 'marks/'
 Django runs show_marks()
 show_marks() returns "Marks Page"
 Django sends it back to browser
 Browser displays it

💎 Powerful Features of Django


Now important part 👇
1️⃣ Admin Panel (Auto Generated)
Django automatically gives:
/admin
You can:
 Add users
 Delete records
 Manage data
 Edit database
Without writing extra code.
This is VERY powerful.

2️⃣ ORM (Object Relational Mapping)


Normally SQL looks like:
SELECT * FROM students;
In Django you write:
[Link]()
No need to write raw SQL.
Easy + Clean + Secure.

3️⃣ Built-in Authentication System


Django already has:
 Login
 Logout
 Register
 Password reset
 User model
You don’t need to build from scratch.

4️⃣ Security
Django protects against:
 SQL Injection
 CSRF Attacks
 XSS Attacks
 Clickjacking
Very secure by default.

5️⃣ Scalability
Websites like:
 Instagram
 Pinterest
Use Django.
That means it can handle millions of users.

6️⃣ Fast Development


Django slogan:
“The web framework for perfectionists with deadlines.”
You build apps very quickly.
2. Install Django
Questions we are covering under this:-
Difference between Project and App :-
1️⃣ First Understand: Project vs App
Imagine you are building a College Management Website.
Your project = entire website
Example project:
CollegePortal
Inside the website you need different features:
 Student management
 Teacher management
 Attendance system
 Exams system
Each feature becomes a Django app.
So the structure becomes:
CollegePortal (project)
|
|---- students (app)
|---- teachers (app)
|---- attendance (app)
So when you run:
python [Link] startapp students
Django automatically creates an app called students.

2️⃣ Are These Files Created Automatically?


Yes ✅
When you run:
python [Link] startapp students
Django automatically creates all these files for you.
You do NOT create them manually.
The structure becomes:
students/

├── migrations/
├── __init__.py
├── [Link]
├── [Link]
├── [Link]
├── [Link]
└── [Link]
Now let’s understand what each file does.

3️⃣ [Link] (Database Structure)


This file defines database tables.
Example: Suppose we want to store student records.
Name
Roll Number
Marks
In [Link] we write:
from [Link] import models

class Student([Link]):
name = [Link](max_length=100)
roll = [Link]()
marks = [Link]()
Django converts this into a database table automatically.
Database table:

id name roll marks

1 Rahul 21 85

So:
[Link] = database structure

4️⃣ [Link] (Backend Logic)


This file contains Python functions that handle requests.
Example:
User opens website page:
[Link]/students
Django runs a function inside [Link].
Example:
from [Link] import HttpResponse

def show_students(request):
return HttpResponse("List of students")
So:
[Link] = backend logic

5️⃣ [Link] (Admin Panel Control)


Django has a built-in admin dashboard.
Example:
localhost:8000/admin
From here you can:
 add students
 edit students
 delete students
But first you must register the model.
Example:
from [Link] import admin
from .models import Student

[Link](Student)
Now you can manage students from admin panel.
So:
[Link] connects database models to admin panel

6️⃣ migrations Folder


This folder tracks database changes.
Example:
Suppose first model was:
name
roll
Later you add:
marks
Django creates a migration file to update the database.
Command:
python [Link] makemigrations
Django generates migration files like:
0001_initial.py
0002_add_marks.py
So:
migrations = history of database changes

7️⃣ [Link]
This file contains configuration of the app.
Example:
[Link]
Django uses this internally to register the app.
Usually you don't change this.

8️⃣ [Link]
This file is used for testing your application.
Example: checking if functions work correctly.
Example test:
from [Link] import TestCase
Most beginners do not use it initially.

9️⃣ [Link]
This file tells Python:
"This folder is a Python package."
Usually it is empty.
You normally never modify it.

🔟 Final Simple Table

File Purpose

[Link] database tables

[Link] backend logic

[Link] connect models to admin panel

migrations database change history


File Purpose

[Link] app configuration

[Link] testing

[Link] makes folder a Python package

1️⃣1️⃣ Very Important Concept


The app does not automatically start working after creation.
You must register it in [Link].
Open:
[Link]
Find:
INSTALLED_APPS
Add:
'students'
Example:
INSTALLED_APPS = [
'[Link]',
'[Link]',
'students',
]
Now Django knows:
This app is part of the project.

🧠 The One Line Summary


Project = Whole Website
App = One Feature of Website
Models = Database
Views = Backend Logic
Both at same time.
Folder structure of Django:-
Step 3: Create Your First Project
Understanding each file :-
Project folder analysis:-
App folder:-
Difference between
httpsresponse ,render,template

You might also like