Essential Django Utilities to Enhance Your Python Projects
Written on
Django Essentials
Django is well-known as one of the leading full-stack frameworks available today. It boasts a robust community that includes developers, maintainers, and users ranging from solo entrepreneurs to major corporations.
In this article, I will outline several open-source tools, libraries, and resources I frequently use to enhance my Django development experience.
Introduction to Useful Tools
1. django-authuser
How often have you initiated a new Django project only to spend unnecessary time setting up user authentication models? I certainly have. django-authuser is an open-source repository that offers a reusable custom user model for your Django projects, saving you the hassle of creating one from scratch.
To quickly integrate it into your project using the command line, follow these steps:
- Download the zip file from the GitHub repository
- Unzip the package
- Rename the unzipped folder to "auth" and include it in your project as an app
- Delete the zip file
Here are the commands for your reference:
unzip django-authuser.zip
mv django-authuser-main auth
rm -r django-authuser.zip
2. gibo
Another library that can save you valuable time is gibo, which eliminates the need to manually create .gitignore files. With gibo, you can easily access all the .gitignore templates from GitHub directly from your command line.
Whether you’re working with a framework or a programming language, gibo ensures your .gitignore file is ready in moments instead of minutes. Installation is simple via Homebrew for Mac and Linux, or Git/Scoop for Windows.
Here’s how I implement it in my Django/Python projects:
gibo dump Python macOS VisualStudioCode VirtualEnv >> .gitignore
3. djhtml
DjHTML is an automatic template indenter designed to work with mixed HTML, CSS, and JavaScript templates that include Django template tags. It functions similarly to other code formatting tools like Black and works well with pre-commit.
To install it, use:
pip install djhtml
Then, set it up with pre-commit:
$ pip install pre-commit
$ pre-commit install
Your pre-commit-config.yaml should look something like this (assuming you are using Black as well):
repos:
rev: v1.5.2
hooks:
id: djhtml
files: .*/templates/.*.html$
rev: 22.12.0
hooks:
- id: black
For a comprehensive guide to pre-commit tools, check out my article on Python code formatting: The Easy Python Auto-code Formatting Guide.
4. django-browser-reload
Do you often find yourself reloading the browser every time you make adjustments in your Django application? I used to feel the same way until I discovered django-browser-reload. This open-source tool automatically reloads your browser whenever it detects changes in your application.
It's a development dependency, meaning it functions only when DEBUG = True in your settings.py. To install and implement it:
pip install django-browser-reload
Add it to your installed apps:
INSTALLED_APPS = [
...,
"django_browser_reload",
...,
]
Incorporate the app URLs into your root URL configuration:
from django.urls import include, path
urlpatterns = [
...,
path("__reload__/", include("django_browser_reload.urls")),
]
Lastly, add the middleware:
MIDDLEWARE = [
# ...
"django_browser_reload.middleware.BrowserReloadMiddleware",
# ...
]
5. django-tailwind
django-tailwind offers a streamlined approach to integrating the Tailwind CSS utility framework with your Django project, without the complications of using NPM.
To install it:
pip install django-tailwind
Add it to your settings.py:
INSTALLED_APPS = [
...
"tailwind",
]
Run the following command to automatically create a Django app named theme in your root directory:
python manage.py tailwind init
Then, update your settings.py with:
TAILWIND_APP_NAME = "theme"
INTERNAL_IPS = [
"127.0.0.1",
]
Finally, install the theme to get started:
python manage.py tailwind install
6. crispy-tailwind
Similar to django-crispy-forms, crispy-tailwind facilitates the automatic generation of styled Tailwind form components within your Django applications.
To install it:
pip install crispy-tailwind
In your settings.py, add:
INSTALLED_APPS = [
...
"crispy_forms",
"crispy_tailwind",
]
CRISPY_ALLOWED_TEMPLATE_PACKS = "tailwind"
CRISPY_TEMPLATE_PACK = "tailwind"
Final Thoughts
Thank you for taking the time to read this concise guide on some incredibly useful Django tools that I've come across in recent months. If you have any recommendations, please share them in the comments; I’d love to learn what I might be missing!
This article is part of my programming newsletter, Codecast. Subscribe for insights on Backend Development, System Design, and DevOps.
The first video, "Styling a Django app and deploying it with Heroku: Python Crash Course - Episode 20," provides a step-by-step guide on enhancing your Django application’s aesthetic and deploying it efficiently.
The second video, "How To Fix Import Could Not Be Resolved From Source Pylance," offers solutions for resolving common import issues in your Python projects.