WOLAITA SODO UNIVERSITY
SCHOOL OF INFORMATICS
DEPARTMENT OF: INFORMATION TECHNOLOGY.
COURSE TITLE: WEB PROGRAMMING.
Group Assignment:
BY:-
Name of Group Members ID No Email
1. DIBA NURE KAYO PGS/79873/14 dhibbaasabaa17@[Link]
2. JEMAL WOTICHA GOBENA PGS/79878/14 jemalilmasabaa@[Link]
3. MATUSAL KOCHARE ASHA PGS/79881/14
August /2024
Wolaita Sodo ,Ethiopia
Table of Contents
1. Introduction to Django ...................................................................................................................................... 3
1. What is Django? ................................................................................................................................................. 3
2. What Does Django Do? ...................................................................................................................................... 3
3. Django Architecture ........................................................................................................................................... 3
4. Why Django? ...................................................................................................................................................... 4
4. Advantages of Django ........................................................................................................................................... 5
5. Disadvantages of Django....................................................................................................................................... 5
6. Features of Django ............................................................................................................................................. 5
7. Advancements Over Other Development Tools ................................................................................................... 6
8. How to Install Django......................................................................................................................................... 6
9. Django Operators............................................................................................................................................... 9
10. Variables and Data Types in Django.................................................................................................................. 10
11. Django Types of Variables ................................................................................................................................. 10
12. Django Functions ............................................................................................................................................... 10
13. Django Loop Statements .................................................................................................................................... 10
14. Django Conditional Statements ........................................................................................................................ 11
15. Example: A Simple Django Project ..................................................................................................................... 12
16. Conclusion ......................................................................................................................................................... 13
1. Introduction to Django
1. What is Django?
Django is a high-level Python web framework that allows developers to build secure, scalable,
and maintainable web applications quickly and efficiently.
It follows the Model-View-Template (MVT) architectural pattern and emphasizes reusability,
less code, and rapid development.
2. What Does Django Do?
Django simplifies the web development process by providing built-in tools and libraries for
handling common tasks like:
User authentication
URL routing
Database interactions, and
Template rendering.
It allows developers to focus more on the unique aspects of their applications rather than
reinventing the wheel for every project.
3. Django Architecture
Django follows the MVT design pattern (Model View Template).
Model - The data you want to present, usually data from a database.
Django provides an abstraction layer (the “models”) for structuring and manipulating the data of your
Web application
View - Django has the concept of “views” to encapsulate the logic responsible for processing a user’s
request and for returning the response.
Template - A text file (like an HTML file) containing the layout of the web page, with logic on how to
display the data. The template layer provides a designer-friendly syntax for rendering the information to
be presented to the user.
Model
The model provides data from the database.
In Django, the data is delivered as an Object Relational Mapping (ORM), which is a technique designed
to make it easier to work with databases.
The most common way to extract data from a database is SQL. One problem with SQL is that you have
to have a pretty good understanding of the database structure to be able to work with it.
Django, with ORM, makes it easier to communicate with the database, without having to write complex
SQL statements. The models are usually located in a file called models. [Link].
Template
A template is a file where you describe how the result should be represented.
Templates are often .html files, with HTML code describing the layout of a web page, but it can also be
in other file formats to present other results, but we will concentrate on .html files.
Django uses standard HTML to describe the layout, but uses Django tags to add logic:
4. Why Django?
1. Rapid Development: Django enables developers to quickly build and deploy web applications
by automating many of the tedious aspects of development.
2. Security: Django comes with security features to protect against common web vulnerabilities,
such as SQL injection, cross-site scripting, and cross-site request forgery.
3. Scalability: Django is designed to scale, making it suitable for both small projects and large,
and high-traffic sites.
4. Community and Ecosystem: With a large and active community, Django offers extensive
documentation, third-party packages, and tutorials, making it easier for developers to find
solutions and extend functionality.
4. Advantages of Django
1. Batteries-Included: Django comes with a lot of built-in features, reducing the need for third-
party libraries.
2. ORM: The Object-Relational Mapping (ORM) allows developers to interact with databases
using Python code rather than SQL, speeding up development and making the code more
maintainable.
3. Admin Interface: Django automatically generates a customizable admin interface, saving time
on backend management.
4. Versatile: Django can be used for various applications, including content management systems,
social networks, and scientific computing platforms.
5. High-Level Framework: Django handles much of the complex, repetitive tasks of web
development, such as URL routing, form handling, and session management.
5. Disadvantages of Django
1. Monolithic: While Django is powerful, its "batteries-included" approach can sometimes be
overkill for smaller projects.
2. Learning Curve: Django's deep integration and convention over configuration can have a steep
learning curve for beginners.
3. Tight Coupling: While Django promotes reusability, it can also lead to tightly coupled
components, making it harder to customize or replace parts of the system without affecting
others.
6. Features of Django
1. Automatic Admin Interface: Django's admin interface is automatically generated and
customizable, allowing for easy content management.
2. ORM: Django's ORM lets you interact with your database using Python objects.
3. Templating System: Django's templating system enables the separation of design and logic,
promoting the DRY (Don't Repeat Yourself) principle.
4. Security Features: Django includes protections against many common web attacks by default.
5. URL Routing: Django's flexible URL routing system allows you to map URLs to views in a
straightforward manner.
6. Internationalization: Django supports translations and locale-specific formatting.
7. Middleware Support: Middleware is a lightweight plugin system for globally altering input or
output.
7. Advancements Over Other Development Tools
1. High-Level Abstraction: Django abstracts many complex web development tasks, allowing
developers to focus on writing business logic instead of handling low-level details.
2. Comprehensive Documentation: Django is well-documented, making it easier for developers to
learn and apply best practices.
3. Active Community: Django has a strong and active community that regularly contributes to the
framework, ensuring its continuous improvement and relevance.
4. Scalability: Django is suitable for both simple projects and large-scale applications, offering
flexibility that some other frameworks may lack.
8. How to Install Django
To install Django, you'll need Python and `pip`, Python's package manager, installed on your
system.
1. Install Python:
Download Python from the [official website]([Link]
Follow the instructions to install it.
2. Install Django:
Open your terminal or command prompt and run the following command:
pip install django
This will install Django and its dependencies.
3. Verify Installation:
After installation, you can verify Django is installed by running:
python -m django --version
This should output the installed Django version.
How to Install Django?
To Install Django in Linux and Mac is similar, here I am showing it in Windows for Linux and Mac just
open the terminal in place of the command prompt and go through the following commands.
Step 1: Install Pip
Open the command prompt and enter the following command-
Step 2: Set Virtual environment:
Setting up the virtual environment will allow you to edit the dependency which generally your system
wouldn’t allow. Follow these steps to set up a virtual environment-
Step 3: Create virtual environment in Django:
We should first go the directory where we want to create the virtual environment then we type the
following command to create virtual environment in djang
then we need to activate virtual environment in django
Step 4: Activate the virtual environment:
Run the activation script located in the bin directory within the virtual environment folder
Step 5: Install Django:
Install django by giving following command
Django Setup
Once Django is installed, we can start to create a new Django project.
Step 1: Start a new Django Project
Start a project by following command-
Step 2: Navigate to the Project Directory
Change directory to geeks_site
Step 3: Start the server
Start the server by typing following command in cmd-
Step 4: Verify Server Status
To check whether server is running or not go to web browser and enter [Link] as URL.
9. Django Operators
Django template system supports a limited set of operators, and Python operators can be used in views.
List of Operators:
Arithmetic Operators: `+`, `-`, `*`, `/`, `%`, `//`, `**`
Comparison Operators: `==`, `!=`, `>`, `<`, `>=`, `<=`
Logical Operators: `and`, `or`, `not`
Membership Operator: `in`, `not in`
Identity Operator: `is`, `is not`
10. Variables and Data Types in Django
Django, being a Python framework, uses Python's variables and data types.
Here’s a brief overview:
11. Django Types of Variables
Django uses Python's types of variables in views.
List of Variable Types:
String: `item = "Laptop"`
Integer: `count = 10`
Float: `price = 99.99`
Boolean: `is_available = True`
List (Array): `fruits = ["Apple", "Banana", "Cherry"]`
Dictionary: `product = {"name": "Laptop", "price": 999.99}`
Tuple: `coordinates = (10.5, 20.3)`
12. Django Functions
In Django, functions are defined in views to handle logic and return responses or templates.
List of Common Function Patterns:
Basic Function: `def function_name(request): ...`
Return Template Response: `return render(request, '[Link]', context)`
Return HTTP Response: `return HttpResponse("Hello, World!")`
Handling GET/POST: `if [Link] == 'POST': ...`
Example:
from [Link] import render
def greet_user(request, username):
context = {'username': username}
return render(request, '[Link]', context)
13. Django Loop Statements
Loops in Django are handled with `{% for %}` tags.
List of Loop Statements:
For Loop:`{% for item in items %} ... {% endfor %}`
For with Index: {% for item in items %} {{ [Link] }}: {{ item }} {% endfor %}`
For with Reverse Index: {% for item in items %} {{ [Link] }}: {{ item }} {%
endfor %}`
Empty For Loop: {% for item in items %} ... {% empty %} No items available {% endfor %}`
Example:
<ul>
{% for fruit in fruits %}
<li>{{ fruit }}</li>
{% empty %}
<li>No fruits available</li>
{% endfor %}
</ul>
14. Django Conditional Statements
Conditionals in Django are handled with `{% if %}` tags.
List of Conditional Statements:
If Statement: {% if condition %} ... {% endif %}`
If-Else Statement: {% if condition %} ... {% else %} ... {% endif %}`
Elif (Else If): {% if condition %} ... {% elif another_condition %} ... {% else %} ... {%
endif %}`
If with Logical Operators:** `{% if condition1 and condition2 %} ... {% endif %}`
If with Negation: {% if not condition %} ... {% endif %}`
Example:
{% if user.is_authenticated %}
<p>Welcome, {{ [Link] }}!</p>
{% else %}
<p>Please log in.</p>
{% endif %}
Example :
a = 10
b = 20
sum = a + b # Arithmetic operator
is_equal = (a == b) # Comparison operator
Example:
{% if a > b %}
<p>A is greater than B</p>
{% endif %}
Example:
product = {
"name": "Laptop",
"price": 999.99,
"is_available": True,
}
15. Example: A Simple Django Project
1. Creating a Django Project:
In your terminal, navigate to the directory where you want to create your project and run:
django-admin startproject myproject
This will create a new directory called `myproject` with the basic structure of a Django project.
2. Running the Development Server:
Navigate to your project directory:
cd myproject
Run the development server:
python [Link] runserver
Open your browser and go to `[Link]
You should see the Django welcome page, indicating that your project is up and running.
3. Creating a Django App:
Inside your project directory, run:
python [Link] startapp myapp
This creates a new app called `myapp` where you can start adding your application logic.
4. Creating a View:
Open `myapp/[Link]` and add a simple view:
from [Link] import HttpResponse
def hello_world(request):
return HttpResponse("Hello, World!")
5. Mapping the View to a URL:
Open `myapp/[Link]` and add a URL pattern:
from [Link] import path
from .views import hello_world
urlpatterns = [
path('', hello_world),
]
6. Including the App’s URL in the Project:
Open `myproject/[Link]` and include your app's URLs:
from [Link] import admin
from [Link] import path, include urlpatterns = [
path('admin/', [Link]),
path('', include('[Link]')),]
7. Running the Server Again:
Save all your files and run the server again:
Python [Link] run server
Visit `[Link] in your browser, and
You should see "Hello, World!" displayed on the page.
16. Conclusion
Django is a powerful and versatile web framework that provides many built-in tools to help developers
create robust web applications efficiently.
It abstracts many of the complexities of web development, allowing you to focus on building the
features that make your application unique.