Django Framework Overview
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).
2. Setting Up Django
Before starting, you'll need Python and Django installed on your system.
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.
python --version
or
python3 --version
Check by:
pip --version
If not found, follow this guide: https://pip.pypa.io/en/stable/installation/
For Windows:
virtualenv venv
Activate it:
Windows:
venv\Scripts\activate
Verify installation:
django-admin --version
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
Visit: http://127.0.0.1:8000
You’ll see the Django Welcome Page.
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
# 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)
In myapp/views.py:
def product_list(request):
products = Product.objects.all()
return render(request, 'product_list.html', {'products': products})
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:
urlpatterns = [
path('admin/', admin.site.urls),
path('', product_list, name='product_list'),
]