Django is a popular Python web framework used for building complex web applications. Django is designed to be full-featured and scalable, and provides a set of tools and conventions for building web applications quickly and efficiently.
Here are some key features and examples of how to use Django:
## Model-View-Controller (MVC) Architecture
The core of Django is its Model-View-Controller (MVC) architecture, which separates the application logic into three components: models, views, and controllers.
* Models: Define the data schema and provide an interface for working with the database.
* Views: Handle user requests and generate responses, typically using HTML templates.
* Controllers: Handle the application logic and mediate between the models and views.
Here’s an example of how to define a simple Django model:
python from django.db import models class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=100) published_date = models.DateField() price = models.DecimalField(max_digits=5, decimal_places=2)
In this example, we define a Django model called `Book` that has four fields: `title`, `author`, `published_date`, and `price`. Django will automatically create a database table for this model and provide an API for creating, reading, updating, and deleting records.
## URL Routing
Django also provides a flexible URL routing system, which allows you to map URLs to views. Here’s an example of how to define a simple Django URL route:
python from django.urls import path from . import views urlpatterns = [ path("", views.index, name="index"), ]
In this example, we define a Django URL route that maps the root URL (`””`) to a view function called `index()`. When the user visits the root URL, Django will call the `index()` function and generate a response.
## Template System
Django also provides a powerful template system, which allows you to generate dynamic HTML content using variables, loops, and conditional statements. Here’s an example of how to use a Django template to generate a dynamic greeting:
html {% extends "base.html" %} {% block content %}Hello, {{ name }}!
{% endblock %}
In this example, we define a Django template that extends a base template (`base.html`) and defines a `content` block. The template uses a variable (`name`) to generate a dynamic greeting.
## Administration Site
Django also provides a built-in administration site, which allows you to manage the data in your application using a web-based interface. Here’s an example of how to register a Django model with the administration site:
python from django.contrib import admin from .models import Book admin.site.register(Book)
In this example, we register the `Book` model with the Django administration site. When we run the Django development server and visit the `/admin` URL, we can log in to the administration site and view, create, update, and delete `Book` objects.
Overall, Django is a powerful and flexible web framework for building complex web applications in Python, and is widely used in a variety of fields, including e-commerce, social media, and content management systems.