One of the best things about Django is the ability to plug in functionality written by others into your project.
Coding your own apps instead of using what’s already available will result in waste of time and energy, and prevent you from launching your app quickly.
In this post, I will tell you about 5 essential apps that every project should have so that you can get ready for production in no time.
Following the third pillar of the 12 factor app methodology, django-environ allows you to easily fetch environment variables and cast them to Python types.
Configuring your app from environment variables will make your deployment process a breeze.
You can use
getenv() from Python’s built in os module to get environment variables but django-environ is better because it:
- Allows you to read variables from a file.
- Provides a few shortcuts like database url.
- Has methods to allow you to cast to Python types.
For example, a common enviroment variable is for the
In your environment, you can set
Then, in your settings.py,
ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS")
Wihout it, you’d have to write your own string unpacking logic to build a list.
I encourage you to check this package out and read the docs to get a full picture.
When working on large Django projects, how can you find out what urls have been set across the entire project?
You could look into the urls module of each app manually and scan them line by line.
Or, you could type
python manage.py show_urls.
This is one of the little things django-extensions add to your project.
It’s an app that is installed in 100% of all projects I work on because of killer features like:
- Instead of the regular
runserver command, the
runserver_plus command provides you with an interactive debugger when errors are encountered during template rendering. Huge timer saver.
shell_plus is much better than built-in shell because it autoloads new apps and classes.
- AutoSlugField that allows you to have a slug field that auto populates itself upon model creation.
It’s an absolute beast of an app and you must check it out.
Displaying images on a web page is one of the most common functionalities in web applications.
Instead of displaying full sized images, which are usually large in size and uncompressed, you should consider resizing them according to the user’s viewport as well as compress them to save bandwidth.
Using a thumbnail app like sorl-thumbnail is the perfect way to achieve this.
What I love about sorl-thumbnail:
- It’s ability to use a fast cache.
- Ability to use graphicsmagick.
These features allow you to generate high quality thumbnails fast so that you can achieve a good user experience.
Another great feature is the
ImageField which auto deletes images when the model instance is deleted.
This is another app that I always have in my
Whitenoise allows your web app to efficiently serve static files regardless of where it’s hosted.
Without Whitenoise, you’d use something like Nginx or Amazon S3 if you’re hosting on Heroku.
All the complexity of setting up your web server for efficient caching, compression, and cors is taken care of by this package.
Without this package, you’d have to waste a whole bunch of time setting up proper static file serving.
Many web apps need to run code in a separate thread in order to avoid blocking the main one.
If you block the main thread with code that takes 5 minutes to return, your users will get angry.
Instead, use django-q to delegate tasks to a separate thread.
Many projects use Celery but django-q is what I recommend because it’s possible to spend an entire week integrating Celery into your Django project.
Compare this to django-q which takes about 2 minutes to configure.
For simpler projects, a cron job or systemd timer might even be better.
django-q provides a nice admin interface that allows you to view statistics about your tasks, as well as schedule tasks just like Linux systemd timers.
Django’s huge ecosystem
There’s a package for pretty much anything you can imagine out there.
Make sure you find packages before starting any project in order to avoid wasting time coding and maintaining something that already existed.