Table of Contents
Using packages makes your work easier and makes the development of a project much faster. In this article, I present the packages that I find useful and that I use in my projects.
These packages will simplify your life with Django. I invite you to share in the comments your favorite Django packages that you use and why.
Pillow
Any time you work with ImageFields you have to install an image library, e.g Pillow. Pillow is the “friendly” PIL, while PIL is the Python Imaging.
How to install Pillow?
PIL and Pillow currently cannot co-exist in the same environment. If you want to use Pillow, you have to remove PIL first.
You can install Pillow with pip:
pip install Pillow
Django Cleanup
in Django unfortunately, after deleting an image it remains stored in the media directory.
If in your project you work a lot with images, files, in time the space occupied on the disk increases, which is an inconvenience.
You can modify to physically delete the files from the code or use Django Cleanup package.
First, install django cleanup package:
pip install django-cleanup
Then, add django_cleanup app in your settings.py INSTALLED_APPS:
INSTALLED_APPS = ( ... 'django_cleanup', # should go after your apps )
The django-cleanup app automatically deletes files for FileField, ImageField and subclasses.
When a FileField’s value is changed and the model is saved, the old file is deleted. When a model that has a FileField is deleted, the file is also deleted.
psycopg2 – Python-PostgreSQL Database Adapter
Psycopg2 is the most popular PostgreSQL database adapter for the Python programming language.
How to install psycopg2?
pip install psycopg2
psycopg2-binary
You can also obtain a stand-alone package, not requiring a compiler or external libraries, by installing the psycopg2-binary package from PyPI:
pip install psycopg2-binary
The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources.
pip install psycopg2-binary
!!When I deployed a Django app on shared hosting with a postreSQL database, I could not install psycopg2, but I worked with psycopg2-binary.
Using font-awesome in Python Django application
django-fontawesome is a Django app that provides a couple of Fontawesome related utilities, namely:
– an IconField to associate Fontawesome icons with model instances
– templatetags to render Fontawesome icons
1. Install django-static-fontawesome library
pip install django-fontawesome
2. Add ‘fontawesome’ to your installed apps setting
INSTALLED_APPS = (
…
‘fontawesome’,
)
django-file-resubmit
In Django project you have forms with FileField, ImageField. Everything works great, but when ValidationError is raised, you have to reselect all files and images again. It is kind of annoying. django-file-resubmit solves this problem. It works with FileField, ImageField and sorl.thumbnail.ImageField.
How Django file resubmit work?
Here are special widgets for FileField and ImageField. When you submit files, every widget saves its file in cache. And when ValidationError is raised, widgets restore files from cache. Read more here about Django file resubmit
How to install Django resubmit?
pip install django-file-resubmit
Creating Sitemaps in Django.
XML Sitemap is the most preferred way of creating sitemaps. Django provides django.contrib.sitemaps a sitemap framework, which automates the process of creating sitemaps.
Use django.contrib.sitemaps to create sitemap for Your site
Read here about How to create a sitemap in Django.
Django Simple Cookie Consent notice
I tested the packages below for cookie consent:
Django cookie consent
Django cookie consent is a reusable application for managing various cookies and visitors consent for their use in Django project. Provides support for both opt-in and opt-out cookie consent schemes.
Cookies and cookie groups are stored in models for easy management through Django admin interface.
Django cookie law
Django cookie law will display a dismissable banner, making your users aware of cookies being used.
Django Simple Cookie Consent
Django Simple Cookie Consent is a simple Django app to display a custom cookie message. By far, it was the easiest to set up and I used it in my Django applications.
Each visitor to the site will get a popup to say that the site uses cookies.
How to install Django Simple Cookie Consent?
1. pip install django-simple-cookie-consent
2. Add “django_simple_cookie_consent” to your INSTALLED_APPS setting
3. Add “{% load django_simple_cookie_consent_tags %}” to your base template then place “{% display_cookie_consent %}” somewhere in the head tag like this:
…other loads
{% load django_simple_cookie_consent_tags %}
…other links/scripts
{% display_cookie_consent %}
4. Run ‘python manage.py migrate’ to create the CookieConsentSettings model.
5. Visit admin panel and create a settings instance (you’ll need the Admin app enabled)
Django admin honeypot
django-admin-honeypot is a fake Django admin login screen to log and notify admins of attempted unauthorized access. This app was inspired by discussion in and around Paul McMillan’s security talk at DjangoCon 2011.
How to install Django admin honeypot?
1. Install django-admin-honeypot from PyPI:
pip install django-admin-honeypot
2. Add admin_honeypot to INSTALLED_APPS
3. Update your urls.py:
urlpatterns = patterns('' path('admin/', include('admin_honeypot.urls', namespace='admin_honeypot')), path('secret/', admin.site.urls), )
4. Run python manage.py migrate
Django Rest Framework
Django REST framework is a powerful tool for building REST APIs.
REST stands for “representational state transfer” and API stands for application programming interface.
Django Rest Framework is very popular, makes it easy to use your Django Server as a REST API.
How to install Django Rest Framework?
pip install djangorestframework
2. Add ‘rest_framework’ to your INSTALLED_APPS setting.
INSTALLED_APPS = [ ... 'rest_framework', ]
3. Any global settings for a REST framework API are kept in a single configuration dictionary named REST_FRAMEWORK. Add the following to your settings.py module:
REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.AllowAny', ] }
4. If you’re intending to use the browsable API you’ll probably also want to add the REST framework’s login and logout views. Add the following to your root urls.py file.
urlpatterns = [ ... path('api-auth/', include('rest_framework.urls')) ]
Django cors headers
Django cors headers adds Cross-Origin Resource Sharing (CORS) headers to responses. This allows in-browser requests to your Django application from other origins.
How to install Django cors headers?
pip install django-cors-headers
2. Add it to your installed apps:
INSTALLED_APPS = [ ... 'corsheaders', ... ]
3. Add a middleware class to listen in on responses:
MIDDLEWARE = [ ... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ]
Careful CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django’s CommonMiddleware or Whitenoise’s WhiteNoiseMiddleware. If it is not before, it will not be able to add the CORS headers to these responses.
4. Configure the middleware’s behavior in your Django settings. You must set at least one of three following settings:
CORS_ALLOWED_ORIGINS
CORS_ALLOWED_ORIGIN_REGEXES
CORS_ALLOW_ALL_ORIGINS
CORS_ALLOWED_ORIGINS = [ "https://example.com", "https://sub.example.com", "http://localhost:8080", "http://127.0.0.1:9000" ]
Hello there!
I hope you find this post useful!I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.
If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!