IWSD01-Django Overview
IWSD01-Django Overview
BCA-IV
SOUNotes
SOUNotes
Django Overview
What is Django?
Django is a high-level Python web framework used for building robust and
scalable web applications quickly. It encourages rapid development and
clean, maintainable design.
Key Features:
Built-in Tools: Django includes an admin interface, authentication system,
URL routing, database management, and more.
Security: It helps developers avoid common security pitfalls like SQL
injection, cross-site scripting, and cross-site request forgery.
Scalability: Django can handle high-traffic websites and large-scale
applications efficiently.
Use Cases:
Social media platforms, e-commerce sites, content management
systems (CMS), and APIs.
What is a Framework?
A framework is a collection of pre-written code libraries and tools that
provides a foundation to build and deploy applications efficiently.
Advantages of Frameworks:
Saves time by providing ready-made modules for common tasks like
user authentication, database interactions, and form handling.
Encourages best practices and standardization in development.
Examples: Django (Python), Laravel (PHP), Spring (Java), React (JavaScript).
Django Architecture
Page 1 of 9
Django follows the MVT (Model-View-Template) architecture, ensuring a
separation of concerns between business logic, data, and presentation layers.
Components:
1. Model: Represents the database structure. It is used to query, add, or
update data in the database.
1. Handles the data in the application.
2. It communicates with the database and stores or retrieves data.
3. Example: Storing user information, products, or blog posts.
2. View: Contains the business logic and interacts with the model to fetch
data and pass it to the template.
1. Manages the logic and processes user requests.
2. It fetches data from the Model and passes it to the Template.
3. Example: When a user requests a webpage, the View gets the
required data and sends it for display.
3. Template: Handles the presentation layer by rendering HTML pages
based on the data passed from the view.
1. Manages the presentation layer.
2. Defines how the data will look on the webpage using HTML and CSS.
3. Example: Displaying a user profile with their name, photo, and posts.
Page 2 of 9
4. Template: The data fetched by the view is passed to a template for rendering
as an HTML page.
5. Response: The template is sent back to the user as a response.
Installation in Virtualenv
Virtualenv creates an isolated Python environment to avoid dependency
conflicts between projects.
1. Install virtualenv:
virtualenv env_name
env_name\Scripts\activate
5. Verify Installation:
django-admin --version
Page 3 of 9
django-admin startproject project_name
Linking the App: Add the app name to the INSTALLED_APPS list in the project’s
settings.py file.
settings.py: configures the project
urls.py: maps URLs to views
manage.py: command-line utility for the project
Creating a Superuser
What is a Superuser? A superuser is an administrator account that has all
permissions, including access to the admin panel.
Enter the following details: Username, Email address, Password and start
server to access admin panel
Page 4 of 9
Application Programming Interface
An API is a set of rules and protocols that allow different software applications
to communicate with each other. It acts as an intermediary between two
applications, enabling data exchange.
Why Use APIs?
To integrate third-party services (e.g., payment gateways, social media).
To enable data sharing between systems.
To enhance modular and scalable application development.
Characteristics of API:
Communication Bridge: Connects two systems or applications to
exchange data.
Reusability: APIs can be reused across multiple applications and systems.
Abstraction: Hides complexity of back-end processes & provides a simple
interface to users.
Standardization: Follows standard methods like HTTP, JSON, XML, etc., to
make integration easier.
Examples of APIs:
Weather API to fetch real-time weather data.
Payment APIs like PayPal or Stripe for online transactions.
Types of APIs
Open APIs (Public APIs):
Available for public use.
Example: Twitter API for social media integration.
Internal APIs (Private APIs):
Used within an organization to integrate internal systems.
Example: APIs for a company’s HR software.
Partner APIs:
Shared with specific business partners under controlled access.
Example: APIs shared between payment providers and e-commerce
platforms.
Composite APIs:
Combine multiple API calls into a single request.
Example: A booking service API that retrieves flight and hotel details
simultaneously.
Page 5 of 9
REST (Representational State Transfer) is a lightweight architecture that
defines a set of constraints for creating APIs.
Key Features:
Stateless: Each request contains all the necessary information, with no
dependency on previous requests.
Client-Server Separation: The client and server operate independently.
Cacheable: Responses can be cached to improve performance.
Common HTTP Methods in REST APIs:
GET: Retrieve data (e.g., fetch a user’s profile).
POST: Create new data (e.g., add a new record).
PUT: Update existing data (e.g., modify user details).
DELETE: Remove data (e.g., delete a user account).
RESTful Architecture
Principles:
Uniform Interface: Use standard conventions for resources (e.g., /users for
accessing user data).
Stateless: Each request must contain all the information needed.
Cacheable: Responses should define whether they can be cached.
Layered System: The client should not know the server’s infrastructure.
Resource Based: Everything is treated as a resource, identified b URLs.
Client-Server Model: The client (frontend) and server (backend) are
separate, allowing scalability.
Resource Representation:
Resources (data) are represented as JSON or XML in REST APIs.
Request Library
Page 6 of 9
A Python library used to send HTTP/HTTPS requests to interact with APIs.
It simplifies the process of working with APIs, handling URL parameters,
headers, authentication, and file uploads while providing support for secure
connections with SSL.
Features:
Supports GET, POST, PUT, DELETE, and other HTTP methods.
Handles headers, parameters, authentication, and more.
import requests
response = requests.get("https://api.example.com/users")
print(response.json())
Assert Statements
A debugging tool used to test assumptions in the code. If the condition is
false, an AssertionError is raised.
If the condition evaluates to True , the program continues execution. If it
evaluates to False , an AssertionError is raised, optionally with a custom
error message.
It is commonly used in testing to validate assumptions and ensure that the
code behaves as expected.
Syntax: assert condition, "Optional error message"
x = 10
assert x > 5, "x should be greater than 5"
Testing
Testing verifies that a software application meets the specified requirements
and is free from defects
Types of Testing in the Context of APIs:
1. Unit Testing: Testing individual components like functions or endpoints.
2. Integration Testing: Ensuring different API endpoints work together.
3. Functional testing: Validates the application against user requirements
by simulating user behavior.
4. Security Testing: Ensuring the API is protected from vulnerabilities.
Example: Using Python’s unittest or pytest frameworks for automated API
testing.
Page 8 of 9
1. Create: Insert new data.
Resources
Official Django Documentation
Django Tutorial (Hindi)
Django Tutorial (English)
Page 9 of 9
SOUNotes
SOUNotes
SOUNotes