Python libraries you should be using along with Django

Devashish Patil
Feb 8 · 3 min read
Designed by: Author

When it comes to backend development in Python, Django is one of the best frameworks you can find. It enables you to do fast development because of the number of features it has and its built-in ORM. Moreover, you can add new functionalities with the help of a wide variety of libraries specifically designed for Django. Here, we are going to discuss some of the important and popular ones.

Django Rest Framework (djangorestframework)
Django Rest Framework or DRF is inarguably the most popular plugin if you want to build APIs on top of your Django models. If you haven’t used it yet, you should give it a try right now.

Features:

  • It supports serialization on ORM and non-ORM data sources.
  • Model Views/Viewsets got GET, POST, PUT, PATCH, and DELETE requests. This enables fast development.
  • Highly customizable.
  • Well maintained and documented.
# Install using pip
pip install djangorestframework
# In your settings.py
INSTALLED_APPS = [
...
'rest_framework',
]
# In urls.py
urlpatterns = [
...
path('api-auth/', include('rest_framework.urls'))
]

You can find a lot of examples on how to get started with DRF here

django-cors-headers
Imagine, you have already created your models, serializers, and views and done with all the migrations. You tried the APIs using Postman or the browser view of Django Rest Framework.

Everything is working fine, but as soon as you put the API endpoint in your Javascript application and try to run it, it throws a CORS error.

Unfortunately, Django doesn’t have anything in-built for this. django-cors-headers library addresses this issue and is really easy to add to your Django app. Here’s how you can do this:

Install using pip

pip install django-cors-headers

In your settings.py file, change the following configurations

# Add corsheaders to your installed apps 
INSTALLED_APPS = [
...
'corsheaders',
...
]
# Add these 2 lines in the existing middleware list
MIDDLEWARE = [
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
# Finally Define the origins you want to allow trafic from
CORS_ALLOWED_ORIGINS = [
"https://example.com",
"http://localhost:3000"
]

Django Filters (django-filter)
This library enables filtering on Querysets based on a model’s fields. It also supports nested filters if you have multiple models connected with primary keys. You can also order the data in the Queryset using this.

Another good thing about this is it integrates well with the Django rest framework, which comes in pretty handy.

# install using pip
pip install django-filter
# add to your installed apps
INSTALLED_APPS = [
...
'django_filters',
]

Here’s a glimpse of how to use filtersets. You can explore more in the official documentation.

from rest_framework import generics
from django_filters import rest_framework as filters
from myapp import Product
class ProductList(generics.ListAPIView):
queryset = Product.objects.all()
filter_backends = (filters.DjangoFilterBackend,)
filterset_fields = ('category', 'in_stock')
# Equivalent FilterSet:
class ProductFilter(filters.FilterSet):
class Meta:
model = Product
fields = ('category', 'in_stock')

Django Storages (django-storages)

So, you are done developing an app and everything is working well from your local machine. It’s time to deploy your application on the server(s).

In your settings.py file, you might have defined paths for static and media files such as the CSS and Javascript files, icons, and Images/files shown in the app.

You’ll want to store all those files in a location independent of the servers’ lifecycle, similar to a database so that you don’t lose this data if a server goes down. It also helps data consistency on your web application if you have multiple servers running in the backend.

django-storages comes to the rescue. It supports several cloud providers such as Amazon S3, Google Cloud Storage, and Azure Storage among others.

Full installation and usage information can be found in the official documentation here.

I hope this helped you in some way. If you made it through here, consider following for more bits like this.

Happy Coding… :)

CodeByte

Build | Test | Deploy

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store