Django
How to Install Django and Create a Project
-
Ensure Python and pip are installed. If not installed Download Python, or install PyEnv-Win.
SHELL
Copied!
python --version pip --version
-
In you project folder activate a virtual environment (recommended) Virtual Environment (venv)
-
Install Django
SHELL
Copied!
pip install django
-
Can verify installation by checking the current version
SHELL
Copied!
django-admin --version
-
It's a good habit (though optional) to save your installed packages.
SHELL
Copied!
pip freeze > requirements.txt
-
After installing Django you can create a project. Replace 'myproject' with whatever name you like.
SHELL
Copied!
django-admin startproject myproject .
-
Use the cd command to change your current location in the terminal to your project folder. This lets you work inside that folder where your Django project files are. Replace 'myproject' with the name that you used.
SHELL
Copied!
cd myproject
-
Once you are inside the folder containing manage.py (usually the project root), you can start the Django development server.
SHELL
Copied!
python manage.py runserver
Creating a new App
-
This command creates a new Django app named myapp within your project (you can name it whatever you like).
SHELL
Copied!
python manage.py startapp myapp
-
Add the new app to installed apps within the settings.py file
SHELL
Copied!
python manage.py startapp myapp
Django Database Management and Migrations
-
Detect model changes and prepare migration scripts.
SHELL
Copied!
python manage.py makemigrations
-
Use this to generate migration files only for the app you are working on, rather than scanning the entire project.
SHELL
Copied!
python manage.py makemigrations myapp
-
Check what migrations will be created (Optional) useful for debugging.
SHELL
Copied!
python manage.py makemigrations --dry-run
-
Update the database schema based on migration files.
SHELL
Copied!
python manage.py migrate
-
This command runs the database migrations only for the specified app (myapp), instead of applying migrations for all installed apps in the project.
SHELL
Copied!
python manage.py migrate myapp
-
View which migrations have been applied or are pending.
SHELL
Copied!
python manage.py showmigrations
Debugging Migrations
-
This command rolls back all migrations.
SHELL
Copied!
python manage.py migrate zero
-
This command rolls back all migrations for the specified app (myapp), returning it to its initial state.
SHELL
Copied!
python manage.py migrate myapp zero
-
Deletes all data from the database across all apps, while keeping the database schema and applied migrations intact.
SHELL
Copied!
python manage.py flush
Create a Superuser
-
Once you've created an app, you can create a superuser. An admin account with full access to the Django admin panel. This account includes a username, email, and password, which you'll use to log in.
SHELL
Copied!
python manage.py createsuperuser
Quick Links | |
---|---|
Django Cheat Sheet (Code Institute) | |
Django Documentation | DOCS |
Top ↑ |