The Web framework for perfectionists (with deadlines). Django makes it easier to build better Web apps more quickly and with less code.
Free online book: DjangoBook
Django is a web framework which I have been using for a very long time now, and I hold it in high regards. I tend to compare other frameworks to Django, although not every framework shares the same goal.
If you are looking for an easy to use Pythonic web framework, Django has you covered. It has a full community of support, a plethora of 3rd party apps, and is one of the most widely known and used frameworks in the Python community. If you want to get into Python web development, then Django is a great starting place. If you are just starting out using Python, Django is also great for the novice programmers as well. The code is very clean and readable, with very MVC-like separation. Django calls their implementation of MVC, MTV. MTV stands for Model, View, Template. Which makes perfect sense when programming for the web. MVC in my eyes, seems more targeted to desktop programming
The Django API is very well laid out and every module is where one would think it is. The API in general is very easy to learn and understand, and to make it even easier for novices, classes are not required to make views. The API fully includes what you would expect from a full-featured framework, session management, cookie management, middleware support, and a handful of other goodies. I find that I rarely need to go looking around online to find a module, since Django has a great set of included APIs, and once you download your preferred 3rd party Django apps, you can work almost entirely offline. The entire Django documentation is even downloadable in various formats as well.
Out of the box, Django provides you everything you need right away to get started. The Django installation doesn't have many dependencies as it uses it's own template engine, ORM, etc... This makes the actual framework an integrated solution, since it doesn't depend on any outside Python packages, upgrades to newer versions of Django are generally smooth. You don't need to worry about upgrading your other Python modules just to upgrade Django. This is not saying that this fully integrated solution is the best, since it can make using third party template engines and database abstraction layers a tad interesting at times.
The Django admin interface as can be seen in the screenshot to the right is very customizable. You can change how the fields are displayed, if any fields are hidden from view initial and even view related tables with ease. In the next screenshot, which you should click to enlarge, is from one of my most recent projects, Hacker's Edge. You can see from this screen capture, that a section of fields classified as Resources is entirely hidden, unless a simple link is clicked. Below, you can see two related tables, and the objects which are pointing to this row. All of this was created using only a few lines of Python code... Actually, all is needed is to subclass a single class, and add some tuples to it.
Having such a powerful and customizable administrative interface is very nice to have in a web framework, especially if your website is very data-centric. You can also assign permissions to various users from this interface, you can say assign Sally Anne access to the Inventory app, but only give her access to a specific table in the eShop app. This will allow her to add new inventory items, and place them up for sale on the website. Permissions are app and table based by default. You can provide access to Add, Change, or Delete objects in a particular table. You can provide all those access rights, or only a subset. The Admin interface can even be customized to only display items created by Sally Anne, when she is logged in as herself. So, this can enable her to only change inventory items she added personally.
If you need fine-grained row-level permissions, this is available in various 3rd party Django apps, which can be found on the package list of this website.
The extendability of Django is it's high point. It comes with so many APIs, signals, middleware, and template tags, that there is at least one way to extend Django to do a particular task you have in mind. There are signals to check when a database table has been altered. Signals are best used with apps you do not have direct control of, like something in Django's core, or a 3rd party app. Lots of 3rd party Django apps provide signals, which are basically hooks to change how they function without needing to directly fork their projects. For your own apps, you can simply alter the Model class as much as you see fit. You can override a models save() method, to change how a data is saved back to the database. Say you want to alter the modified_at field when a model is saved, and also change the field modified_by to whom modified it. The save method is called everywhere, including in the admin interface. There are exceptions to the save method, these are when you get into relational field saving. The best part about modified_at, is that it is supported natively by Django, there is no need to extend or override any methods to make this work.
Django includes many template tags to make web development much easier, and preventing broken links on your website. When using Django, never enter in the URLs manually, and get into that habit. Instead, use a simple template tag and tell it what view you want to link to. If you ever change your urls, all of these links will also change with it. When it comes to using 3rd party apps, this is a must. It also makes referencing components inside those 3rd party apps very easy.
From Django 1.3 onwards, class-based views are fully supported. This makes developing websites even quicker than before.
from django.views.generic.edit import UpdateView from django.views.generic.list import ListView from gregarious.forms import ProfileForm from gregarious.models import PhotoAlbum from django.core.urlresolvers import reverse class UpdateProfile(UpdateView): form_class = ProfileForm def get_object(self): return self.request.profile def get_success_url(self): return reverse('profile-home') class PhotoAlbumList(ListView): def get_queryset(self): return PhotoAlbum.objects.filter(user=self.request.user)
This is a simple class-based view to display and take submit data for a Update profile form. The profile itself is saved on the request. The class itself is very easy to read and understand what's going on exactly. The second class demonstrates how to display a simple photo album list. In both examples, the code is very easy to follow and understand for almost anybody. This is how a form based off a model is created in Django, AKA Django's CRUD system:
from django import forms from gregarious.models import SocialProfile class ProfileForm(forms.ModelForm): class Meta: model = SocialProfile exclude = ('user', 'photo',)
This is entirely how Django development is, creating a database table/model is no different than creating a form. You extend a class, and add variables to the class to describe the database tables structure. If you have an existing database you would like to use with Django, you can easily inspect that database, and Django will auto-magically create the needed classes for you. You should slightly modify the generated classes to match your exact requirements.
Django's template system is very customizable, and creating new tags to use in your website couldn't be easier. Say, your website embeds a good amount of YouTube videos, and you'd rather use a simple tag to place them in your templates. Here's how easy that is done in Django:
from django import template register = template.Library() @register.simple_tag def youtube(slug): return '<iframe width="420" height="315" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>' % slug
Then in your templates, all you need to do, is reference the slug for your YouTube video, as you can see from the above linkage, you can tell exactly what I mean by your video's slug. Here is an example of the above tag in action:
Django is definitely one powerful framework, and I highly recommend it for any Python developer, new or old.
- Fairly easy to install and setup
- Plenty of deployment options
- Supports standard Python modules
- Plenty of existing 3rd party apps
- Support for all major Database engines
- Works on Google App Engine
- Extendable template engine based on Jinja
- Great separation of front-end and back-end code
- Easy to navigate and sane project layout
- Plenty of "batteries included"
- Automatic admin interface
- The database abstraction layer can be slow sometimes
- ORM not as flexible as SQLAlchemy
- Admin interface only supports Django ORM
- No AJAX framework out of the box(like Rails/Web2Py)
- Some 3rd party apps can be tedious to install
- Backward compatibility with Django 1.2 is gone