Tuesday, June 30, 2015

9 Insanely Useful Django Tips

There are quite a few great little tricks and tips one could use on their Django projects that would speed up development and save many headaches in the long run. From basic to obscure, these tips can help any skill-level of programmer become more adept with Django and all it's glory.
Django is an excellent framework for Python. While it may not get as much ink as other popular frameworks like Rails, it is just as much a polished framework as any of the rest. It puts plenty of emphasis on the DRY principle (Don't Repeat Yourself) in clean coding by automating many of the processes in programming.
For some reason, projects tend to be moved around in location from time to time. This can be an absolute bear to deal with if you don't first plan ahead for the possibility. Rob Hudson has an excellent technique to ensure that your Django deployments can be moved around with ease, only having to edit a few lines of code in your settings files
Instead of hardcoding individual links, try using the backwards compatible {% url %}tag to achieve the same result. This will give you the absolute URL, so that if, heaven forbid, your Django project moves the links will still remain in tact.
Essentially the {% url %} takes a view name and its parameters and does a reverse lookup to return the queried URL. If you make changes to your urls.py file, the links won't break.
While it's not the most advanced tip, it's an excellent technique to use in your django projects.
Possibly one of the greatest features of Django is the user authentication system that Django has built straight into the core. It's seriously easy to set up, and it comes packed with a robust system to authenticate users and configure other necessary settings.
One of the core philosophies of Django is loose coupling. Besides the more alluring free-love connotation, this mean that each of the layers of Django’s “stack” ought to be independent of the others, such that you can choose to use only bits and pieces of it, and/or slide in other components if you prefer. Lucky you, it’s incredibly simple to learn the basics of Django’s model layer, which handles database schema and object-relational mapping — even if you don’t know Python. Anyone can create a database schema in Django and get the crown jewel of the framework -- it's admin interface -- with very little work, even if you plan to write the application logic itself in another language.
Django allows you to serve static files in your development environment, but not your production environment Why? It's not efficient. At all. Jacobian.org gives an explanation.
Django deliberately doesn't serve media for you, and it's designed that way to save you from yourself. If you try to serve media from the same Apache instance that's serving Django, you're going to absolutely kill performance. Apache reuses processes between each request, so once a process caches all the code and libraries for Django, those stick around in memory. If you aren't using that process to service a Django request, all the memory overhead is wasted.
Debugging tools for any language are invaluable. They speed up development by spotting errors in your code and potential pitfalls that might happen. Rob Hudson has recently released the django debug toolbar to help with debugging code, and it could greatly help any developer.
The toolbar itself is a piece of middleware that instantiates each panel object on request, and performs processing and rendering as the response is being written back to the browser. In this way it is essentially a set of middleware classes (the panels) grouped together to display a single toolbar. Each panel subclasses a base panel class and overrides a few methods to render the toolbar.

Unit testing is a great way to ensure that your changes to the code that it works as expected and doesn't break any older code to maintain backwards compatibility. A great feature in Django is that it's incredibly easy to write unit tests. Django offers the ability to use the doctest or unittest straight out of the box.
Django's documentation offers a great tutorial and some sample code on how to set up unit tests to keep your code running smoothly and spot any nasty bugs.

Is cheating wrong? I hope not. This nifty 2-page cheatsheet contains some golden nuggets within arm's reach that one might have to dig around the Django documentation to find.
The cheatsheet features these helpful topics:
Templates
  • Template tags and their options
  • Template filters and their options
  • Date formatting syntax quick reference
Models
  • Fields and their options
  • Common field options
  • Meta class options
  • ModelAdmin options
Forms
  • Fields and their options
  • Common field options
  • Standard error_messages keys
We all know time spent looking at documentation is time spent not solving the world’s problems through code. And no good programmer wants that.
Django-chunks is essentially a way to create blocks of reused code in your templates. Except creating the blocks is made even easier by using Django's rich text editor.
Well it essentially allows someone to define "chunks" (I had wanted to call it blocks, but that would be very confusing for obvious reasons) of content in your template that can be directly edited from the awesome Django admin interface. Throwing a rich text editor control on top of it make it even easier.
By replicating bits of reusable code, django-chunks ensures that pieces of the layout can quickly and easily be changed if need be. While django-chunks is really only a model and template tag, it can greatly speed up development by replicating processes.

If performance is going to be an issue with your Django-powered application, you'll want to install some sort of caching. While Django offers many options for caching, the best by far is memcached. Installing and using memcached is fairly easy with Django if you use the cmemcache module. Once the module is installed, you just change a single configuration option, and your Django pages will be served lighting fast.