Django is a full server-side framework written in python providing batteries and low-level automation that makes it easier to build a better web app with less code. The major advantages of using Django include the administration interface where developers and administrators can add and edit the back-end content and pre[built authentication functionalities allowing users to safely securely create user profiles for your website.
Django
What Is Django?
Documentation
The hints, tips, tricks, and examples provided herein are nowhere near being all-encompassing, and are instead aimed to get a novice to a slightly-more-competent-novice level. There are far more encompassing guides available out there but a must would be the Django professional documentation.
MVT
At the heart of Django lies the MVT architecture. This setup promotes code reusability, separation of concerns, and a structured way to handle data and user interactions. By keeping these layers distinct, it prevents the mixing of SQL, Python, and front-end code in one file, keeping the code looking clean but letting a team of developers of differing disciplines to collaborate effectively.
The Model is the foundation of the MVT architecture, responsible for managing data and interacting with the database. Models define the structure of your data using Django's ORM, allowing developers to work with objects Pythonically instead of using raw SQL. This abstraction not only simplifies database operations but also enhances code reusability and maintainability.
The View serves as the intermediary layer, acting as the controller in the traditional MVC architecture. It processes incoming requests from users, applies necessary logic, and interacts with the model to fetch or modify data. Views then prepare this data and pass it to templates for rendering in the form of context. This is where most of your application's logic resides; validating user input, handling form submissions, and determining what information is displayed to the user. For example, a view might filter products based on a user's search query or display a list of results in reverse price order.
The Template the presentation layer, responsible for rendering dynamic content and defining how data is displayed to the user. Templates use Django's Template Language or alternatives like Jinja2 to create visually appealing and user-friendly pages. They receive context data from views and use tags, filters, and variables to render this data dynamically. Templates are strictly focused on the presentation and avoid back-end logic, ensuring a clean separation from models and views.
Common Commands
Here is a selection of common commands used when building projects in Django to act as a reference. These will all be explored fully in subsequent sections.
| Code | Use |
|---|---|
python -m venv venv
|
Create a virtual environment |
.\venv\Scripts\activate
|
Activate the virtual environment |
deactivate
|
Deactivate the virtual environment |
pip3 install django
|
Install Django |
django-admin startproject project_name .
|
Start a project named project_name |
pip3 freeze --local > requirements.txt
|
Freeze requirements to a file named requirements.txt |
python manage.py runserver
|
Run the project locally |
python manage.py showmigrationspython manage.py makemigrations --dry-runpython manage.py makemigrationspython manage.py migrate --planpython manage.py migrate
|
Migration flow |
python manage.py createsuperuser
|
Create a superuser |
python manage.py startapp app_name
|
Create an app named app_name |