#16 Django Extensions

By far one of the most useful app to include in your django toolbox is Django Extensions. It is a must have app for any django project. It provides a bunch of very useful management commands, admin extensions and much more.

Install it with pip command and then include it in INSTALLED_APPS in settings.py file. Notice that in INSTALLED_APPS django_extensions is written with _ (underscore) while for pip install command it is specified with "-" (dash) character.

    
$ pip install django-extensions  # notice "-" character
$ cat settings.py
...
INSTALLED_APPS = [
 ...
   django_extensions,  # notice "_" character
 ...
]
...
    

Let's review top 5 most practical commands:

  • show_urls
  • shell_plus
  • graph_models
  • runserver_plus

show_urls

The django extension that I personally find most useful is show_urls.

show_urls prints all routes used by your django application. I found it very useful for discovering not documented routes added by 3rd party apps. It is also immediately available one you install django extensions app without external dependencies (other commands may require additional modules to install). It displays information in URL - View - url_name format:

    
/pages/_util/login/     django.contrib.auth.views.LoginView     wagtailcore_login
/privacy        lessons.views.PageView  privacy
/subscribe      lessons.views.subscribe subscribe
    

shell_plus

This one is very cool, it adds auto-complete and syntax highlighting in django shell. It provides a lot of fun when experimenting with models and functions in interactive mode. shell_plus relies on

on another third party libraries like ipython or bpython. I advice you to experiment with both of them. Let's first install both modules:

    
$ pip install ipython bpython
    

Then start shell_plus session based on bpython provider:

    
$ python manage shell_plus --bpython
    
shell_plus_bpython_session.png

Similarly you can choose for ipython based shell. It provides little bit different look and feel, as I said, experiment with both to decide which one suites you better.

    
$ python manage shell_plus --ipython
    
shell_plus_ipython_session.png

graph_models

This one is really cool. This command requires pygraphviz though, which is a python wrapper around graphviz. So you will need to install both for model_graph to work.

The way you install graphviz depends on the operating system you are using:

    
$ sudo pacman -S graphviz       # on archlinux
$ sudo apt-get install graphviz  # on ubuntu
$ pip install pygraphviz
    

To generate a png image with all models of say, lessons app, use command:

    
$ python manage.py graph_models lessons -o lessons.png
    
graph_model_visualisation.png

runserver_plus

This command is usual django's runserver command with one neat feature added - it includes werkzeug's interactive debugger. Basically if you got a python's exception, beside displaying exception stack trace, it adds a interactive debugger directly in the context of that exception in web browser. Using this nice feature in development/DEBUG=True mode only. As of writing this lesson, I was unable to team together django latest version 3.0.3 and werkzeug latest version 1.0.0. To make runserver_plus command recognize and work with werkzeug I needed to downgrade werkzeug to 0.16 version.