Django commands and packages, along with their installation instructions and common usage
Django commands and packages, along with their installation instructions and common usage
Initial Setup
Installing Django
- Upgrade pip:
py -m pip install --upgrade pip
- Create a Virtual Environment:
Windows:
py -m venv my_env my_env\Scripts\activate.bat
Linux/Mac :
py -m venv my_env source my_env/bin/activate
- Set Execution Policy for PowerShell (for VS Code):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
- Activate Virtual Environment in PowerShell (for VS Code):
.\env\Scripts\activate.ps1
Installing Django
- Installing Django
Install Django:
- Verify Django Installation:
django-admin --version
Creating and Running a Django Project
- Start a New Django Project:
django-admin startproject my_project
- Navigate to Your Project Directory:
cd my_project
- Run the Development Server:
Default port
python manage.py runserver
python manage.py runserver 7000
- Create a New Django App:
django-admin startapp employee
Managing Migrations
- Make Migrations:
python manage.py makemigrations
- Show Migrations:
python manage.py showmigrations
- SQLMigrate: To view the SQL for a migration.
python manage.py sqlmigrate appname migration_number
- Apply Migrations:
python manage.py migrate
Admin and Shell
- Create a Superuser:
python manage.py createsuperuser
- Open Django Shell:
To interact with your Django models and environment using an interactive Python shell:
python manage.py shell
Checking for Problems
- To check for any problems in your project (similar to a lint tool):
python manage.py check
- Flush the Database:
python manage.py flush
Dependency Management
- Freeze Installed Packages:
pip freeze > requirements.txt
- Install Packages from requirements.txt:
pip install -r requirements.txt
Installing Django Extensions
- Install Django Extensions:
pip install django-extensions
- Clean Python Bytecode Files:
python manage.py clean_pyc
- Clear Cache:
python manage.py clear_cache
- Collect Static Files:
python manage.py collectstatic
Additional Packages
- Install Whitenoise (for static file serving):
pip install whitenoise
- Install Django Widget Tweaks:
pip install django-widget-tweaks
- Install Django Crispy Forms:
pip install django-crispy-forms
- Install Crispy Bootstrap 5 Integration:
pip install django-crispy-forms crispy-bootstrap5
- Install MySQL Client:
pip install mysqlclient
- Install PyMySQL:
pip install pymysql
- Install Pillow (for image handling):
pip install pillow
Running Tests
- To run tests for your Django project:
python manage.py test
- To clear expired sessions from the database:
python manage.py clearsessions
- Change Password: To change a user's password
python manage.py changepassword username
Django Packages for Common Tasks
- Django REST Framework: For building RESTful APIs.
pip install djangorestframework
- Django Allauth: For authentication, registration, and account management.
pip install django-allauth
- Django Debug Toolbar: For debugging during development.
pip install django-debug-toolbar
- Django Channels: For WebSockets and asynchronous support.
pip install channels
- Django Celery: For background task processing.
pip install django-celery
- Django Guardian: For object-level permissions
pip install django-guardian
- Django Storages: For cloud storage backends like S3.
pip install django-storages
- Django Compressor: For compressing CSS and JavaScript.
pip install django-compressor
This list covers the most essential commands and packages for Django development.
Thank you for visiting and taking the time to read our blog post! We hope you found the guide on setting up a Django project helpful and informative. Your support means a lot to us. Feel free to explore more of our content, and don’t hesitate to reach out with any questions or feedback. Happy coding!