0% found this document useful (0 votes)
13 views5 pages

Django Framework Overview

Django is a high-level Python web framework that promotes rapid development and follows the Model-View-Template (MVT) architecture. The document outlines key features, installation steps, and a guide to creating a Django project and app, including setting up models, views, and templates. It concludes with instructions on running the server to display a dynamic product list from the database.
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)
13 views5 pages

Django Framework Overview

Django is a high-level Python web framework that promotes rapid development and follows the Model-View-Template (MVT) architecture. The document outlines key features, installation steps, and a guide to creating a Django project and app, including setting up models, views, and templates. It concludes with instructions on running the server to display a dynamic product list from the database.
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
You are on page 1/ 5

Django Framework Overview

1. Django Framework Overview

Django is a high-level Python web framework that encourages rapid development and clean,
pragmatic design. It follows the Model-View-Template (MVT) architectural pattern, which
separates data (model), user interface (template), and business logic (view).

 Key Features of Django:


o Object-Relational Mapping (ORM): Connects the database with Python
classes.
o Automatic Admin Interface: A powerful and customizable admin panel.
o URL Routing: Handles requests based on defined patterns.
o Scalability and Security: Django helps developers avoid common security
pitfalls.

2. Setting Up Django

Before starting, you'll need Python and Django installed on your system.

Steps to Set Up Django:

Here is a step-by-step guide to install Django and start working with it using Python on your
local machine (Windows/Linux/macOS). This includes installation, creating a Django
project, running the server, and writing Python programs using Django.

Step 1: Install Python

Check if Python is installed

Open your terminal/command prompt and type:

python --version

or

python3 --version

If not installed, download it from: https://www.python.org/downloads/

Ensure you check "Add Python to PATH" during installation.

Step 2: Install pip (Python Package Installer)

Usually, pip is installed with Python by default.

Check by:

pip --version
If not found, follow this guide: https://pip.pypa.io/en/stable/installation/

Step 3: Install Virtual Environment (Recommended)

A virtual environment keeps project dependencies isolated.

For Windows:

pip install virtualenv

Create a virtual environment:

virtualenv venv

Activate it:

 Windows:

venv\Scripts\activate

Step 4: Install Django

After activating the virtual environment:

pip install django

Verify installation:

django-admin --version

Step 5: Create a Django Project

django-admin startproject myproject


cd myproject

This creates the following structure:

myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py

Step 6: Run the Django Development Server


To start the server:

python manage.py runserver

Visit: http://127.0.0.1:8000
You’ll see the Django Welcome Page.

Step 7: Create a Django App

A Django project can contain multiple apps.

python manage.py startapp myapp

Add 'myapp' to INSTALLED_APPS in settings.py.

Step 8: Create a Model in myapp/models.py


from django.db import models

class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=5, decimal_places=2)
description = models.TextField()

def __str__(self):
return self.name

Step 9: Create and Apply Migrations

Tell Django to create database tables:

python manage.py makemigrations


python manage.py migrate

Step 10: Use the Django Shell to Test Python Programs

Run the shell:

python manage.py shell

Then you can write Python code:

from myapp.models import Product

# Create a product
Product.objects.create(name="Laptop", price=999.99, description="A good laptop")

# Retrieve products
products = Product.objects.all()
for p in products:
print(p.name, p.price)

Step 11: Create Views and Templates

In myapp/views.py:

from django.shortcuts import render


from .models import Product

def product_list(request):
products = Product.objects.all()
return render(request, 'product_list.html', {'products': products})

Create a folder: myapp/templates/

Inside product_list.html:

<!DOCTYPE html>
<html>
<head>
<title>Products</title>
</head>
<body>
<h1>Product List</h1>
<ul>
{% for product in products %}
<li>{{ product.name }} - ₹{{ product.price }}</li>
{% endfor %}
</ul>
</body>
</html>

In myproject/urls.py:

from django.contrib import admin


from django.urls import path
from myapp.views import product_list

urlpatterns = [
path('admin/', admin.site.urls),
path('', product_list, name='product_list'),
]

Step 12: Run the Server Again

python manage.py runserver


Go to: http://127.0.0.1:8000
You'll see your product list rendered dynamically from the database!

You might also like