Useful Tools for Django Development

A lot of time during web application development is not spent writing code: it’s spent researching, investigating, debugging, testing, and so forth. In addition to the libraries used as part of the web application itself, the libraries and tools used in development are also critical to the application. By using the right tools, individual developers become more productive and can avoid human error. Here are a few of my favorites in Django development.

Text Editor

Although it sounds obvious, it’s worth learning how to use your editor better. Most popular editors are highly extensible with expert features, so spend the time to make yourself an expert user. You can save time by knowing keyboard shortcuts for common tasks. You may also be able to refactor entire applications with one keystroke. Configure¬†your personal environment and keep it in a public repository so you can sit down at any computer and instantly load yourself into your natural flow.

Suggested Tools: vi (vimtutor), emacs (built-in help), pycharm, sublime

Code Search

Coming into a new codebase can be very scary: it is probably big and poorly documented. It takes time to internalize it, and until then, you should rely on code search tools to trace references and understand the flow of the system. Even after you internalize it, these tools are helpful in big refactor jobs to make simultaneous changes and catch every reference.

Suggested tools: grep, ack (ack-grep), github/repo host search, sed, pycharm

Server-side Debugger

When I actually get to writing code, I spend most of my time debugging. Sometimes the task itself was a bug, but often, I need to figure out why my code doesn’t work. The two most common tools for debugging are print statements and debuggers. Print statements and logging in Python work fine for Django, though if you use print statements, be careful to remove them before committing since they can crash production servers.

Knowing how to use a debugger well can be even more powerful. You can check the state of your program at almost any point and follow the logical sequence of events as well. pdb is a very standard debugger and works with django development servers. Note that if you run your server in a shell inside of emacs, the debugger will actually navigate through the code in another buffer.

I myself use the debugger the most while trying to fix broken unit tests. Since the data is static, it’s more necessary to inspect state than during development when I can check and replicate data state in many ways.

Suggested tools: pdb, pycharm, print statements

Server Profiler

The debugger is quite powerful for checking stability and logic, but web applications also need to be fast. Browser developer tools can  monitor the aggregate performance from the network tab, but server-side profiling can break down the processes generating the responses. By seeing expensive computation and details of database queries, you can hone in on inefficient components in need of optimization. django-debug-toolbar happens to be one of my favorites. The Django ORM lets you become lazy about writing efficient queries, and the debug toolbar counts, times, and explains SQL queries. It also allows you to look at response contexts as an alternative to using the debugger.

Suggested tools: django-debug-toolbar, cProfile

Database Inspection

During development, I often have to reproduce bugs, test and retest particular flows, and diagnose various states. When the application is misbehaving, it is often because the underlying data doesn’t match your preconditions. Even outputted HTML can sometimes be misleading if the data is subject to processing, so always be willing to dig into the actual data and see the ground truth.

The bold can always go into their database terminal to run queries and describe tables. The Django built-in admin site, however, is a very accessible (and highly customizable) way to access the same data. It is also friendly enough for non-technical people to use as well.

Suggested tools: Django admin (http://yoursite.com/admin), SQL shell (python manage.py dbshell), Python shell (python manage.py shell)

Browser Activity Inspector

The internet browser is the web application client, and it can behave in surprising ways. The tools can be difficult to use as they are grouped functionally rather than pointed towards specific uses.

  1. The DOM is helpful for inspecting HTML and CSS to figure out tweaks without going back and forth to your editor.
  2. The console is useful for both outputting javascript console messages and running commands live to inspect the state of the program. It also may give you the stacktrace when there are JS errors
  3. The network tab is useful for looking at requests and responses, particularly to see what AJAX content is.
  4. The resources are useful for the debugger.

These tools are about as granular as it gets

Suggested tools: Chrome Console (tutorial at http://discover-devtools.codeschool.com/), Firebug, User Agent Switch FF/Chrome extensions

Wrap-up

Those are the tools I use the most often and would introduce to anyone new to Django development. There are many more for specific use cases and that I myself am unaware of. A great part about the community is that there are always more tools being developed to try out. As software engineers, we should always strive not only to improve the products we work on but also to improve our own knowledge and development environments to make ourselves as productive and efficient as possible.

Posted by: Kevin Leung, Lead Engineer