DEV Community

Cover image for Django Framework - Popular extensions, a short-list
Sm0ke
Sm0ke

Posted on • Edited on • Originally published at blog.appseed.us

Django Framework - Popular extensions, a short-list

Hello Coders,

This article presents a short-list with popular Django modules and apps that might help Python programmers to code faster new features by reusing some valuable work provided by open-source enthusiasts.


Thank you! Content provided by AppSeed - App Generator.


The list is built based on my personal experience and suggestions published by developers across relevant communities: Dev.to Pythoners / Django, Reddit Django and Python channels, and the classic Python Forum.


What is Django (web framework)

Short-Note for beginners - Django is an open-source web application framework written in Python. A framework means a collection of modules and helpers that make development easier. They are logically grouped together and allow you to create web applications by reusing stuff, instead of writing all from scratch.

Useful Django Resources:

  • Django - official website and docs
  • Django - related content provided by the (popular) Full-Stack-Python platform

The Short-List


In case you want to test quickly any extension presented here, by updating some open-source projects, here is a shortlist with Django free applications

Django Atlantis Dark - Open-Source Admin Panel coded in Django.


Django Extensions

This is an open-source collection of custom extensions used for Django projects, released under the MIT license.

How to use it

$ pip install django-extensions
Enter fullscreen mode Exit fullscreen mode

Enable the extensions in Django configuration (settings.py):

INSTALLED_APPS = (
    ...
    'django_extensions',
    ...
)
Enter fullscreen mode Exit fullscreen mode

Now we can use the magic via manage.py master script:

$ # Generate (and view) a graphviz graph of app models:
$ python manage.py graph_models -a -o myapp_models.png
$
$ # Check templates for rendering errors:
$ python manage.py validate_templates
$
$ # Run the enhanced django shell:
$ python manage.py shell_plus
Enter fullscreen mode Exit fullscreen mode

Django REST framework

Django REST framework is a powerful and flexible toolkit for building Web APIs with an impressive feature set:

  • The Web browsable API is a huge usability win for your developers.
  • Authentication policies including packages for OAuth1a and OAuth2.
  • Serialization that supports both ORM and non-ORM data sources.

Django REST Links:


Django Debug Toolbar

The Django Debug Toolbar is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content.

Django Debug Toolbar - Sample Screen.


Django CORS Headers

A Django App that adds Cross-Origin Resource Sharing (CORS) headers to responses. This allows in-browser requests to your Django application from other origins.

How to use it

$ pip install django-cors-headers
Enter fullscreen mode Exit fullscreen mode

Add the extension in Django configuration:


INSTALLED_APPS = [
    ...
    'corsheaders',
    ...
]
Enter fullscreen mode Exit fullscreen mode

You will also need to add a middleware class to listen in on responses:


MIDDLEWARE = [  # Or MIDDLEWARE_CLASSES on Django < 1.10
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

Enter fullscreen mode Exit fullscreen mode

Django reCaptcha v3

This integration app implements a ReCaptcha field for Google reCaptcha v3.

How to use it

$ pip install django-recaptcha3
Enter fullscreen mode Exit fullscreen mode

Then add django-recaptcha3 to your installed apps:

INSTALLED_APPS = (
    ...
    'snowpenguin.django.recaptcha3',
    ...
)
Enter fullscreen mode Exit fullscreen mode

Update settings.py with reCaptcha private and public key:

RECAPTCHA_PRIVATE_KEY = 'Super_s3Cret_1234'
RECAPTCHA_PUBLIC_KEY  = 'Public key'
Enter fullscreen mode Exit fullscreen mode

Usage in Forms


from snowpenguin.django.recaptcha3.fields import ReCaptchaField

class ExampleForm(forms.Form):
    [...]
    captcha = ReCaptchaField()
    [...]
Enter fullscreen mode Exit fullscreen mode

Drf-Yasg

Swagger/OpenAPI Documentation Generator for Django REST Framework.

How to use it

$ pip install -U drf-yasg
Enter fullscreen mode Exit fullscreen mode

Update the settings.py to enable the app:

INSTALLED_APPS = [
    ...
    'drf_yasg',
    ...
]
Enter fullscreen mode Exit fullscreen mode

Update urls.py:

...
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

...

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version='v1',
      description="Test description",
      terms_of_service="https://www.google.com/policies/terms/",
      contact=openapi.Contact(email="contact@snippets.local"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
   url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
   url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
   url(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
   ...
]
Enter fullscreen mode Exit fullscreen mode

These simple settings will expose 4 endpoints:

  • A JSON view of your API specification at /swagger.json
  • A YAML view of your API specification at /swagger.yaml
  • A swagger-ui view of your API specification at /swagger/
  • A ReDoc view of your API specification at /redoc/

Django-environ

Django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables.

How to use it

$ pip install django-environ
Enter fullscreen mode Exit fullscreen mode

Create an .env file in the root of your Django project (sample below):

DEBUG=on
SECRET_KEY=your-secret-key
DATABASE_URL=psql://urser:un-githubbedpassword@127.0.0.1:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
Enter fullscreen mode Exit fullscreen mode

Usage in Django Application (No need to add it to INSTALLED_APPS)


import environ
env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)
# reading .env file
environ.Env.read_env()

# False if not in os.environ
DEBUG = env('DEBUG')

# Raises django's ImproperlyConfigured exception if SECRET_KEY not in os.environ
SECRET_KEY = env('SECRET_KEY')

# Parse database connection url strings like psql://user:pass@127.0.0.1:8458/db
DATABASES = {
    # read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
    'default': env.db(),
    # read os.environ['SQLITE_URL']
    'extra': env.db('SQLITE_URL', default='sqlite:////tmp/my-tmp-sqlite.db')
}
Enter fullscreen mode Exit fullscreen mode

Thank you!

Top comments (4)

Collapse
 
simo97 profile image
ADONIS SIMO

Hi,
Didn't know about those admin packages, i use Django for 3 year now, thanks for sharing this. Seem like to use them i need to start a whole new project with them as project template.

Collapse
 
sm0ke profile image
Sm0ke

Yw & Happy Coding!

Collapse
 
jheld profile image
Jason Held

The swagger package listed is being deprecated I think. You may want to check out pypi.org/project/drf-yasg/

Collapse
 
sm0ke profile image
Sm0ke

Thank you! I will update the links :)