Django App Overview
In Django, an app is a self-contained module of code within a project that is responsible for a specific feature or functionality. Django encourages breaking down a project into smaller, manageable apps, each with its own models, views, templates, and URLs. This modular approach makes it easier to maintain, test, and reuse code.
Key Characteristics of a Django App:
- Self-Contained Functionality: Each app is designed to handle one specific piece of functionality. For example, in an e-commerce site, you might have separate apps for products, orders, and customers.
- Reusable: Apps can be reused in multiple projects. Once you create an app, you can plug it into other Django projects with minimal changes.
- Independent Components: Each app typically includes its own models, views, templates, URLs, and other files necessary to perform its tasks.
Creating a Django App
To create an app within a Django project, use the following command:
python manage.py startapp appname
This command generates a folder structure with essential files for the app:
- models.py: Define database models for storing data.
- views.py: Write the logic for handling requests and responses.
- urls.py: Map URL patterns to views.
- admin.py: Configure models for the Django admin interface.
- apps.py: Register the app with the project.
- migrations/: Store database migrations for the app.
Registering an App
After creating an app, you need to add it to the INSTALLED_APPS list in the project’s settings.py file to make Django aware of it:
INSTALLED_APPS = [
...
'appname', # Add your app here
]
Django Project Creation
To start a new Django project, use the following command:
django-admin startproject calc_pro
Creating a Templates Folder
To handle HTML files, create a templates folder within your app. This folder will store HTML files that Django can render in response to requests.
Important Concepts
- HttpResponse: A function used to generate a text response in Django.
- View: A function that handles HTTP requests and responses, including methods like
get(),post(),put(), anddelete().
Django Template Response
- render: A function used to retrieve an HTML page as a response in Django.
Django HTML Form with CSRF Protection
In Django, using method="post" in a form specifies that the form data should be submitted to the server via a POST request, which is typically used for actions that modify data (like adding, updating, or deleting data). By default, Django requires CSRF (Cross-Site Request Forgery) protection for any form that uses POST requests, to help prevent malicious websites from submitting unauthorized requests on behalf of authenticated users.
CSRF Token ({% csrf_token %})
The {% csrf_token %} template tag generates a unique token that Django includes in the form. This token is checked when the form is submitted to ensure the request is legitimate. Including {% csrf_token %} in your form is essential for security when using POST requests.
Example HTML Form
Below is an example HTML form (add.html) that uses POST and includes CSRF protection:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Addition</title>
</head>
<body>
<h1>Addition</h1>
<form action="{% url 'add' %}" method="post">
{% csrf_token %}
<label for="first_number">Enter First Number:</label>
<input type="number" id="first_number" name="first_number" required><br><br>
<label for="second_number">Enter Second Number:</label>
<input type="number" id="second_number" name="second_number" required><br><br>
<button type="submit">Submit</button>
</form>
{% if answer is not None %}
<h2>Answer: {{ answer }}</h2>
{% endif %}
</body>
</html>
Explanation of Key Elements
method="post": Specifies that the form will use the POST method, meaning data is sent securely to the server.{% csrf_token %}: Generates a token that Django checks upon form submission. If the token is missing or incorrect, Django will raise a CSRF error. Forbidden (403) CSRF verification failed. Request aborted.- Conditionally Displayed Answer: After form submission, if an
answeris available, it will be displayed asAnswer: {{ answer }}.
This document summarizes the basics of setting up and managing Django apps and projects.