Insanely Useful Tips for Any Django Web Developer

Uncategorised • 29th Jul, 15


An excellent framework for Python, Django is known for its clean coding by automating many of the processes in programming.  For Django web developers, there are various tips and tricks which they could make use of to speed up development and save themselves from running into any unnecessary headaches in the future.  Here are a few tips that can help a web developer on any level become more comfortable using Django and all of the greatness that comes with it:

  • Use relative paths when it comes to configuration

We can’t always explain it, but projects seem to be moved around in location from time to time.  This is why it’s important that you plan ahead for instances such as these.  Rob Hudson came up with an excellent technique for this which goes something like:

“My default Django settings file has changed over time to now include settings that do not depend on the location of the project on the file system.  This is great in a team environment where more than one person is working on the same project, or when deploying your project to a web server that likely has different paths to the project root directory.”

  • Make use of the {%url%} tag

Rather than incorporating hardcore individual links, try to achieve the same result by using the backwards compatible {%url%}.  This will provide you with the absolute URL so that if by any chance your Django project moves, the links will remain as they should be.  This may not seem as though it’s a very advanced tip, but it will undoubtedly come in handy when it comes to your Django projects. 

  • Use Django admin for your PHP apps too

The user authentication system – this is possibly one of the greatest features of Django which has been built straight into the core.  Not only is it easy to set up, but it also comes packed with a robust system which will allow you to authenticate users and configure any other necessary settings.  You can be sure that this user system is amazing, considering that it’s been suggested to use Django as your admin area even for your PHP application.  Jeff Croft has something to say on why Django is the perfect solution for an admin system no matter what the application, regardless of what language it is:

“One of the core philosophies of Django is loose coupling.  Besides the more alluring free-love connotation, this means that each of the layers of Django 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 – its admin interface – with very little work, even if you plan to write the application logic itself in another language.”

  •  Choose a separate media server

 It may not seem efficient that Django gives you the option to serve static files in your development, but not in your production environment.  Here’s an explanation of why this is so:

“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 in your memory.  If you aren’t using that process to service a Django request, all the memory overhead is wasted.” 

By ensuring that you use a separate server to both house and serve static files, there’s no way that your performance can suffer.  

  • And the Debugger Toolbar

The thing about debugging tools for any language is that they are invaluable, speeding up development by spotting errors in code as well as potential pitfalls that might happen.  Rob Hudson once again had something to say about this, after releasing a Django debug toolbar which could help with debugging code:

“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.”    

  • Use Django Unit Testing

One way to ensure that your changes to the code work as expected without breaking any older code to maintain backwards capability is unit testing.  One of the great features which you’ll become aware of when using Django is how you’ll be able to write unit tests with ease, along with offering you the ability to use the unittest and doctest straight out of the box.        

  • And a Cheatsheet

Though you might have to do some digging around the Django documentation to find the cheatseet, it will offer you help with the some tricky topics, including:

  1. Templates

- template tags as well as their options

- template filters and their options

- date formatting syntax quick reference

  1. Models

- fields and their options

- common field options

- ModelAdmin options

- Meta class options

  1. Forms

- common field options

- fields and their options

- standard error_messages keys

  • Utilize Django-chunks

This is basically a way for you to create blocks of reused code in your already existing templates.  Thanks to Django’s rich text editor, this is all made even easier. 

  • Stop looking elsewhere and just use Django!

After reading through all of this, you should have a clear idea of just how powerful Django really is.  There are an endless amount of reasons for you to make use of Django for the next project coming your way, with time hacking combining with different sets of software to make your task that much easier.  Take a look at why Jeff Croft thinking that creating a project using Django alone is much more efficient than trying to hack together too many scripts to keep up with what’s going on:

“Now, if you’re used to building sites using a blogging app (Wordpress, TXP, etc.) as a CMS, you’re used to getting all that for free.  But you’re also getting a lot of things built-in that maybe you don’t want (unless, of course, you’re building a blog).  The limitation I ran across in using a blogging app for my personal site is that I wanted it to be more than just a blog.  As soon as I tried to hack a blog into a phone gallery, or a links feed, or a statistics database, a game of Sudoku, or whatever else I could think of, things started to get crufty.  I also built a website in which I tied to use a discussions forum app (vBulletin) as a CMS, and that had the same results.  It worked – sort of – but it wasn’t extensible, was very hackish, and just generally was inelegant.  With Django, you get all of these things and none of the limitations of a blogging app.”

Using Django, you will be able to expand your website into any direction which you would like without the stress and worries of hacking the design and making sure that the scripts and databases are compatible.    


Back to Blog