What Is New in Django 1.2
Django 1.2 introduces a significant set of features and refinements that enhance the framework's capabilities for building complex applications. This release focuses on providing developers with more powerful tools while maintaining backward compatibility where possible.
| Category | Key Changes |
|---|---|
| New Features | Multiple database support, model validation, class-based views, messages framework, email backends |
| Improvements | Enhanced CSRF protection, smarter if template tag, improved localization |
| Backwards Incompatible Changes | CSRF protection overhaul, changes to admin static files, url tag syntax update |
How does multiple database support work?
Django 1.2 finally delivers native support for multiple databases, a highly requested enterprise feature. You can now define multiple database connections in your settings and route queries appropriately.
This is implemented through a database router API that lets you control which models use which databases. In practice, this means you can have a read replica for certain queries or split your data across different database systems.
Database Router Example
class AuthRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label == 'auth':
return 'auth_db'
return None
DATABASE_ROUTERS = ['path.to.AuthRouter']
What improvements were made to model validation?
Model validation got a major upgrade with full-cycle validation that runs both at the model and form levels. The new Model.full_clean() method provides comprehensive validation outside of form processing.
This matters because you can now validate your models programmatically without going through forms. The validation checks all constraints including field validation, unique constraints, and custom validation methods.
How were class-based views implemented?
Django 1.2 introduces the foundation for class-based views, though they weren't the primary interface yet. This started as a contrib module that would later become central to Django's view system.
The initial implementation provided generic views as classes rather than functions, offering better extensibility through inheritance and method overriding. This approach eventually replaced function-based generic views entirely in later versions.
What security improvements were introduced?
The CSRF protection system was completely overhauled to be more secure and easier to use. The new implementation moved away from the older middleware approach to a more robust solution.
Instead of the previous {% raw %}{% csrf_token %}{% endraw %} tag that only worked with forms, the protection now applies to all POST requests. This required adding the token to AJAX requests and forms, making the protection more comprehensive.
How did the messages framework improve user experience?
Django 1.2 introduced a unified messages framework for displaying one-time notifications to users. This replaced the older admin-specific message system with a general-purpose solution.
The framework supports different message levels (info, success, warning, error) and multiple storage backends including session-based and cookie-based storage. This made it much easier to provide user feedback across an entire application.
FAQ
How do I handle the CSRF changes when upgrading to Django 1.2?
You need to add {% raw %}{% csrf_token %}{% endraw %} to all your forms and ensure AJAX requests include the CSRF token in their headers. The old csrf middleware should be replaced with the new django.middleware.csrf.CsrfViewMiddleware.
Can I use different database engines with multiple database support?
Yes, you can mix database engines - for example, using PostgreSQL for your main data and MySQL for logging or specific applications. The router system doesn't restrict you to a single database type.
What happened to the old generic views?
The function-based generic views still work in 1.2 but are deprecated. The new class-based generic views in django.views.generic provide the same functionality with better organization and extensibility.
How do I use the new messages framework?
Use from django.contrib import messages and then call messages.success(request, "Your message") in your views. Display messages in templates with template tags that iterate through the message storage.
What's the deal with the updated url tag syntax?
The {% raw %}{% url %}{% endraw %} tag now requires quotes around view names and arguments. Instead of {% raw %}{% url myapp.views.my_view arg %}{% endraw %}, you now write {% raw %}{% url "myapp.views.my_view" arg %}{% endraw %} to avoid parsing issues.