| 1 |
======================== |
| 2 |
Django 1.4 release notes |
| 3 |
======================== |
| 4 |
|
| 5 |
*March 23, 2012* |
| 6 |
|
| 7 |
Welcome to Django 1.4! |
| 8 |
|
| 9 |
These release notes cover the :ref:`new features <whats-new-1.4>`, as well as |
| 10 |
some :ref:`backwards incompatible changes <backwards-incompatible-1.4>` you'll |
| 11 |
want to be aware of when upgrading from Django 1.3 or older versions. We've |
| 12 |
also dropped some features, which are detailed in :ref:`our deprecation plan |
| 13 |
<deprecation-removed-in-1.4>`, and we've :ref:`begun the deprecation process |
| 14 |
for some features <deprecated-features-1.4>`. |
| 15 |
|
| 16 |
Overview |
| 17 |
======== |
| 18 |
|
| 19 |
The biggest new feature in Django 1.4 is `support for time zones`_ when |
| 20 |
handling date/times. When enabled, this Django will store date/times in UTC, |
| 21 |
use timezone-aware objects internally, and translate them to users' local |
| 22 |
timezones for display. |
| 23 |
|
| 24 |
If you're upgrading an existing project to Django 1.4, switching to the timezone |
| 25 |
aware mode may take some care: the new mode disallows some rather sloppy |
| 26 |
behavior that used to be accepted. We encourage anyone who's upgrading to check |
| 27 |
out the :ref:`timezone migration guide <time-zones-migration-guide>` and the |
| 28 |
:ref:`timezone FAQ <time-zones-faq>` for useful pointers. |
| 29 |
|
| 30 |
Other notable new features in Django 1.4 include: |
| 31 |
|
| 32 |
* A number of ORM improvements, including `SELECT FOR UPDATE support`_, |
| 33 |
the ability to `bulk insert <#model-objects-bulk-create-in-the-orm>`_ |
| 34 |
large datasets for improved performance, and |
| 35 |
`QuerySet.prefetch_related`_, a method to batch-load related objects |
| 36 |
in areas where :meth:`~django.db.models.query.QuerySet.select_related` |
| 37 |
doesn't work. |
| 38 |
|
| 39 |
* Some nice security additions, including `improved password hashing`_ |
| 40 |
(featuring PBKDF2_ and bcrypt_ support), new `tools for cryptographic |
| 41 |
signing`_, several `CSRF improvements`_, and `simple clickjacking |
| 42 |
protection`_. |
| 43 |
|
| 44 |
* An `updated default project layout and manage.py`_ that removes the "magic" |
| 45 |
from prior versions. And for those who don't like the new layout, you can |
| 46 |
use `custom project and app templates`_ instead! |
| 47 |
|
| 48 |
* `Support for in-browser testing frameworks`_ (like Selenium_). |
| 49 |
|
| 50 |
* ... and a whole lot more; `see below <#what-s-new-in-django-1-4>`_! |
| 51 |
|
| 52 |
Wherever possible we try to introduce new features in a backwards-compatible |
| 53 |
manner per :doc:`our API stability policy </misc/api-stability>` policy. |
| 54 |
However, as with previous releases, Django 1.4 ships with some minor |
| 55 |
:ref:`backwards incompatible changes <backwards-incompatible-1.4>`; people |
| 56 |
upgrading from previous versions of Django should read that list carefully. |
| 57 |
|
| 58 |
Python compatibility |
| 59 |
==================== |
| 60 |
|
| 61 |
Django 1.4 has dropped support for Python 2.4. Python 2.5 is now the minimum |
| 62 |
required Python version. Django is tested and supported on Python 2.5, 2.6 and |
| 63 |
2.7. |
| 64 |
|
| 65 |
This change should affect only a small number of Django users, as most |
| 66 |
operating-system vendors today are shipping Python 2.5 or newer as their default |
| 67 |
version. If you're still using Python 2.4, however, you'll need to stick to |
| 68 |
Django 1.3 until you can upgrade. Per :doc:`our support policy |
| 69 |
</internals/release-process>`, Django 1.3 will continue to receive security |
| 70 |
support until the release of Django 1.5. |
| 71 |
|
| 72 |
Django does not support Python 3.x at this time. At some point before the |
| 73 |
release of Django 1.4, we plan to publish a document outlining our full |
| 74 |
timeline for deprecating Python 2.x and moving to Python 3.x. |
| 75 |
|
| 76 |
.. _whats-new-1.4: |
| 77 |
|
| 78 |
What's new in Django 1.4 |
| 79 |
======================== |
| 80 |
|
| 81 |
Support for time zones |
| 82 |
---------------------- |
| 83 |
|
| 84 |
In previous versions, Django used "naive" date/times (that is, date/times |
| 85 |
without an associated time zone), leaving it up to each developer to interpret |
| 86 |
what a given date/time "really means". This can cause all sorts of subtle |
| 87 |
timezone-related bugs. |
| 88 |
|
| 89 |
In Django 1.4, you can now switch Django into a more correct, time-zone aware |
| 90 |
mode. In this mode, Django stores date and time information in UTC in the |
| 91 |
database, uses time-zone-aware datetime objects internally and translates them |
| 92 |
to the end user's time zone in templates and forms. Reasons for using this |
| 93 |
feature include: |
| 94 |
|
| 95 |
- Customizing date and time display for users around the world. |
| 96 |
|
| 97 |
- Storing datetimes in UTC for database portability and interoperability. |
| 98 |
(This argument doesn't apply to PostgreSQL, because it already stores |
| 99 |
timestamps with time zone information in Django 1.3.) |
| 100 |
|
| 101 |
- Avoiding data corruption problems around DST transitions. |
| 102 |
|
| 103 |
Time zone support is enabled by default in new projects created with |
| 104 |
:djadmin:`startproject`. If you want to use this feature in an existing |
| 105 |
project, read the :ref:`migration guide <time-zones-migration-guide>`. If you |
| 106 |
encounter problems, there's a helpful :ref:`FAQ <time-zones-faq>`. |
| 107 |
|
| 108 |
Support for in-browser testing frameworks |
| 109 |
----------------------------------------- |
| 110 |
|
| 111 |
Django 1.4 supports integration with in-browser testing frameworks like |
| 112 |
Selenium_. The new :class:`django.test.LiveServerTestCase` base class lets you |
| 113 |
test the interactions between your site's front and back ends more |
| 114 |
comprehensively. See the |
| 115 |
:class:`documentation<django.test.LiveServerTestCase>` for more details and |
| 116 |
concrete examples. |
| 117 |
|
| 118 |
.. _Selenium: https://www.selenium.dev/ |
| 119 |
|
| 120 |
Updated default project layout and ``manage.py`` |
| 121 |
------------------------------------------------ |
| 122 |
|
| 123 |
Django 1.4 ships with an updated default project layout and ``manage.py`` file |
| 124 |
for the :djadmin:`startproject` management command. These fix some issues with |
| 125 |
the previous ``manage.py`` handling of Python import paths that caused double |
| 126 |
imports, trouble moving from development to deployment, and other |
| 127 |
difficult-to-debug path issues. |
| 128 |
|
| 129 |
The previous ``manage.py`` called functions that are now deprecated, and thus |
| 130 |
projects upgrading to Django 1.4 should update their ``manage.py``. (The |
| 131 |
old-style ``manage.py`` will continue to work as before until Django 1.6. In |
| 132 |
1.5 it will raise ``DeprecationWarning``). |
| 133 |
|
| 134 |
The new recommended ``manage.py`` file should look like this:: |
| 135 |
|
| 136 |
#!/usr/bin/env python |
| 137 |
import os, sys |
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings") |
| 141 |
|
| 142 |
from django.core.management import execute_from_command_line |
| 143 |
|
| 144 |
execute_from_command_line(sys.argv) |
| 145 |
|
| 146 |
``{{ project_name }}`` should be replaced with the Python package name of the |
| 147 |
actual project. |
| 148 |
|
| 149 |
If settings, URLconfs and apps within the project are imported or referenced |
| 150 |
using the project name prefix (e.g. ``myproject.settings``, ``ROOT_URLCONF = |
| 151 |
"myproject.urls"``, etc.), the new ``manage.py`` will need to be moved one |
| 152 |
directory up, so it is outside the project package rather than adjacent to |
| 153 |
``settings.py`` and ``urls.py``. |
| 154 |
|
| 155 |
For instance, with the following layout: |
| 156 |
|
| 157 |
.. code-block:: text |
| 158 |
|
| 159 |
manage.py |
| 160 |
mysite/ |
| 161 |
__init__.py |
| 162 |
settings.py |
| 163 |
urls.py |
| 164 |
myapp/ |
| 165 |
__init__.py |
| 166 |
models.py |
| 167 |
|
| 168 |
You could import ``mysite.settings``, ``mysite.urls``, and ``mysite.myapp``, |
| 169 |
but not ``settings``, ``urls``, or ``myapp`` as top-level modules. |
| 170 |
|
| 171 |
Anything imported as a top-level module can be placed adjacent to the new |
| 172 |
``manage.py``. For instance, to decouple ``myapp`` from the project module and |
| 173 |
import it as just ``myapp``, place it outside the ``mysite/`` directory: |
| 174 |
|
| 175 |
.. code-block:: text |
| 176 |
|
| 177 |
manage.py |
| 178 |
myapp/ |
| 179 |
__init__.py |
| 180 |
models.py |
| 181 |
mysite/ |
| 182 |
__init__.py |
| 183 |
settings.py |
| 184 |
urls.py |
| 185 |
|
| 186 |
If the same code is imported inconsistently (some places with the project |
| 187 |
prefix, some places without it), the imports will need to be cleaned up when |
| 188 |
switching to the new ``manage.py``. |
| 189 |
|
| 190 |
Custom project and app templates |
| 191 |
-------------------------------- |
| 192 |
|
| 193 |
The :djadmin:`startapp` and :djadmin:`startproject` management commands |
| 194 |
now have a ``--template`` option for specifying a path or URL to a custom app |
| 195 |
or project template. |
| 196 |
|
| 197 |
For example, Django will use the ``/path/to/my_project_template`` directory |
| 198 |
when you run the following command: |
| 199 |
|
| 200 |
.. code-block:: shell |
| 201 |
|
| 202 |
django-admin.py startproject --template=/path/to/my_project_template myproject |
| 203 |
|
| 204 |
You can also now provide a destination directory as the second |
| 205 |
argument to both :djadmin:`startapp` and :djadmin:`startproject`: |
| 206 |
|
| 207 |
.. code-block:: shell |
| 208 |
|
| 209 |
django-admin.py startapp myapp /path/to/new/app |
| 210 |
django-admin.py startproject myproject /path/to/new/project |
| 211 |
|
| 212 |
For more information, see the :djadmin:`startapp` and :djadmin:`startproject` |
| 213 |
documentation. |
| 214 |
|
| 215 |
Improved WSGI support |
| 216 |
--------------------- |
| 217 |
|
| 218 |
The :djadmin:`startproject` management command now adds a :file:`wsgi.py` |
| 219 |
module to the initial project layout, containing a simple WSGI application that |
| 220 |
can be used for :doc:`deploying with WSGI app |
| 221 |
servers</howto/deployment/wsgi/index>`. |
| 222 |
|
| 223 |
The :djadmin:`built-in development server<runserver>` now supports using an |
| 224 |
externally-defined WSGI callable, which makes it possible to run ``runserver`` |
| 225 |
with the same WSGI configuration that is used for deployment. The new |
| 226 |
:setting:`WSGI_APPLICATION` setting lets you configure which WSGI callable |
| 227 |
:djadmin:`runserver` uses. |
| 228 |
|
| 229 |
(The ``runfcgi`` management command also internally wraps the WSGI |
| 230 |
callable configured via :setting:`WSGI_APPLICATION`.) |
| 231 |
|
| 232 |
``SELECT FOR UPDATE`` support |
| 233 |
----------------------------- |
| 234 |
|
| 235 |
Django 1.4 includes a :meth:`QuerySet.select_for_update() |
| 236 |
<django.db.models.query.QuerySet.select_for_update>` method, which generates a |
| 237 |
``SELECT ... FOR UPDATE`` SQL query. This will lock rows until the end of the |
| 238 |
transaction, meaning other transactions cannot modify or delete rows matched by |
| 239 |
a ``FOR UPDATE`` query. |
| 240 |
|
| 241 |
For more details, see the documentation for |
| 242 |
:meth:`~django.db.models.query.QuerySet.select_for_update`. |
| 243 |
|
| 244 |
``Model.objects.bulk_create`` in the ORM |
| 245 |
---------------------------------------- |
| 246 |
|
| 247 |
This method lets you create multiple objects more efficiently. It can result in |
| 248 |
significant performance increases if you have many objects. |
| 249 |
|
| 250 |
Django makes use of this internally, meaning some operations (such as database |
| 251 |
setup for test suites) have seen a performance benefit as a result. |
| 252 |
|
| 253 |
See the :meth:`~django.db.models.query.QuerySet.bulk_create` docs for more |
| 254 |
information. |
| 255 |
|
| 256 |
``QuerySet.prefetch_related`` |
| 257 |
----------------------------- |
| 258 |
|
| 259 |
Similar to :meth:`~django.db.models.query.QuerySet.select_related` but with a |
| 260 |
different strategy and broader scope, |
| 261 |
:meth:`~django.db.models.query.QuerySet.prefetch_related` has been added to |
| 262 |
:class:`~django.db.models.query.QuerySet`. This method returns a new |
| 263 |
``QuerySet`` that will prefetch each of the specified related lookups in a |
| 264 |
single batch as soon as the query begins to be evaluated. Unlike |
| 265 |
``select_related``, it does the joins in Python, not in the database, and |
| 266 |
supports many-to-many relationships, ``GenericForeignKey`` and more. This |
| 267 |
allows you to fix a very common performance problem in which your code ends up |
| 268 |
doing O(n) database queries (or worse) if objects on your primary ``QuerySet`` |
| 269 |
each have many related objects that you also need to fetch. |
| 270 |
|
| 271 |
Improved password hashing |
| 272 |
------------------------- |
| 273 |
|
| 274 |
Django's auth system (``django.contrib.auth``) stores passwords using a one-way |
| 275 |
algorithm. Django 1.3 uses the SHA1_ algorithm, but increasing processor speeds |
| 276 |
and theoretical attacks have revealed that SHA1 isn't as secure as we'd like. |
| 277 |
Thus, Django 1.4 introduces a new password storage system: by default Django now |
| 278 |
uses the PBKDF2_ algorithm (as recommended by NIST_). You can also easily choose |
| 279 |
a different algorithm (including the popular bcrypt_ algorithm). For more |
| 280 |
details, see :ref:`auth_password_storage`. |
| 281 |
|
| 282 |
.. _sha1: https://en.wikipedia.org/wiki/SHA1 |
| 283 |
.. _pbkdf2: https://en.wikipedia.org/wiki/PBKDF2 |
| 284 |
.. _nist: https://csrc.nist.gov/publications/detail/sp/800-132/final |
| 285 |
.. _bcrypt: https://en.wikipedia.org/wiki/Bcrypt |
| 286 |
|
| 287 |
HTML5 doctype |
| 288 |
------------- |
| 289 |
|
| 290 |
We've switched the admin and other bundled templates to use the HTML5 |
| 291 |
doctype. While Django will be careful to maintain compatibility with older |
| 292 |
browsers, this change means that you can use any HTML5 features you need in |
| 293 |
admin pages without having to lose HTML validity or override the provided |
| 294 |
templates to change the doctype. |
| 295 |
|
| 296 |
List filters in admin interface |
| 297 |
------------------------------- |
| 298 |
|
| 299 |
Prior to Django 1.4, the :mod:`~django.contrib.admin` app let you specify |
| 300 |
change list filters by specifying a field lookup, but it didn't allow you to |
| 301 |
create custom filters. This has been rectified with a simple API (previously |
| 302 |
used internally and known as "FilterSpec"). For more details, see the |
| 303 |
documentation for :attr:`~django.contrib.admin.ModelAdmin.list_filter`. |
| 304 |
|
| 305 |
Multiple sort in admin interface |
| 306 |
-------------------------------- |
| 307 |
|
| 308 |
The admin change list now supports sorting on multiple columns. It respects all |
| 309 |
elements of the :attr:`~django.contrib.admin.ModelAdmin.ordering` attribute, and |
| 310 |
sorting on multiple columns by clicking on headers is designed to mimic the |
| 311 |
behavior of desktop GUIs. We also added a |
| 312 |
:meth:`~django.contrib.admin.ModelAdmin.get_ordering` method for specifying the |
| 313 |
ordering dynamically (i.e., depending on the request). |
| 314 |
|
| 315 |
New ``ModelAdmin`` methods |
| 316 |
-------------------------- |
| 317 |
|
| 318 |
We added a :meth:`~django.contrib.admin.ModelAdmin.save_related` method to |
| 319 |
:mod:`~django.contrib.admin.ModelAdmin` to ease customization of how |
| 320 |
related objects are saved in the admin. |
| 321 |
|
| 322 |
Two other new :class:`~django.contrib.admin.ModelAdmin` methods, |
| 323 |
:meth:`~django.contrib.admin.ModelAdmin.get_list_display` and |
| 324 |
:meth:`~django.contrib.admin.ModelAdmin.get_list_display_links` |
| 325 |
enable dynamic customization of fields and links displayed on the admin |
| 326 |
change list. |
| 327 |
|
| 328 |
Admin inlines respect user permissions |
| 329 |
-------------------------------------- |
| 330 |
|
| 331 |
Admin inlines now only allow those actions for which the user has |
| 332 |
permission. For ``ManyToMany`` relationships with an auto-created intermediate |
| 333 |
model (which does not have its own permissions), the change permission for the |
| 334 |
related model determines if the user has the permission to add, change or |
| 335 |
delete relationships. |
| 336 |
|
| 337 |
Tools for cryptographic signing |
| 338 |
------------------------------- |
| 339 |
|
| 340 |
Django 1.4 adds both a low-level API for signing values and a high-level API |
| 341 |
for setting and reading signed cookies, one of the most common uses of |
| 342 |
signing in web applications. |
| 343 |
|
| 344 |
See the :doc:`cryptographic signing </topics/signing>` docs for more |
| 345 |
information. |
| 346 |
|
| 347 |
Cookie-based session backend |
| 348 |
---------------------------- |
| 349 |
|
| 350 |
Django 1.4 introduces a cookie-based session backend that uses the tools for |
| 351 |
:doc:`cryptographic signing </topics/signing>` to store the session data in |
| 352 |
the client's browser. |
| 353 |
|
| 354 |
.. warning:: |
| 355 |
|
| 356 |
Session data is signed and validated by the server, but it's not |
| 357 |
encrypted. This means a user can view any data stored in the |
| 358 |
session but cannot change it. Please read the documentation for |
| 359 |
further clarification before using this backend. |
| 360 |
|
| 361 |
See the :ref:`cookie-based session backend <cookie-session-backend>` docs for |
| 362 |
more information. |
| 363 |
|
| 364 |
New form wizard |
| 365 |
--------------- |
| 366 |
|
| 367 |
The previous ``FormWizard`` from ``django.contrib.formtools`` has been |
| 368 |
replaced with a new implementation based on the class-based views |
| 369 |
introduced in Django 1.3. It features a pluggable storage API and doesn't |
| 370 |
require the wizard to pass around hidden fields for every previous step. |
| 371 |
|
| 372 |
Django 1.4 ships with a session-based storage backend and a cookie-based |
| 373 |
storage backend. The latter uses the tools for |
| 374 |
:doc:`cryptographic signing </topics/signing>` also introduced in |
| 375 |
Django 1.4 to store the wizard's state in the user's cookies. |
| 376 |
|
| 377 |
``reverse_lazy`` |
| 378 |
---------------- |
| 379 |
|
| 380 |
A lazily evaluated version of ``reverse()`` was added to allow using URL |
| 381 |
reversals before the project's URLconf gets loaded. |
| 382 |
|
| 383 |
Translating URL patterns |
| 384 |
------------------------ |
| 385 |
|
| 386 |
Django can now look for a language prefix in the URLpattern when using the new |
| 387 |
:func:`~django.conf.urls.i18n.i18n_patterns` helper function. |
| 388 |
It's also now possible to define translatable URL patterns using |
| 389 |
``django.utils.translation.ugettext_lazy()``. See |
| 390 |
:ref:`url-internationalization` for more information about the language prefix |
| 391 |
and how to internationalize URL patterns. |
| 392 |
|
| 393 |
Contextual translation support for ``{% trans %}`` and ``{% blocktrans %}`` |
| 394 |
--------------------------------------------------------------------------- |
| 395 |
|
| 396 |
The :ref:`contextual translation<contextual-markers>` support introduced in |
| 397 |
Django 1.3 via the ``pgettext`` function has been extended to the |
| 398 |
:ttag:`trans` and :ttag:`blocktrans` template tags using the new ``context`` |
| 399 |
keyword. |
| 400 |
|
| 401 |
Customizable ``SingleObjectMixin`` URLConf kwargs |
| 402 |
------------------------------------------------- |
| 403 |
|
| 404 |
Two new attributes, |
| 405 |
:attr:`pk_url_kwarg<django.views.generic.detail.SingleObjectMixin.pk_url_kwarg>` |
| 406 |
and |
| 407 |
:attr:`slug_url_kwarg<django.views.generic.detail.SingleObjectMixin.slug_url_kwarg>`, |
| 408 |
have been added to :class:`~django.views.generic.detail.SingleObjectMixin` to |
| 409 |
enable the customization of URLconf keyword arguments used for single |
| 410 |
object generic views. |
| 411 |
|
| 412 |
Assignment template tags |
| 413 |
------------------------ |
| 414 |
|
| 415 |
A new ``assignment_tag`` helper function was added to ``template.Library`` to |
| 416 |
ease the creation of template tags that store data in a specified context |
| 417 |
variable. |
| 418 |
|
| 419 |
``*args`` and ``**kwargs`` support for template tag helper functions |
| 420 |
-------------------------------------------------------------------- |
| 421 |
|
| 422 |
The :ref:`simple_tag<howto-custom-template-tags-simple-tags>`, |
| 423 |
:ref:`inclusion_tag <howto-custom-template-tags-inclusion-tags>` and newly |
| 424 |
introduced ``assignment_tag`` template helper functions may now accept any |
| 425 |
number of positional or keyword arguments. For example:: |
| 426 |
|
| 427 |
@register.simple_tag |
| 428 |
def my_tag(a, b, *args, **kwargs): |
| 429 |
warning = kwargs["warning"] |
| 430 |
profile = kwargs["profile"] |
| 431 |
... |
| 432 |
return ... |
| 433 |
|
| 434 |
Then, in the template, any number of arguments may be passed to the template tag. |
| 435 |
For example: |
| 436 |
|
| 437 |
.. code-block:: html+django |
| 438 |
|
| 439 |
{% my_tag 123 "abcd" book.title warning=message|lower profile=user.profile %} |
| 440 |
|
| 441 |
No wrapping of exceptions in ``TEMPLATE_DEBUG`` mode |
| 442 |
---------------------------------------------------- |
| 443 |
|
| 444 |
In previous versions of Django, whenever the ``TEMPLATE_DEBUG`` setting |
| 445 |
was ``True``, any exception raised during template rendering (even exceptions |
| 446 |
unrelated to template syntax) were wrapped in ``TemplateSyntaxError`` and |
| 447 |
re-raised. This was done in order to provide detailed template source location |
| 448 |
information in the debug 500 page. |
| 449 |
|
| 450 |
In Django 1.4, exceptions are no longer wrapped. Instead, the original |
| 451 |
exception is annotated with the source information. This means that catching |
| 452 |
exceptions from template rendering is now consistent regardless of the value of |
| 453 |
``TEMPLATE_DEBUG``, and there's no need to catch and unwrap |
| 454 |
``TemplateSyntaxError`` in order to catch other errors. |
| 455 |
|
| 456 |
``truncatechars`` template filter |
| 457 |
--------------------------------- |
| 458 |
|
| 459 |
This new filter truncates a string to be no longer than the specified |
| 460 |
number of characters. Truncated strings end with a translatable ellipsis |
| 461 |
sequence ("..."). See the documentation for :tfilter:`truncatechars` for |
| 462 |
more details. |
| 463 |
|
| 464 |
``static`` template tag |
| 465 |
----------------------- |
| 466 |
|
| 467 |
The :mod:`staticfiles<django.contrib.staticfiles>` contrib app has a new |
| 468 |
``static`` template tag to refer to files saved with the |
| 469 |
``STATICFILES_STORAGE`` storage backend. It uses the storage backend's |
| 470 |
``url`` method and therefore supports advanced features such as :ref:`serving |
| 471 |
files from a cloud service<staticfiles-from-cdn>`. |
| 472 |
|
| 473 |
``CachedStaticFilesStorage`` storage backend |
| 474 |
-------------------------------------------- |
| 475 |
|
| 476 |
The :mod:`staticfiles<django.contrib.staticfiles>` contrib app now has a |
| 477 |
``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` backend |
| 478 |
that caches the files it saves (when running the :djadmin:`collectstatic` |
| 479 |
management command) by appending the MD5 hash of the file's content to the |
| 480 |
filename. For example, the file ``css/styles.css`` would also be saved as |
| 481 |
``css/styles.55e7cbb9ba48.css`` |
| 482 |
|
| 483 |
Simple clickjacking protection |
| 484 |
------------------------------ |
| 485 |
|
| 486 |
We've added a middleware to provide easy protection against `clickjacking |
| 487 |
<https://en.wikipedia.org/wiki/Clickjacking>`_ using the ``X-Frame-Options`` |
| 488 |
header. It's not enabled by default for backwards compatibility reasons, but |
| 489 |
you'll almost certainly want to :doc:`enable it </ref/clickjacking/>` to help |
| 490 |
plug that security hole for browsers that support the header. |
| 491 |
|
| 492 |
CSRF improvements |
| 493 |
----------------- |
| 494 |
|
| 495 |
We've made various improvements to our CSRF features, including the |
| 496 |
:func:`~django.views.decorators.csrf.ensure_csrf_cookie` decorator, which can |
| 497 |
help with AJAX-heavy sites; protection for PUT and DELETE requests; and the |
| 498 |
:setting:`CSRF_COOKIE_SECURE` and :setting:`CSRF_COOKIE_PATH` settings, which can |
| 499 |
improve the security and usefulness of CSRF protection. See the :doc:`CSRF |
| 500 |
docs </ref/csrf>` for more information. |
| 501 |
|
| 502 |
Error report filtering |
| 503 |
---------------------- |
| 504 |
|
| 505 |
We added two function decorators, |
| 506 |
:func:`~django.views.decorators.debug.sensitive_variables` and |
| 507 |
:func:`~django.views.decorators.debug.sensitive_post_parameters`, to allow |
| 508 |
designating the local variables and POST parameters that may contain sensitive |
| 509 |
information and should be filtered out of error reports. |
| 510 |
|
| 511 |
All POST parameters are now systematically filtered out of error reports for |
| 512 |
certain views (``login``, ``password_reset_confirm``, ``password_change`` and |
| 513 |
``add_view`` in :mod:`django.contrib.auth.views`, as well as |
| 514 |
``user_change_password`` in the admin app) to prevent the leaking of sensitive |
| 515 |
information such as user passwords. |
| 516 |
|
| 517 |
You can override or customize the default filtering by writing a :ref:`custom |
| 518 |
filter<custom-error-reports>`. For more information see the docs on |
| 519 |
:ref:`Filtering error reports<filtering-error-reports>`. |
| 520 |
|
| 521 |
Extended IPv6 support |
| 522 |
--------------------- |
| 523 |
|
| 524 |
Django 1.4 can now better handle IPv6 addresses with the new |
| 525 |
:class:`~django.db.models.GenericIPAddressField` model field, |
| 526 |
:class:`~django.forms.GenericIPAddressField` form field and |
| 527 |
the validators :data:`~django.core.validators.validate_ipv46_address` and |
| 528 |
:data:`~django.core.validators.validate_ipv6_address`. |
| 529 |
|
| 530 |
HTML comparisons in tests |
| 531 |
------------------------- |
| 532 |
|
| 533 |
The base classes in :mod:`django.test` now have some helpers to |
| 534 |
compare HTML without tripping over irrelevant differences in whitespace, |
| 535 |
argument quoting/ordering and closing of self-closing tags. You can either |
| 536 |
compare HTML directly with the new |
| 537 |
:meth:`~django.test.SimpleTestCase.assertHTMLEqual` and |
| 538 |
:meth:`~django.test.SimpleTestCase.assertHTMLNotEqual` assertions, or use |
| 539 |
the ``html=True`` flag with |
| 540 |
:meth:`~django.test.SimpleTestCase.assertContains` and |
| 541 |
:meth:`~django.test.SimpleTestCase.assertNotContains` to test whether the |
| 542 |
client's response contains a given HTML fragment. See the :ref:`assertions |
| 543 |
documentation <assertions>` for more. |
| 544 |
|
| 545 |
Two new date format strings |
| 546 |
--------------------------- |
| 547 |
|
| 548 |
Two new :tfilter:`date` formats were added for use in template filters, |
| 549 |
template tags and :doc:`/topics/i18n/formatting`: |
| 550 |
|
| 551 |
- ``e`` -- the name of the timezone of the given datetime object |
| 552 |
- ``o`` -- the ISO 8601 year number |
| 553 |
|
| 554 |
Please make sure to update your :ref:`custom format files |
| 555 |
<custom-format-files>` if they contain either ``e`` or ``o`` in a format |
| 556 |
string. For example a Spanish localization format previously only escaped the |
| 557 |
``d`` format character:: |
| 558 |
|
| 559 |
DATE_FORMAT = r"j \de F \de Y" |
| 560 |
|
| 561 |
But now it needs to also escape ``e`` and ``o``:: |
| 562 |
|
| 563 |
DATE_FORMAT = r"j \d\e F \d\e Y" |
| 564 |
|
| 565 |
For more information, see the :tfilter:`date` documentation. |
| 566 |
|
| 567 |
Minor features |
| 568 |
-------------- |
| 569 |
|
| 570 |
Django 1.4 also includes several smaller improvements worth noting: |
| 571 |
|
| 572 |
* A more usable stacktrace in the technical 500 page. Frames in the |
| 573 |
stack trace that reference Django's framework code are dimmed out, |
| 574 |
while frames in application code are slightly emphasized. This change |
| 575 |
makes it easier to scan a stacktrace for issues in application code. |
| 576 |
|
| 577 |
* :doc:`Tablespace support </topics/db/tablespaces>` in PostgreSQL. |
| 578 |
|
| 579 |
* Customizable names for :meth:`~django.template.Library.simple_tag`. |
| 580 |
|
| 581 |
* In the documentation, a helpful :doc:`security overview </topics/security>` |
| 582 |
page. |
| 583 |
|
| 584 |
* The ``django.contrib.auth.models.check_password`` function has been moved |
| 585 |
to the :mod:`django.contrib.auth.hashers` module. Importing it from the old |
| 586 |
location will still work, but you should update your imports. |
| 587 |
|
| 588 |
* The :djadmin:`collectstatic` management command now has a ``--clear`` option |
| 589 |
to delete all files at the destination before copying or linking the static |
| 590 |
files. |
| 591 |
|
| 592 |
* It's now possible to load fixtures containing forward references when using |
| 593 |
MySQL with the InnoDB database engine. |
| 594 |
|
| 595 |
* A new 403 response handler has been added as |
| 596 |
``'django.views.defaults.permission_denied'``. You can set your own handler by |
| 597 |
setting the value of :data:`django.conf.urls.handler403`. See the |
| 598 |
documentation about :ref:`the 403 (HTTP Forbidden) view<http_forbidden_view>` |
| 599 |
for more information. |
| 600 |
|
| 601 |
* The :djadmin:`makemessages` command uses a new and more accurate lexer, |
| 602 |
:pypi:`JsLex <jslex>`, for extracting translatable strings from JavaScript |
| 603 |
files. |
| 604 |
|
| 605 |
* The :ttag:`trans` template tag now takes an optional ``as`` argument to |
| 606 |
be able to retrieve a translation string without displaying it but setting |
| 607 |
a template context variable instead. |
| 608 |
|
| 609 |
* The :ttag:`if` template tag now supports ``{% elif %}`` clauses. |
| 610 |
|
| 611 |
* If your Django app is behind a proxy, you might find the new |
| 612 |
:setting:`SECURE_PROXY_SSL_HEADER` setting useful. It solves the problem of your |
| 613 |
proxy "eating" the fact that a request came in via HTTPS. But only use this |
| 614 |
setting if you know what you're doing. |
| 615 |
|
| 616 |
* A new, plain-text, version of the HTTP 500 status code internal error page |
| 617 |
served when :setting:`DEBUG` is ``True`` is now sent to the client when |
| 618 |
Django detects that the request has originated in JavaScript code. |
| 619 |
(``is_ajax()`` is used for this.) |
| 620 |
|
| 621 |
Like its HTML counterpart, it contains a collection of different |
| 622 |
pieces of information about the state of the application. |
| 623 |
|
| 624 |
This should make it easier to read when debugging interaction with |
| 625 |
client-side JavaScript. |
| 626 |
|
| 627 |
* Added the :option:`makemessages --no-location` option. |
| 628 |
|
| 629 |
* Changed the ``locmem`` cache backend to use |
| 630 |
``pickle.HIGHEST_PROTOCOL`` for better compatibility with the other |
| 631 |
cache backends. |
| 632 |
|
| 633 |
* Added support in the ORM for generating ``SELECT`` queries containing |
| 634 |
``DISTINCT ON``. |
| 635 |
|
| 636 |
The ``distinct()`` ``QuerySet`` method now accepts an optional list of model |
| 637 |
field names. If specified, then the ``DISTINCT`` statement is limited to these |
| 638 |
fields. This is only supported in PostgreSQL. |
| 639 |
|
| 640 |
For more details, see the documentation for |
| 641 |
:meth:`~django.db.models.query.QuerySet.distinct`. |
| 642 |
|
| 643 |
* The admin login page will add a password reset link if you include a URL with |
| 644 |
the name ``'admin_password_reset'`` in your ``urls.py``, so plugging in the |
| 645 |
built-in password reset mechanism and making it available is now much easier. |
| 646 |
For details, see :ref:`auth_password_reset`. |
| 647 |
|
| 648 |
* The MySQL database backend can now make use of the savepoint feature |
| 649 |
implemented by MySQL version 5.0.3 or newer with the InnoDB storage engine. |
| 650 |
|
| 651 |
* It's now possible to pass initial values to the model forms that are part of |
| 652 |
both model formsets and inline model formsets as returned from factory |
| 653 |
functions ``modelformset_factory`` and ``inlineformset_factory`` respectively |
| 654 |
just like with regular formsets. However, initial values only apply to extra |
| 655 |
forms, i.e. those which are not bound to an existing model instance. |
| 656 |
|
| 657 |
* The sitemaps framework can now handle HTTPS links using the new |
| 658 |
:attr:`Sitemap.protocol <django.contrib.sitemaps.Sitemap.protocol>` class |
| 659 |
attribute. |
| 660 |
|
| 661 |
* A new :class:`django.test.SimpleTestCase` subclass of |
| 662 |
:class:`unittest.TestCase` |
| 663 |
that's lighter than :class:`django.test.TestCase` and company. It can be |
| 664 |
useful in tests that don't need to hit a database. See |
| 665 |
:ref:`testcase_hierarchy_diagram`. |
| 666 |
|
| 667 |
.. _backwards-incompatible-1.4: |
| 668 |
|
| 669 |
Backwards incompatible changes in 1.4 |
| 670 |
===================================== |
| 671 |
|
| 672 |
SECRET_KEY setting is required |
| 673 |
------------------------------ |
| 674 |
|
| 675 |
Running Django with an empty or known :setting:`SECRET_KEY` disables many of |
| 676 |
Django's security protections and can lead to remote-code-execution |
| 677 |
vulnerabilities. No Django site should ever be run without a |
| 678 |
:setting:`SECRET_KEY`. |
| 679 |
|
| 680 |
In Django 1.4, starting Django with an empty :setting:`SECRET_KEY` will raise a |
| 681 |
``DeprecationWarning``. In Django 1.5, it will raise an exception and Django |
| 682 |
will refuse to start. This is slightly accelerated from the usual deprecation |
| 683 |
path due to the severity of the consequences of running Django with no |
| 684 |
:setting:`SECRET_KEY`. |
| 685 |
|
| 686 |
``django.contrib.admin`` |
| 687 |
------------------------ |
| 688 |
|
| 689 |
The included administration app ``django.contrib.admin`` has for a long time |
| 690 |
shipped with a default set of static files such as JavaScript, images and |
| 691 |
stylesheets. Django 1.3 added a new contrib app ``django.contrib.staticfiles`` |
| 692 |
to handle such files in a generic way and defined conventions for static |
| 693 |
files included in apps. |
| 694 |
|
| 695 |
Starting in Django 1.4, the admin's static files also follow this |
| 696 |
convention, to make the files easier to deploy. In previous versions of Django, |
| 697 |
it was also common to define an ``ADMIN_MEDIA_PREFIX`` setting to point to the |
| 698 |
URL where the admin's static files live on a web server. This setting has now |
| 699 |
been deprecated and replaced by the more general setting :setting:`STATIC_URL`. |
| 700 |
Django will now expect to find the admin static files under the URL |
| 701 |
``<STATIC_URL>/admin/``. |
| 702 |
|
| 703 |
If you've previously used a URL path for ``ADMIN_MEDIA_PREFIX`` (e.g. |
| 704 |
``/media/``) simply make sure :setting:`STATIC_URL` and :setting:`STATIC_ROOT` |
| 705 |
are configured and your web server serves those files correctly. The |
| 706 |
development server continues to serve the admin files just like before. Read |
| 707 |
the :doc:`static files howto </howto/static-files/index>` for more details. |
| 708 |
|
| 709 |
If your ``ADMIN_MEDIA_PREFIX`` is set to a specific domain (e.g. |
| 710 |
``http://media.example.com/admin/``), make sure to also set your |
| 711 |
:setting:`STATIC_URL` setting to the correct URL -- for example, |
| 712 |
``http://media.example.com/``. |
| 713 |
|
| 714 |
.. warning:: |
| 715 |
|
| 716 |
If you're implicitly relying on the path of the admin static files within |
| 717 |
Django's source code, you'll need to update that path. The files were moved |
| 718 |
from :file:`django/contrib/admin/media/` to |
| 719 |
:file:`django/contrib/admin/static/admin/`. |
| 720 |
|
| 721 |
Supported browsers for the admin |
| 722 |
-------------------------------- |
| 723 |
|
| 724 |
Django hasn't had a clear policy on which browsers are supported by the |
| 725 |
admin app. Our new policy formalizes existing practices: `YUI's A-grade`_ |
| 726 |
browsers should provide a fully-functional admin experience, with the notable |
| 727 |
exception of Internet Explorer 6, which is no longer supported. |
| 728 |
|
| 729 |
Released over 10 years ago, IE6 imposes many limitations on modern web |
| 730 |
development. The practical implications of this policy are that contributors |
| 731 |
are free to improve the admin without consideration for these limitations. |
| 732 |
|
| 733 |
This new policy **has no impact** on sites you develop using Django. It only |
| 734 |
applies to the Django admin. Feel free to develop apps compatible with any |
| 735 |
range of browsers. |
| 736 |
|
| 737 |
.. _YUI's A-grade: https://github.com/yui/yui3/wiki/Graded-Browser-Support |
| 738 |
|
| 739 |
Removed admin icons |
| 740 |
------------------- |
| 741 |
|
| 742 |
As part of an effort to improve the performance and usability of the admin's |
| 743 |
change-list sorting interface and :attr:`horizontal |
| 744 |
<django.contrib.admin.ModelAdmin.filter_horizontal>` and :attr:`vertical |
| 745 |
<django.contrib.admin.ModelAdmin.filter_vertical>` "filter" widgets, some icon |
| 746 |
files were removed and grouped into two sprite files. |
| 747 |
|
| 748 |
Specifically: ``selector-add.gif``, ``selector-addall.gif``, |
| 749 |
``selector-remove.gif``, ``selector-removeall.gif``, |
| 750 |
``selector_stacked-add.gif`` and ``selector_stacked-remove.gif`` were |
| 751 |
combined into ``selector-icons.gif``; and ``arrow-up.gif`` and |
| 752 |
``arrow-down.gif`` were combined into ``sorting-icons.gif``. |
| 753 |
|
| 754 |
If you used those icons to customize the admin, then you'll need to replace |
| 755 |
them with your own icons or get the files from a previous release. |
| 756 |
|
| 757 |
CSS class names in admin forms |
| 758 |
------------------------------ |
| 759 |
|
| 760 |
To avoid conflicts with other common CSS class names (e.g. "button"), we added |
| 761 |
a prefix ("field-") to all CSS class names automatically generated from the |
| 762 |
form field names in the main admin forms, stacked inline forms and tabular |
| 763 |
inline cells. You'll need to take that prefix into account in your custom |
| 764 |
style sheets or JavaScript files if you previously used plain field names as |
| 765 |
selectors for custom styles or JavaScript transformations. |
| 766 |
|
| 767 |
Compatibility with old signed data |
| 768 |
---------------------------------- |
| 769 |
|
| 770 |
Django 1.3 changed the cryptographic signing mechanisms used in a number of |
| 771 |
places in Django. While Django 1.3 kept fallbacks that would accept hashes |
| 772 |
produced by the previous methods, these fallbacks are removed in Django 1.4. |
| 773 |
|
| 774 |
So, if you upgrade to Django 1.4 directly from 1.2 or earlier, you may |
| 775 |
lose/invalidate certain pieces of data that have been cryptographically signed |
| 776 |
using an old method. To avoid this, use Django 1.3 first for a period of time |
| 777 |
to allow the signed data to expire naturally. The affected parts are detailed |
| 778 |
below, with 1) the consequences of ignoring this advice and 2) the amount of |
| 779 |
time you need to run Django 1.3 for the data to expire or become irrelevant. |
| 780 |
|
| 781 |
* ``contrib.sessions`` data integrity check |
| 782 |
|
| 783 |
* Consequences: The user will be logged out, and session data will be lost. |
| 784 |
|
| 785 |
* Time period: Defined by :setting:`SESSION_COOKIE_AGE`. |
| 786 |
|
| 787 |
* ``contrib.auth`` password reset hash |
| 788 |
|
| 789 |
* Consequences: Password reset links from before the upgrade will not work. |
| 790 |
|
| 791 |
* Time period: Defined by ``PASSWORD_RESET_TIMEOUT_DAYS``. |
| 792 |
|
| 793 |
Form-related hashes: these have a much shorter lifetime and are relevant |
| 794 |
only for the short window where a user might fill in a form generated by the |
| 795 |
pre-upgrade Django instance and try to submit it to the upgraded Django |
| 796 |
instance: |
| 797 |
|
| 798 |
* ``contrib.comments`` form security hash |
| 799 |
|
| 800 |
* Consequences: The user will see the validation error "Security hash failed." |
| 801 |
|
| 802 |
* Time period: The amount of time you expect users to take filling out comment |
| 803 |
forms. |
| 804 |
|
| 805 |
* ``FormWizard`` security hash |
| 806 |
|
| 807 |
* Consequences: The user will see an error about the form having expired |
| 808 |
and will be sent back to the first page of the wizard, losing the data |
| 809 |
entered so far. |
| 810 |
|
| 811 |
* Time period: The amount of time you expect users to take filling out the |
| 812 |
affected forms. |
| 813 |
|
| 814 |
* CSRF check |
| 815 |
|
| 816 |
* Note: This is actually a Django 1.1 fallback, not Django 1.2, |
| 817 |
and it applies only if you're upgrading from 1.1. |
| 818 |
|
| 819 |
* Consequences: The user will see a 403 error with any CSRF-protected POST |
| 820 |
form. |
| 821 |
|
| 822 |
* Time period: The amount of time you expect user to take filling out |
| 823 |
such forms. |
| 824 |
|
| 825 |
* ``contrib.auth`` user password hash-upgrade sequence |
| 826 |
|
| 827 |
* Consequences: Each user's password will be updated to a stronger password |
| 828 |
hash when it's written to the database in 1.4. This means that if you |
| 829 |
upgrade to 1.4 and then need to downgrade to 1.3, version 1.3 won't be able |
| 830 |
to read the updated passwords. |
| 831 |
|
| 832 |
* Remedy: Set :setting:`PASSWORD_HASHERS` to use your original password |
| 833 |
hashing when you initially upgrade to 1.4. After you confirm your app works |
| 834 |
well with Django 1.4 and you won't have to roll back to 1.3, enable the new |
| 835 |
password hashes. |
| 836 |
|
| 837 |
``django.contrib.flatpages`` |
| 838 |
---------------------------- |
| 839 |
|
| 840 |
Starting in 1.4, the |
| 841 |
:class:`~django.contrib.flatpages.middleware.FlatpageFallbackMiddleware` only |
| 842 |
adds a trailing slash and redirects if the resulting URL refers to an existing |
| 843 |
flatpage. For example, requesting ``/notaflatpageoravalidurl`` in a previous |
| 844 |
version would redirect to ``/notaflatpageoravalidurl/``, which would |
| 845 |
subsequently raise a 404. Requesting ``/notaflatpageoravalidurl`` now will |
| 846 |
immediately raise a 404. |
| 847 |
|
| 848 |
Also, redirects returned by flatpages are now permanent (with 301 status code), |
| 849 |
to match the behavior of :class:`~django.middleware.common.CommonMiddleware`. |
| 850 |
|
| 851 |
Serialization of :class:`~datetime.datetime` and :class:`~datetime.time` |
| 852 |
------------------------------------------------------------------------ |
| 853 |
|
| 854 |
As a consequence of time-zone support, and according to the ECMA-262 |
| 855 |
specification, we made changes to the JSON serializer: |
| 856 |
|
| 857 |
* It includes the time zone for aware datetime objects. It raises an exception |
| 858 |
for aware time objects. |
| 859 |
* It includes milliseconds for datetime and time objects. There is still |
| 860 |
some precision loss, because Python stores microseconds (6 digits) and JSON |
| 861 |
only supports milliseconds (3 digits). However, it's better than discarding |
| 862 |
microseconds entirely. |
| 863 |
|
| 864 |
We changed the XML serializer to use the ISO8601 format for datetimes. |
| 865 |
The letter ``T`` is used to separate the date part from the time part, instead |
| 866 |
of a space. Time zone information is included in the ``[+-]HH:MM`` format. |
| 867 |
|
| 868 |
Though the serializers now use these new formats when creating fixtures, they |
| 869 |
can still load fixtures that use the old format. |
| 870 |
|
| 871 |
``supports_timezone`` changed to ``False`` for SQLite |
| 872 |
----------------------------------------------------- |
| 873 |
|
| 874 |
The database feature ``supports_timezone`` used to be ``True`` for SQLite. |
| 875 |
Indeed, if you saved an aware datetime object, SQLite stored a string that |
| 876 |
included an UTC offset. However, this offset was ignored when loading the value |
| 877 |
back from the database, which could corrupt the data. |
| 878 |
|
| 879 |
In the context of time-zone support, this flag was changed to ``False``, and |
| 880 |
datetimes are now stored without time-zone information in SQLite. When |
| 881 |
:setting:`USE_TZ` is ``False``, if you attempt to save an aware datetime |
| 882 |
object, Django raises an exception. |
| 883 |
|
| 884 |
``MySQLdb``-specific exceptions |
| 885 |
------------------------------- |
| 886 |
|
| 887 |
The MySQL backend historically has raised ``MySQLdb.OperationalError`` |
| 888 |
when a query triggered an exception. We've fixed this bug, and we now raise |
| 889 |
:exc:`django.db.DatabaseError` instead. If you were testing for |
| 890 |
``MySQLdb.OperationalError``, you'll need to update your ``except`` |
| 891 |
clauses. |
| 892 |
|
| 893 |
Database connection's thread-locality |
| 894 |
------------------------------------- |
| 895 |
|
| 896 |
``DatabaseWrapper`` objects (i.e. the connection objects referenced by |
| 897 |
``django.db.connection`` and ``django.db.connections["some_alias"]``) used to |
| 898 |
be thread-local. They are now global objects in order to be potentially shared |
| 899 |
between multiple threads. While the individual connection objects are now |
| 900 |
global, the ``django.db.connections`` dictionary referencing those objects is |
| 901 |
still thread-local. Therefore if you just use the ORM or |
| 902 |
``DatabaseWrapper.cursor()`` then the behavior is still the same as before. |
| 903 |
Note, however, that ``django.db.connection`` does not directly reference the |
| 904 |
default ``DatabaseWrapper`` object anymore and is now a proxy to access that |
| 905 |
object's attributes. If you need to access the actual ``DatabaseWrapper`` |
| 906 |
object, use ``django.db.connections[DEFAULT_DB_ALIAS]`` instead. |
| 907 |
|
| 908 |
As part of this change, all underlying SQLite connections are now enabled for |
| 909 |
potential thread-sharing (by passing the ``check_same_thread=False`` attribute |
| 910 |
to ``pysqlite``). ``DatabaseWrapper`` however preserves the previous behavior |
| 911 |
by disabling thread-sharing by default, so this does not affect any existing |
| 912 |
code that purely relies on the ORM or on ``DatabaseWrapper.cursor()``. |
| 913 |
|
| 914 |
Finally, while it's now possible to pass connections between threads, Django |
| 915 |
doesn't make any effort to synchronize access to the underlying backend. |
| 916 |
Concurrency behavior is defined by the underlying backend implementation. |
| 917 |
Check their documentation for details. |
| 918 |
|
| 919 |
``COMMENTS_BANNED_USERS_GROUP`` setting |
| 920 |
--------------------------------------- |
| 921 |
|
| 922 |
Django's comments has historically |
| 923 |
supported excluding the comments of a special user group, but we've never |
| 924 |
documented the feature properly and didn't enforce the exclusion in other parts |
| 925 |
of the app such as the template tags. To fix this problem, we removed the code |
| 926 |
from the feed class. |
| 927 |
|
| 928 |
If you rely on the feature and want to restore the old behavior, use a custom |
| 929 |
comment model manager to exclude the user group, like this:: |
| 930 |
|
| 931 |
from django.conf import settings |
| 932 |
from django.contrib.comments.managers import CommentManager |
| 933 |
|
| 934 |
|
| 935 |
class BanningCommentManager(CommentManager): |
| 936 |
def get_query_set(self): |
| 937 |
qs = super().get_query_set() |
| 938 |
if getattr(settings, "COMMENTS_BANNED_USERS_GROUP", None): |
| 939 |
where = [ |
| 940 |
"user_id NOT IN (SELECT user_id FROM auth_user_groups WHERE group_id = %s)" |
| 941 |
] |
| 942 |
params = [settings.COMMENTS_BANNED_USERS_GROUP] |
| 943 |
qs = qs.extra(where=where, params=params) |
| 944 |
return qs |
| 945 |
|
| 946 |
Save this model manager in your custom comment app (e.g., in |
| 947 |
``my_comments_app/managers.py``) and add it your custom comment app model:: |
| 948 |
|
| 949 |
from django.db import models |
| 950 |
from django.contrib.comments.models import Comment |
| 951 |
|
| 952 |
from my_comments_app.managers import BanningCommentManager |
| 953 |
|
| 954 |
|
| 955 |
class CommentWithTitle(Comment): |
| 956 |
title = models.CharField(max_length=300) |
| 957 |
|
| 958 |
objects = BanningCommentManager() |
| 959 |
|
| 960 |
``IGNORABLE_404_STARTS`` and ``IGNORABLE_404_ENDS`` settings |
| 961 |
------------------------------------------------------------ |
| 962 |
|
| 963 |
Until Django 1.3, it was possible to exclude some URLs from Django's |
| 964 |
:doc:`404 error reporting</howto/error-reporting>` by adding prefixes to |
| 965 |
``IGNORABLE_404_STARTS`` and suffixes to ``IGNORABLE_404_ENDS``. |
| 966 |
|
| 967 |
In Django 1.4, these two settings are superseded by |
| 968 |
:setting:`IGNORABLE_404_URLS`, which is a list of compiled regular |
| 969 |
expressions. Django won't send an email for 404 errors on URLs that match any |
| 970 |
of them. |
| 971 |
|
| 972 |
Furthermore, the previous settings had some rather arbitrary default values:: |
| 973 |
|
| 974 |
IGNORABLE_404_STARTS = ("/cgi-bin/", "/_vti_bin", "/_vti_inf") |
| 975 |
IGNORABLE_404_ENDS = ( |
| 976 |
"mail.pl", |
| 977 |
"mailform.pl", |
| 978 |
"mail.cgi", |
| 979 |
"mailform.cgi", |
| 980 |
"favicon.ico", |
| 981 |
".php", |
| 982 |
) |
| 983 |
|
| 984 |
It's not Django's role to decide if your website has a legacy ``/cgi-bin/`` |
| 985 |
section or a ``favicon.ico``. As a consequence, the default values of |
| 986 |
:setting:`IGNORABLE_404_URLS`, ``IGNORABLE_404_STARTS``, and |
| 987 |
``IGNORABLE_404_ENDS`` are all now empty. |
| 988 |
|
| 989 |
If you have customized ``IGNORABLE_404_STARTS`` or ``IGNORABLE_404_ENDS``, or |
| 990 |
if you want to keep the old default value, you should add the following lines |
| 991 |
in your settings file:: |
| 992 |
|
| 993 |
import re |
| 994 |
|
| 995 |
IGNORABLE_404_URLS = ( |
| 996 |
# for each <prefix> in IGNORABLE_404_STARTS |
| 997 |
re.compile(r"^<prefix>"), |
| 998 |
# for each <suffix> in IGNORABLE_404_ENDS |
| 999 |
re.compile(r"<suffix>$"), |
| 1000 |
) |
| 1001 |
|
| 1002 |
Don't forget to escape characters that have a special meaning in a regular |
| 1003 |
expression, such as periods. |
| 1004 |
|
| 1005 |
CSRF protection extended to PUT and DELETE |
| 1006 |
------------------------------------------ |
| 1007 |
|
| 1008 |
Previously, Django's :doc:`CSRF protection </ref/csrf/>` provided |
| 1009 |
protection only against POST requests. Since use of PUT and DELETE methods in |
| 1010 |
AJAX applications is becoming more common, we now protect all methods not |
| 1011 |
defined as safe by :rfc:`2616` -- i.e., we exempt GET, HEAD, OPTIONS and TRACE, |
| 1012 |
and we enforce protection on everything else. |
| 1013 |
|
| 1014 |
If you're using PUT or DELETE methods in AJAX applications, please see the |
| 1015 |
:ref:`instructions about using AJAX and CSRF <csrf-ajax>`. |
| 1016 |
|
| 1017 |
Password reset view now accepts ``subject_template_name`` |
| 1018 |
--------------------------------------------------------- |
| 1019 |
|
| 1020 |
The ``password_reset`` view in ``django.contrib.auth`` now accepts a |
| 1021 |
``subject_template_name`` parameter, which is passed to the password save form |
| 1022 |
as a keyword argument. If you are using this view with a custom password reset |
| 1023 |
form, then you will need to ensure your form's ``save()`` method accepts this |
| 1024 |
keyword argument. |
| 1025 |
|
| 1026 |
``django.core.template_loaders`` |
| 1027 |
-------------------------------- |
| 1028 |
|
| 1029 |
This was an alias to ``django.template.loader`` since 2005, and we've removed it |
| 1030 |
without emitting a warning due to the length of the deprecation. If your code |
| 1031 |
still referenced this, please use ``django.template.loader`` instead. |
| 1032 |
|
| 1033 |
``django.db.models.fields.URLField.verify_exists`` |
| 1034 |
-------------------------------------------------- |
| 1035 |
|
| 1036 |
This functionality has been removed due to intractable performance and |
| 1037 |
security issues. Any existing usage of ``verify_exists`` should be |
| 1038 |
removed. |
| 1039 |
|
| 1040 |
``django.core.files.storage.Storage.open`` |
| 1041 |
------------------------------------------ |
| 1042 |
|
| 1043 |
The ``open`` method of the base Storage class used to take an obscure parameter |
| 1044 |
``mixin`` that allowed you to dynamically change the base classes of the |
| 1045 |
returned file object. This has been removed. In the rare case you relied on the |
| 1046 |
``mixin`` parameter, you can easily achieve the same by overriding the ``open`` |
| 1047 |
method, like this:: |
| 1048 |
|
| 1049 |
from django.core.files import File |
| 1050 |
from django.core.files.storage import FileSystemStorage |
| 1051 |
|
| 1052 |
|
| 1053 |
class Spam(File): |
| 1054 |
""" |
| 1055 |
Spam, spam, spam, spam and spam. |
| 1056 |
""" |
| 1057 |
|
| 1058 |
def ham(self): |
| 1059 |
return "eggs" |
| 1060 |
|
| 1061 |
|
| 1062 |
class SpamStorage(FileSystemStorage): |
| 1063 |
""" |
| 1064 |
A custom file storage backend. |
| 1065 |
""" |
| 1066 |
|
| 1067 |
def open(self, name, mode="rb"): |
| 1068 |
return Spam(open(self.path(name), mode)) |
| 1069 |
|
| 1070 |
YAML deserializer now uses ``yaml.safe_load`` |
| 1071 |
--------------------------------------------- |
| 1072 |
|
| 1073 |
``yaml.load`` is able to construct any Python object, which may trigger |
| 1074 |
arbitrary code execution if you process a YAML document that comes from an |
| 1075 |
untrusted source. This feature isn't necessary for Django's YAML deserializer, |
| 1076 |
whose primary use is to load fixtures consisting of simple objects. Even though |
| 1077 |
fixtures are trusted data, the YAML deserializer now uses ``yaml.safe_load`` |
| 1078 |
for additional security. |
| 1079 |
|
| 1080 |
Session cookies now have the ``httponly`` flag by default |
| 1081 |
--------------------------------------------------------- |
| 1082 |
|
| 1083 |
Session cookies now include the ``httponly`` attribute by default to |
| 1084 |
help reduce the impact of potential XSS attacks. As a consequence of |
| 1085 |
this change, session cookie data, including ``sessionid``, is no longer |
| 1086 |
accessible from JavaScript in many browsers. For strict backwards |
| 1087 |
compatibility, use ``SESSION_COOKIE_HTTPONLY = False`` in your |
| 1088 |
settings file. |
| 1089 |
|
| 1090 |
The :tfilter:`urlize` filter no longer escapes every URL |
| 1091 |
-------------------------------------------------------- |
| 1092 |
|
| 1093 |
When a URL contains a ``%xx`` sequence, where ``xx`` are two hexadecimal |
| 1094 |
digits, :tfilter:`urlize` now assumes that the URL is already escaped and |
| 1095 |
doesn't apply URL escaping again. This is wrong for URLs whose unquoted form |
| 1096 |
contains a ``%xx`` sequence, but such URLs are very unlikely to happen in the |
| 1097 |
wild, because they would confuse browsers too. |
| 1098 |
|
| 1099 |
``assertTemplateUsed`` and ``assertTemplateNotUsed`` as context manager |
| 1100 |
----------------------------------------------------------------------- |
| 1101 |
|
| 1102 |
It's now possible to check whether a template was used within a block of |
| 1103 |
code with :meth:`~django.test.SimpleTestCase.assertTemplateUsed` and |
| 1104 |
:meth:`~django.test.SimpleTestCase.assertTemplateNotUsed`. And they |
| 1105 |
can be used as a context manager:: |
| 1106 |
|
| 1107 |
with self.assertTemplateUsed("index.html"): |
| 1108 |
render_to_string("index.html") |
| 1109 |
with self.assertTemplateNotUsed("base.html"): |
| 1110 |
render_to_string("index.html") |
| 1111 |
|
| 1112 |
See the :ref:`assertion documentation<assertions>` for more. |
| 1113 |
|
| 1114 |
Database connections after running the test suite |
| 1115 |
------------------------------------------------- |
| 1116 |
|
| 1117 |
The default test runner no longer restores the database connections after |
| 1118 |
tests' execution. This prevents the production database from being exposed to |
| 1119 |
potential threads that would still be running and attempting to create new |
| 1120 |
connections. |
| 1121 |
|
| 1122 |
If your code relied on connections to the production database being created |
| 1123 |
after tests' execution, then you can restore the previous behavior by |
| 1124 |
subclassing ``DjangoTestRunner`` and overriding its ``teardown_databases()`` |
| 1125 |
method. |
| 1126 |
|
| 1127 |
Output of :djadmin:`manage.py help <help>` |
| 1128 |
------------------------------------------ |
| 1129 |
|
| 1130 |
:djadmin:`manage.py help <help>` now groups available commands by application. |
| 1131 |
If you depended on the output of this command -- if you parsed it, for example |
| 1132 |
-- then you'll need to update your code. To get a list of all available |
| 1133 |
management commands in a script, use |
| 1134 |
:djadmin:`manage.py help --commands <help>` instead. |
| 1135 |
|
| 1136 |
``extends`` template tag |
| 1137 |
------------------------ |
| 1138 |
|
| 1139 |
Previously, the :ttag:`extends` tag used a buggy method of parsing arguments, |
| 1140 |
which could lead to it erroneously considering an argument as a string literal |
| 1141 |
when it wasn't. It now uses ``parser.compile_filter``, like other tags. |
| 1142 |
|
| 1143 |
The internals of the tag aren't part of the official stable API, but in the |
| 1144 |
interests of full disclosure, the ``ExtendsNode.__init__`` definition has |
| 1145 |
changed, which may break any custom tags that use this class. |
| 1146 |
|
| 1147 |
Loading some incomplete fixtures no longer works |
| 1148 |
------------------------------------------------ |
| 1149 |
|
| 1150 |
Prior to 1.4, a default value was inserted for fixture objects that were missing |
| 1151 |
a specific date or datetime value when auto_now or auto_now_add was set for the |
| 1152 |
field. This was something that should not have worked, and in 1.4 loading such |
| 1153 |
incomplete fixtures will fail. Because fixtures are a raw import, they should |
| 1154 |
explicitly specify all field values, regardless of field options on the model. |
| 1155 |
|
| 1156 |
Development Server Multithreading |
| 1157 |
--------------------------------- |
| 1158 |
|
| 1159 |
The development server is now is multithreaded by default. Use the |
| 1160 |
:option:`runserver --nothreading` option to disable the use of threading in the |
| 1161 |
development server: |
| 1162 |
|
| 1163 |
.. code-block:: shell |
| 1164 |
|
| 1165 |
django-admin.py runserver --nothreading |
| 1166 |
|
| 1167 |
Attributes disabled in markdown when safe mode set |
| 1168 |
-------------------------------------------------- |
| 1169 |
|
| 1170 |
Prior to Django 1.4, attributes were included in any markdown output regardless |
| 1171 |
of safe mode setting of the filter. With version > 2.1 of the Python-Markdown |
| 1172 |
library, an enable_attributes option was added. When the safe argument is |
| 1173 |
passed to the markdown filter, both the ``safe_mode=True`` and |
| 1174 |
``enable_attributes=False`` options are set. If using a version of the |
| 1175 |
Python-Markdown library less than 2.1, a warning is issued that the output is |
| 1176 |
insecure. |
| 1177 |
|
| 1178 |
FormMixin get_initial returns an instance-specific dictionary |
| 1179 |
------------------------------------------------------------- |
| 1180 |
|
| 1181 |
In Django 1.3, the ``get_initial`` method of the |
| 1182 |
:class:`django.views.generic.edit.FormMixin` class was returning the |
| 1183 |
class ``initial`` dictionary. This has been fixed to return a copy of this |
| 1184 |
dictionary, so form instances can modify their initial data without messing |
| 1185 |
with the class variable. |
| 1186 |
|
| 1187 |
.. _deprecated-features-1.4: |
| 1188 |
|
| 1189 |
Features deprecated in 1.4 |
| 1190 |
========================== |
| 1191 |
|
| 1192 |
Old styles of calling ``cache_page`` decorator |
| 1193 |
---------------------------------------------- |
| 1194 |
|
| 1195 |
Some legacy ways of calling :func:`~django.views.decorators.cache.cache_page` |
| 1196 |
have been deprecated. Please see the documentation for the correct way to use |
| 1197 |
this decorator. |
| 1198 |
|
| 1199 |
Support for PostgreSQL versions older than 8.2 |
| 1200 |
---------------------------------------------- |
| 1201 |
|
| 1202 |
Django 1.3 dropped support for PostgreSQL versions older than 8.0, and we |
| 1203 |
suggested using a more recent version because of performance improvements |
| 1204 |
and, more importantly, the end of upstream support periods for 8.0 and 8.1 |
| 1205 |
was near (November 2010). |
| 1206 |
|
| 1207 |
Django 1.4 takes that policy further and sets 8.2 as the minimum PostgreSQL |
| 1208 |
version it officially supports. |
| 1209 |
|
| 1210 |
Request exceptions are now always logged |
| 1211 |
---------------------------------------- |
| 1212 |
|
| 1213 |
When we added :doc:`logging support </topics/logging/>` in Django in 1.3, the |
| 1214 |
admin error email support was moved into the |
| 1215 |
:class:`django.utils.log.AdminEmailHandler`, attached to the |
| 1216 |
``'django.request'`` logger. In order to maintain the established behavior of |
| 1217 |
error emails, the ``'django.request'`` logger was called only when |
| 1218 |
:setting:`DEBUG` was ``False``. |
| 1219 |
|
| 1220 |
To increase the flexibility of error logging for requests, the |
| 1221 |
``'django.request'`` logger is now called regardless of the value of |
| 1222 |
:setting:`DEBUG`, and the default settings file for new projects now includes a |
| 1223 |
separate filter attached to :class:`django.utils.log.AdminEmailHandler` to |
| 1224 |
prevent admin error emails in ``DEBUG`` mode:: |
| 1225 |
|
| 1226 |
LOGGING = { |
| 1227 |
# ... |
| 1228 |
"filters": { |
| 1229 |
"require_debug_false": { |
| 1230 |
"()": "django.utils.log.RequireDebugFalse", |
| 1231 |
} |
| 1232 |
}, |
| 1233 |
"handlers": { |
| 1234 |
"mail_admins": { |
| 1235 |
"level": "ERROR", |
| 1236 |
"filters": ["require_debug_false"], |
| 1237 |
"class": "django.utils.log.AdminEmailHandler", |
| 1238 |
} |
| 1239 |
}, |
| 1240 |
} |
| 1241 |
|
| 1242 |
If your project was created prior to this change, your :setting:`LOGGING` |
| 1243 |
setting will not include this new filter. In order to maintain |
| 1244 |
backwards-compatibility, Django will detect that your ``'mail_admins'`` handler |
| 1245 |
configuration includes no ``'filters'`` section and will automatically add |
| 1246 |
this filter for you and issue a pending-deprecation warning. This will become a |
| 1247 |
deprecation warning in Django 1.5, and in Django 1.6 the |
| 1248 |
backwards-compatibility shim will be removed entirely. |
| 1249 |
|
| 1250 |
The existence of any ``'filters'`` key under the ``'mail_admins'`` handler will |
| 1251 |
disable this backward-compatibility shim and deprecation warning. |
| 1252 |
|
| 1253 |
``django.conf.urls.defaults`` |
| 1254 |
----------------------------- |
| 1255 |
|
| 1256 |
Until Django 1.3, the ``include()``, ``patterns()``, and ``url()`` functions, |
| 1257 |
plus :data:`~django.conf.urls.handler404` and :data:`~django.conf.urls.handler500` |
| 1258 |
were located in a ``django.conf.urls.defaults`` module. |
| 1259 |
|
| 1260 |
In Django 1.4, they live in :mod:`django.conf.urls`. |
| 1261 |
|
| 1262 |
``django.contrib.databrowse`` |
| 1263 |
----------------------------- |
| 1264 |
|
| 1265 |
Databrowse has not seen active development for some time, and this does not show |
| 1266 |
any sign of changing. There had been a suggestion for a `GSOC project`_ to |
| 1267 |
integrate the functionality of databrowse into the admin, but no progress was |
| 1268 |
made. While Databrowse has been deprecated, an enhancement of |
| 1269 |
``django.contrib.admin`` providing a similar feature set is still possible. |
| 1270 |
|
| 1271 |
.. _GSOC project: https://code.djangoproject.com/wiki/SummerOfCode2011#Integratedatabrowseintotheadmin |
| 1272 |
|
| 1273 |
The code that powers Databrowse is licensed under the same terms as Django |
| 1274 |
itself, so it's available to be adopted by an individual or group as |
| 1275 |
a third-party project. |
| 1276 |
|
| 1277 |
``django.core.management.setup_environ`` |
| 1278 |
---------------------------------------- |
| 1279 |
|
| 1280 |
This function temporarily modified ``sys.path`` in order to make the parent |
| 1281 |
"project" directory importable under the old flat :djadmin:`startproject` |
| 1282 |
layout. This function is now deprecated, as its path workarounds are no longer |
| 1283 |
needed with the new ``manage.py`` and default project layout. |
| 1284 |
|
| 1285 |
This function was never documented or part of the public API, but it was widely |
| 1286 |
recommended for use in setting up a "Django environment" for a user script. |
| 1287 |
These uses should be replaced by setting the :envvar:`DJANGO_SETTINGS_MODULE` |
| 1288 |
environment variable or using :func:`django.conf.settings.configure`. |
| 1289 |
|
| 1290 |
``django.core.management.execute_manager`` |
| 1291 |
------------------------------------------ |
| 1292 |
|
| 1293 |
This function was previously used by ``manage.py`` to execute a management |
| 1294 |
command. It is identical to |
| 1295 |
``django.core.management.execute_from_command_line``, except that it first |
| 1296 |
calls ``setup_environ``, which is now deprecated. As such, ``execute_manager`` |
| 1297 |
is also deprecated; ``execute_from_command_line`` can be used instead. Neither |
| 1298 |
of these functions is documented as part of the public API, but a deprecation |
| 1299 |
path is needed due to use in existing ``manage.py`` files. |
| 1300 |
|
| 1301 |
``is_safe`` and ``needs_autoescape`` attributes of template filters |
| 1302 |
------------------------------------------------------------------- |
| 1303 |
|
| 1304 |
Two flags, ``is_safe`` and ``needs_autoescape``, define how each template filter |
| 1305 |
interacts with Django's auto-escaping behavior. They used to be attributes of |
| 1306 |
the filter function:: |
| 1307 |
|
| 1308 |
@register.filter |
| 1309 |
def noop(value): |
| 1310 |
return value |
| 1311 |
|
| 1312 |
|
| 1313 |
noop.is_safe = True |
| 1314 |
|
| 1315 |
However, this technique caused some problems in combination with decorators, |
| 1316 |
especially :func:`@stringfilter <django.template.defaultfilters.stringfilter>`. |
| 1317 |
Now, the flags are keyword arguments of :meth:`@register.filter |
| 1318 |
<django.template.Library.filter>`:: |
| 1319 |
|
| 1320 |
@register.filter(is_safe=True) |
| 1321 |
def noop(value): |
| 1322 |
return value |
| 1323 |
|
| 1324 |
See :ref:`filters and auto-escaping <filters-auto-escaping>` for more information. |
| 1325 |
|
| 1326 |
Wildcard expansion of application names in ``INSTALLED_APPS`` |
| 1327 |
------------------------------------------------------------- |
| 1328 |
|
| 1329 |
Until Django 1.3, :setting:`INSTALLED_APPS` accepted wildcards in application |
| 1330 |
names, like ``django.contrib.*``. The expansion was performed by a |
| 1331 |
filesystem-based implementation of ``from <package> import *``. Unfortunately, |
| 1332 |
this can't be done reliably. |
| 1333 |
|
| 1334 |
This behavior was never documented. Since it is unpythonic, it was removed in |
| 1335 |
Django 1.4. If you relied on it, you must edit your settings file to list all |
| 1336 |
your applications explicitly. |
| 1337 |
|
| 1338 |
``HttpRequest.raw_post_data`` renamed to ``HttpRequest.body`` |
| 1339 |
------------------------------------------------------------- |
| 1340 |
|
| 1341 |
This attribute was confusingly named ``HttpRequest.raw_post_data``, but it |
| 1342 |
actually provided the body of the HTTP request. It's been renamed to |
| 1343 |
``HttpRequest.body``, and ``HttpRequest.raw_post_data`` has been deprecated. |
| 1344 |
|
| 1345 |
``django.contrib.sitemaps`` bug fix with potential performance implications |
| 1346 |
--------------------------------------------------------------------------- |
| 1347 |
|
| 1348 |
In previous versions, ``Paginator`` objects used in sitemap classes were |
| 1349 |
cached, which could result in stale site maps. We've removed the caching, so |
| 1350 |
each request to a site map now creates a new Paginator object and calls the |
| 1351 |
:attr:`~django.contrib.sitemaps.Sitemap.items()` method of the |
| 1352 |
:class:`~django.contrib.sitemaps.Sitemap` subclass. Depending on what your |
| 1353 |
``items()`` method is doing, this may have a negative performance impact. |
| 1354 |
To mitigate the performance impact, consider using the :doc:`caching |
| 1355 |
framework </topics/cache>` within your ``Sitemap`` subclass. |
| 1356 |
|
| 1357 |
Versions of Python-Markdown earlier than 2.1 |
| 1358 |
-------------------------------------------- |
| 1359 |
|
| 1360 |
Versions of Python-Markdown earlier than 2.1 do not support the option to |
| 1361 |
disable attributes. As a security issue, earlier versions of this library will |
| 1362 |
not be supported by the markup contrib app in 1.5 under an accelerated |
| 1363 |
deprecation timeline. |