Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- testbed/django__django/docs/releases/1.6.3.txt +183 -0
- testbed/django__django/docs/releases/1.8.1.txt +97 -0
- testbed/django__django/docs/releases/1.9.2.txt +105 -0
- testbed/django__django/docs/releases/1.9.4.txt +8 -0
- testbed/django__django/docs/releases/2.0.1.txt +45 -0
- testbed/django__django/docs/releases/2.0.5.txt +25 -0
- testbed/django__django/docs/releases/2.1.12.txt +15 -0
- testbed/django__django/docs/releases/2.1.14.txt +15 -0
- testbed/django__django/docs/releases/2.1.15.txt +53 -0
- testbed/django__django/docs/releases/2.1.2.txt +40 -0
- testbed/django__django/docs/releases/2.1.4.txt +28 -0
- testbed/django__django/docs/releases/2.1.7.txt +12 -0
- testbed/django__django/docs/releases/2.1.8.txt +14 -0
- testbed/django__django/docs/releases/2.1.9.txt +32 -0
- testbed/django__django/docs/releases/2.1.txt +483 -0
- testbed/django__django/docs/releases/2.2.10.txt +13 -0
- testbed/django__django/docs/releases/2.2.13.txt +33 -0
- testbed/django__django/docs/releases/2.2.15.txt +16 -0
- testbed/django__django/docs/releases/2.2.16.txt +36 -0
- testbed/django__django/docs/releases/2.2.17.txt +7 -0
- testbed/django__django/docs/releases/2.2.18.txt +15 -0
- testbed/django__django/docs/releases/2.2.19.txt +16 -0
- testbed/django__django/docs/releases/2.2.20.txt +15 -0
- testbed/django__django/docs/releases/2.2.23.txt +15 -0
- testbed/django__django/docs/releases/2.2.3.txt +38 -0
- testbed/django__django/docs/releases/2.2.5.txt +24 -0
- testbed/django__django/docs/releases/2.2.7.txt +26 -0
- testbed/django__django/docs/releases/3.0.10.txt +36 -0
- testbed/django__django/docs/releases/3.0.6.txt +14 -0
- testbed/django__django/docs/releases/3.1.1.txt +88 -0
- testbed/django__django/docs/releases/3.1.3.txt +62 -0
- testbed/django__django/docs/releases/3.1.6.txt +21 -0
- testbed/django__django/docs/releases/3.1.txt +866 -0
- testbed/django__django/docs/releases/3.2.txt +791 -0
- testbed/django__django/docs/releases/4.0.txt +773 -0
- testbed/django__django/docs/releases/4.1.txt +753 -0
- testbed/django__django/docs/releases/4.2.4.txt +12 -0
- testbed/django__django/docs/releases/4.2.txt +610 -0
- testbed/django__django/docs/releases/5.0.txt +646 -0
- testbed/django__django/docs/releases/index.txt +471 -0
- testbed/django__django/docs/releases/security.txt +1422 -0
- testbed/django__django/docs/topics/auth/customizing.txt +1213 -0
- testbed/django__django/docs/topics/auth/default.txt +1884 -0
- testbed/django__django/docs/topics/auth/index.txt +89 -0
- testbed/django__django/docs/topics/auth/passwords.txt +755 -0
- testbed/django__django/docs/topics/cache.txt +1514 -0
- testbed/django__django/docs/topics/class-based-views/generic-display.txt +438 -0
- testbed/django__django/docs/topics/class-based-views/generic-editing.txt +275 -0
- testbed/django__django/docs/topics/class-based-views/index.txt +165 -0
- testbed/django__django/docs/topics/class-based-views/intro.txt +327 -0
testbed/django__django/docs/releases/1.6.3.txt
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 1.6.3 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*April 21, 2014*
|
| 6 |
+
|
| 7 |
+
Django 1.6.3 fixes several bugs in 1.6.2, including three security issues,
|
| 8 |
+
and makes one backwards-incompatible change:
|
| 9 |
+
|
| 10 |
+
Unexpected code execution using ``reverse()``
|
| 11 |
+
=============================================
|
| 12 |
+
|
| 13 |
+
Django's URL handling is based on a mapping of regex patterns
|
| 14 |
+
(representing the URLs) to callable views, and Django's own processing
|
| 15 |
+
consists of matching a requested URL against those patterns to
|
| 16 |
+
determine the appropriate view to invoke.
|
| 17 |
+
|
| 18 |
+
Django also provides a convenience function -- ``reverse()`` -- which performs
|
| 19 |
+
this process in the opposite direction. The ``reverse()`` function takes
|
| 20 |
+
information about a view and returns a URL which would invoke that view. Use
|
| 21 |
+
of ``reverse()`` is encouraged for application developers, as the output of
|
| 22 |
+
``reverse()`` is always based on the current URL patterns, meaning developers
|
| 23 |
+
do not need to change other code when making changes to URLs.
|
| 24 |
+
|
| 25 |
+
One argument signature for ``reverse()`` is to pass a dotted Python
|
| 26 |
+
path to the desired view. In this situation, Django will import the
|
| 27 |
+
module indicated by that dotted path as part of generating the
|
| 28 |
+
resulting URL. If such a module has import-time side effects, those
|
| 29 |
+
side effects will occur.
|
| 30 |
+
|
| 31 |
+
Thus it is possible for an attacker to cause unexpected code
|
| 32 |
+
execution, given the following conditions:
|
| 33 |
+
|
| 34 |
+
1. One or more views are present which construct a URL based on user
|
| 35 |
+
input (commonly, a "next" parameter in a querystring indicating
|
| 36 |
+
where to redirect upon successful completion of an action).
|
| 37 |
+
|
| 38 |
+
2. One or more modules are known to an attacker to exist on the
|
| 39 |
+
server's Python import path, which perform code execution with side
|
| 40 |
+
effects on importing.
|
| 41 |
+
|
| 42 |
+
To remedy this, ``reverse()`` will now only accept and import dotted
|
| 43 |
+
paths based on the view-containing modules listed in the project's :doc:`URL
|
| 44 |
+
pattern configuration </topics/http/urls>`, so as to ensure that only modules
|
| 45 |
+
the developer intended to be imported in this fashion can or will be imported.
|
| 46 |
+
|
| 47 |
+
Caching of anonymous pages could reveal CSRF token
|
| 48 |
+
==================================================
|
| 49 |
+
|
| 50 |
+
Django includes both a :doc:`caching framework </topics/cache>` and a system
|
| 51 |
+
for :doc:`preventing cross-site request forgery (CSRF) attacks
|
| 52 |
+
</ref/csrf/>`. The CSRF-protection system is based on a random nonce
|
| 53 |
+
sent to the client in a cookie which must be sent by the client on future
|
| 54 |
+
requests and, in forms, a hidden value which must be submitted back with the
|
| 55 |
+
form.
|
| 56 |
+
|
| 57 |
+
The caching framework includes an option to cache responses to
|
| 58 |
+
anonymous (i.e., unauthenticated) clients.
|
| 59 |
+
|
| 60 |
+
When the first anonymous request to a given page is by a client which
|
| 61 |
+
did not have a CSRF cookie, the cache framework will also cache the
|
| 62 |
+
CSRF cookie and serve the same nonce to other anonymous clients who
|
| 63 |
+
do not have a CSRF cookie. This can allow an attacker to obtain a
|
| 64 |
+
valid CSRF cookie value and perform attacks which bypass the check for
|
| 65 |
+
the cookie.
|
| 66 |
+
|
| 67 |
+
To remedy this, the caching framework will no longer cache such
|
| 68 |
+
responses. The heuristic for this will be:
|
| 69 |
+
|
| 70 |
+
1. If the incoming request did not submit any cookies, and
|
| 71 |
+
|
| 72 |
+
2. If the response did send one or more cookies, and
|
| 73 |
+
|
| 74 |
+
3. If the ``Vary: Cookie`` header is set on the response, then the
|
| 75 |
+
response will not be cached.
|
| 76 |
+
|
| 77 |
+
MySQL typecasting
|
| 78 |
+
=================
|
| 79 |
+
|
| 80 |
+
The MySQL database is known to "typecast" on certain queries; for
|
| 81 |
+
example, when querying a table which contains string values, but using
|
| 82 |
+
a query which filters based on an integer value, MySQL will first
|
| 83 |
+
silently coerce the strings to integers and return a result based on that.
|
| 84 |
+
|
| 85 |
+
If a query is performed without first converting values to the
|
| 86 |
+
appropriate type, this can produce unexpected results, similar to what
|
| 87 |
+
would occur if the query itself had been manipulated.
|
| 88 |
+
|
| 89 |
+
Django's model field classes are aware of their own types and most
|
| 90 |
+
such classes perform explicit conversion of query arguments to the
|
| 91 |
+
correct database-level type before querying. However, three model
|
| 92 |
+
field classes did not correctly convert their arguments:
|
| 93 |
+
|
| 94 |
+
* :class:`~django.db.models.FilePathField`
|
| 95 |
+
* :class:`~django.db.models.GenericIPAddressField`
|
| 96 |
+
* ``IPAddressField``
|
| 97 |
+
|
| 98 |
+
These three fields have been updated to convert their arguments to the
|
| 99 |
+
correct types before querying.
|
| 100 |
+
|
| 101 |
+
Additionally, developers of custom model fields are now warned via
|
| 102 |
+
documentation to ensure their custom field classes will perform
|
| 103 |
+
appropriate type conversions, and users of the :meth:`raw()
|
| 104 |
+
<django.db.models.query.QuerySet.raw>` and :meth:`extra()
|
| 105 |
+
<django.db.models.query.QuerySet.extra>` query methods -- which allow the
|
| 106 |
+
developer to supply raw SQL or SQL fragments -- will be advised to ensure they
|
| 107 |
+
perform appropriate manual type conversions prior to executing queries.
|
| 108 |
+
|
| 109 |
+
``select_for_update()`` requires a transaction
|
| 110 |
+
==============================================
|
| 111 |
+
|
| 112 |
+
Historically, queries that use
|
| 113 |
+
:meth:`~django.db.models.query.QuerySet.select_for_update()` could be
|
| 114 |
+
executed in autocommit mode, outside of a transaction. Before Django
|
| 115 |
+
1.6, Django's automatic transactions mode allowed this to be used to
|
| 116 |
+
lock records until the next write operation. Django 1.6 introduced
|
| 117 |
+
database-level autocommit; since then, execution in such a context
|
| 118 |
+
voids the effect of ``select_for_update()``. It is, therefore, assumed
|
| 119 |
+
now to be an error and raises an exception.
|
| 120 |
+
|
| 121 |
+
This change was made because such errors can be caused by including an
|
| 122 |
+
app which expects global transactions (e.g. :setting:`ATOMIC_REQUESTS
|
| 123 |
+
<DATABASE-ATOMIC_REQUESTS>` set to ``True``), or Django's old autocommit
|
| 124 |
+
behavior, in a project which runs without them; and further, such
|
| 125 |
+
errors may manifest as data-corruption bugs.
|
| 126 |
+
|
| 127 |
+
This change may cause test failures if you use ``select_for_update()``
|
| 128 |
+
in a test class which is a subclass of
|
| 129 |
+
:class:`~django.test.TransactionTestCase` rather than
|
| 130 |
+
:class:`~django.test.TestCase`.
|
| 131 |
+
|
| 132 |
+
Other bugfixes and changes
|
| 133 |
+
==========================
|
| 134 |
+
|
| 135 |
+
* Content retrieved from the GeoIP library is now properly decoded from its
|
| 136 |
+
default ``iso-8859-1`` encoding
|
| 137 |
+
(:ticket:`21996`).
|
| 138 |
+
|
| 139 |
+
* Fixed ``AttributeError`` when using
|
| 140 |
+
:meth:`~django.db.models.query.QuerySet.bulk_create` with ``ForeignObject``
|
| 141 |
+
(:ticket:`21566`).
|
| 142 |
+
|
| 143 |
+
* Fixed crash of ``QuerySet``\s that use ``F() + timedelta()`` when their query
|
| 144 |
+
was compiled more once
|
| 145 |
+
(:ticket:`21643`).
|
| 146 |
+
|
| 147 |
+
* Prevented custom ``widget`` class attribute of
|
| 148 |
+
:class:`~django.forms.IntegerField` subclasses from being overwritten by the
|
| 149 |
+
code in their ``__init__`` method
|
| 150 |
+
(:ticket:`22245`).
|
| 151 |
+
|
| 152 |
+
* Improved :func:`~django.utils.html.strip_tags` accuracy (but it still cannot
|
| 153 |
+
guarantee an HTML-safe result, as stated in the documentation).
|
| 154 |
+
|
| 155 |
+
* Fixed a regression in the :mod:`django.contrib.gis` SQL compiler for
|
| 156 |
+
non-concrete fields (:ticket:`22250`).
|
| 157 |
+
|
| 158 |
+
* Fixed :attr:`ModelAdmin.preserve_filters
|
| 159 |
+
<django.contrib.admin.ModelAdmin.preserve_filters>` when running a site with
|
| 160 |
+
a URL prefix (:ticket:`21795`).
|
| 161 |
+
|
| 162 |
+
* Fixed a crash in the ``find_command`` management utility when the ``PATH``
|
| 163 |
+
environment variable wasn't set
|
| 164 |
+
(:ticket:`22256`).
|
| 165 |
+
|
| 166 |
+
* Fixed :djadmin:`changepassword` on Windows
|
| 167 |
+
(:ticket:`22364`).
|
| 168 |
+
|
| 169 |
+
* Avoided shadowing deadlock exceptions on MySQL
|
| 170 |
+
(:ticket:`22291`).
|
| 171 |
+
|
| 172 |
+
* Wrapped database exceptions in ``_set_autocommit``
|
| 173 |
+
(:ticket:`22321`).
|
| 174 |
+
|
| 175 |
+
* Fixed atomicity when closing a database connection or when the database server
|
| 176 |
+
disconnects (:ticket:`21239` and :ticket:`21202`)
|
| 177 |
+
|
| 178 |
+
* Fixed regression in ``prefetch_related`` that caused the related objects
|
| 179 |
+
query to include an unnecessary join
|
| 180 |
+
(:ticket:`21760`).
|
| 181 |
+
|
| 182 |
+
Additionally, Django's vendored version of six, ``django.utils.six`` has been
|
| 183 |
+
upgraded to the latest release (1.6.1).
|
testbed/django__django/docs/releases/1.8.1.txt
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 1.8.1 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*May 1, 2015*
|
| 6 |
+
|
| 7 |
+
Django 1.8.1 fixes several bugs in 1.8 and includes some optimizations in the
|
| 8 |
+
migrations framework.
|
| 9 |
+
|
| 10 |
+
Bugfixes
|
| 11 |
+
========
|
| 12 |
+
|
| 13 |
+
* Added support for serializing :class:`~datetime.timedelta` objects in
|
| 14 |
+
migrations (:ticket:`24566`).
|
| 15 |
+
|
| 16 |
+
* Restored proper parsing of the :djadmin:`testserver` command's positional
|
| 17 |
+
arguments (fixture names) (:ticket:`24571`).
|
| 18 |
+
|
| 19 |
+
* Prevented ``TypeError`` in translation functions ``check_for_language()`` and
|
| 20 |
+
``get_language_bidi()`` when translations are deactivated (:ticket:`24569`).
|
| 21 |
+
|
| 22 |
+
* Fixed :djadmin:`squashmigrations` command when using
|
| 23 |
+
:class:`~django.db.migrations.operations.SeparateDatabaseAndState`
|
| 24 |
+
(:ticket:`24278`).
|
| 25 |
+
|
| 26 |
+
* Stripped microseconds from ``datetime`` values when using an older version of
|
| 27 |
+
the MySQLdb DB API driver as it does not support fractional seconds
|
| 28 |
+
(:ticket:`24584`).
|
| 29 |
+
|
| 30 |
+
* Fixed a migration crash when altering
|
| 31 |
+
:class:`~django.db.models.ManyToManyField`\s (:ticket:`24513`).
|
| 32 |
+
|
| 33 |
+
* Fixed a crash with ``QuerySet.update()`` on foreign keys to one-to-one fields
|
| 34 |
+
(:ticket:`24578`).
|
| 35 |
+
|
| 36 |
+
* Fixed a regression in the model detail view of
|
| 37 |
+
:mod:`~django.contrib.admindocs` when a model has a reverse foreign key
|
| 38 |
+
relation (:ticket:`24624`).
|
| 39 |
+
|
| 40 |
+
* Prevented arbitrary file inclusions in :mod:`~django.contrib.admindocs`
|
| 41 |
+
(:ticket:`24625`).
|
| 42 |
+
|
| 43 |
+
* Fixed a crash with ``QuerySet.update()`` on foreign keys to instances with
|
| 44 |
+
``uuid`` primary keys (:ticket:`24611`).
|
| 45 |
+
|
| 46 |
+
* Fixed database introspection with SQLite 3.8.9 (released April 8, 2015)
|
| 47 |
+
(:ticket:`24637`).
|
| 48 |
+
|
| 49 |
+
* Updated ``urlpatterns`` examples generated by :djadmin:`startproject` to
|
| 50 |
+
remove usage of referencing views by dotted path in
|
| 51 |
+
``django.conf.urls.url()`` which is deprecated in Django 1.8
|
| 52 |
+
(:ticket:`24635`).
|
| 53 |
+
|
| 54 |
+
* Fixed queries where an expression was referenced in ``order_by()``, but wasn't
|
| 55 |
+
part of the select clause. An example query is
|
| 56 |
+
``qs.annotate(foo=F('field')).values('pk').order_by('foo'))`` (:ticket:`24615`).
|
| 57 |
+
|
| 58 |
+
* Fixed a database table name quoting regression (:ticket:`24605`).
|
| 59 |
+
|
| 60 |
+
* Prevented the loss of ``null``/``not null`` column properties during field
|
| 61 |
+
alteration of MySQL databases (:ticket:`24595`).
|
| 62 |
+
|
| 63 |
+
* Fixed JavaScript path of ``contrib.admin``’s related field widget when using
|
| 64 |
+
alternate static file storages (:ticket:`24655`).
|
| 65 |
+
|
| 66 |
+
* Fixed a migration crash when adding new relations to models
|
| 67 |
+
(:ticket:`24573`).
|
| 68 |
+
|
| 69 |
+
* Fixed a migration crash when applying migrations with model managers on
|
| 70 |
+
Python 3 that were generated on Python 2 (:ticket:`24701`).
|
| 71 |
+
|
| 72 |
+
* Restored the ability to use iterators as queryset filter arguments
|
| 73 |
+
(:ticket:`24719`).
|
| 74 |
+
|
| 75 |
+
* Fixed a migration crash when renaming the target model of a many-to-many
|
| 76 |
+
relation (:ticket:`24725`).
|
| 77 |
+
|
| 78 |
+
* Removed flushing of the test database with :option:`test --keepdb`, which
|
| 79 |
+
prevented apps with data migrations from using the option (:ticket:`24729`).
|
| 80 |
+
|
| 81 |
+
* Fixed ``makemessages`` crash in some locales (:ticket:`23271`).
|
| 82 |
+
|
| 83 |
+
* Fixed help text positioning of ``contrib.admin`` fields that use the
|
| 84 |
+
``ModelAdmin.filter_horizontal`` and ``filter_vertical`` options
|
| 85 |
+
(:ticket:`24676`).
|
| 86 |
+
|
| 87 |
+
* Fixed ``AttributeError: function 'GDALAllRegister' not found`` error when
|
| 88 |
+
initializing ``contrib.gis`` on Windows.
|
| 89 |
+
|
| 90 |
+
Optimizations
|
| 91 |
+
=============
|
| 92 |
+
|
| 93 |
+
* Changed ``ModelState`` to deepcopy fields instead of deconstructing and
|
| 94 |
+
reconstructing (:ticket:`24591`). This speeds up the rendering of model
|
| 95 |
+
states and reduces memory usage when running :djadmin:`manage.py migrate
|
| 96 |
+
<migrate>` (although other changes in this release may negate any performance
|
| 97 |
+
benefits).
|
testbed/django__django/docs/releases/1.9.2.txt
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 1.9.2 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*February 1, 2016*
|
| 6 |
+
|
| 7 |
+
Django 1.9.2 fixes a security regression in 1.9 and several bugs in 1.9.1. It
|
| 8 |
+
also makes a small backwards incompatible change that hopefully doesn't affect
|
| 9 |
+
any users.
|
| 10 |
+
|
| 11 |
+
Security issue: User with "change" but not "add" permission can create objects for ``ModelAdmin``’s with ``save_as=True``
|
| 12 |
+
=========================================================================================================================
|
| 13 |
+
|
| 14 |
+
If a ``ModelAdmin`` uses ``save_as=True`` (not the default), the admin
|
| 15 |
+
provides an option when editing objects to "Save as new". A regression in
|
| 16 |
+
Django 1.9 prevented that form submission from raising a "Permission Denied"
|
| 17 |
+
error for users without the "add" permission.
|
| 18 |
+
|
| 19 |
+
Backwards incompatible change: ``.py-tpl`` files rewritten in project/app templates
|
| 20 |
+
===================================================================================
|
| 21 |
+
|
| 22 |
+
The addition of some Django template language syntax to the default app
|
| 23 |
+
template in Django 1.9 means those files now have some invalid Python syntax.
|
| 24 |
+
This causes difficulties for packaging systems that unconditionally
|
| 25 |
+
byte-compile ``*.py`` files.
|
| 26 |
+
|
| 27 |
+
To remedy this, a ``.py-tpl`` suffix is now used for the project and app
|
| 28 |
+
template files included in Django. The ``.py-tpl`` suffix is replaced with
|
| 29 |
+
``.py`` by the ``startproject`` and ``startapp`` commands. For example, a
|
| 30 |
+
template with the filename ``manage.py-tpl`` will be created as ``manage.py``.
|
| 31 |
+
|
| 32 |
+
Please file a ticket if you have a custom project template containing
|
| 33 |
+
``.py-tpl`` files and find this behavior problematic.
|
| 34 |
+
|
| 35 |
+
Bugfixes
|
| 36 |
+
========
|
| 37 |
+
|
| 38 |
+
* Fixed a regression in ``ConditionalGetMiddleware`` causing ``If-None-Match``
|
| 39 |
+
checks to always return HTTP 200 (:ticket:`26024`).
|
| 40 |
+
|
| 41 |
+
* Fixed a regression that caused the "user-tools" items to display on the
|
| 42 |
+
admin's logout page (:ticket:`26035`).
|
| 43 |
+
|
| 44 |
+
* Fixed a crash in the translations system when the current language has no
|
| 45 |
+
translations (:ticket:`26046`).
|
| 46 |
+
|
| 47 |
+
* Fixed a regression that caused the incorrect day to be selected when opening
|
| 48 |
+
the admin calendar widget for timezones from GMT+0100 to GMT+1200
|
| 49 |
+
(:ticket:`24980`).
|
| 50 |
+
|
| 51 |
+
* Fixed a regression in the admin's edit related model popup that caused an
|
| 52 |
+
escaped value to be displayed in the select dropdown of the parent window
|
| 53 |
+
(:ticket:`25997`).
|
| 54 |
+
|
| 55 |
+
* Fixed a regression in 1.8.8 causing incorrect index handling in migrations on
|
| 56 |
+
PostgreSQL when adding ``db_index=True`` or ``unique=True`` to a
|
| 57 |
+
``CharField`` or ``TextField`` that already had the other specified, or when
|
| 58 |
+
removing one of them from a field that had both, or when adding
|
| 59 |
+
``unique=True`` to a field already listed in ``unique_together``
|
| 60 |
+
(:ticket:`26034`).
|
| 61 |
+
|
| 62 |
+
* Fixed a regression where defining a relation on an abstract model's field
|
| 63 |
+
using a string model name without an app_label no longer resolved that
|
| 64 |
+
reference to the abstract model's app if using that model in another
|
| 65 |
+
application (:ticket:`25858`).
|
| 66 |
+
|
| 67 |
+
* Fixed a crash when destroying an existing test database on MySQL or
|
| 68 |
+
PostgreSQL (:ticket:`26096`).
|
| 69 |
+
|
| 70 |
+
* Fixed CSRF cookie check on POST requests when ``USE_X_FORWARDED_PORT=True``
|
| 71 |
+
(:ticket:`26094`).
|
| 72 |
+
|
| 73 |
+
* Fixed a ``QuerySet.order_by()`` crash when ordering by a relational field of
|
| 74 |
+
a ``ManyToManyField`` ``through`` model (:ticket:`26092`).
|
| 75 |
+
|
| 76 |
+
* Fixed a regression that caused an exception when making database queries on
|
| 77 |
+
SQLite with more than 2000 parameters when :setting:`DEBUG` is ``True`` on
|
| 78 |
+
distributions that increase the ``SQLITE_MAX_VARIABLE_NUMBER`` compile-time
|
| 79 |
+
limit to over 2000, such as Debian (:ticket:`26063`).
|
| 80 |
+
|
| 81 |
+
* Fixed a crash when using a reverse ``OneToOneField`` in
|
| 82 |
+
``ModelAdmin.readonly_fields`` (:ticket:`26060`).
|
| 83 |
+
|
| 84 |
+
* Fixed a crash when calling the ``migrate`` command in a test case with the
|
| 85 |
+
``available_apps`` attribute pointing to an application with migrations
|
| 86 |
+
disabled using the ``MIGRATION_MODULES`` setting (:ticket:`26135`).
|
| 87 |
+
|
| 88 |
+
* Restored the ability for testing and debugging tools to determine the
|
| 89 |
+
template from which a node came from, even during template inheritance or
|
| 90 |
+
inclusion. Prior to Django 1.9, debugging tools could access the template
|
| 91 |
+
origin from the node via ``Node.token.source[0]``. This was an undocumented,
|
| 92 |
+
private API. The origin is now available directly on each node using the
|
| 93 |
+
``Node.origin`` attribute (:ticket:`25848`).
|
| 94 |
+
|
| 95 |
+
* Fixed a regression in Django 1.8.5 that broke copying a ``SimpleLazyObject``
|
| 96 |
+
with ``copy.copy()`` (:ticket:`26122`).
|
| 97 |
+
|
| 98 |
+
* Always included ``geometry_field`` in the GeoJSON serializer output regardless
|
| 99 |
+
of the ``fields`` parameter (:ticket:`26138`).
|
| 100 |
+
|
| 101 |
+
* Fixed the ``contrib.gis`` map widgets when using
|
| 102 |
+
``USE_THOUSAND_SEPARATOR=True`` (:ticket:`20415`).
|
| 103 |
+
|
| 104 |
+
* Made invalid forms display the initial of values of their disabled fields
|
| 105 |
+
(:ticket:`26129`).
|
testbed/django__django/docs/releases/1.9.4.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 1.9.4 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*March 5, 2016*
|
| 6 |
+
|
| 7 |
+
Django 1.9.4 fixes a regression on Python 2 in the 1.9.3 security release
|
| 8 |
+
where ``utils.http.is_safe_url()`` crashes on bytestring URLs (:ticket:`26308`).
|
testbed/django__django/docs/releases/2.0.1.txt
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.0.1 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*January 1, 2018*
|
| 6 |
+
|
| 7 |
+
Django 2.0.1 fixes several bugs in 2.0.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Fixed a regression in Django 1.11 that added newlines between ``MultiWidget``'s
|
| 13 |
+
subwidgets (:ticket:`28890`).
|
| 14 |
+
|
| 15 |
+
* Fixed incorrect class-based model index name generation for models with
|
| 16 |
+
quoted ``db_table`` (:ticket:`28876`).
|
| 17 |
+
|
| 18 |
+
* Fixed incorrect foreign key constraint name for models with quoted
|
| 19 |
+
``db_table`` (:ticket:`28876`).
|
| 20 |
+
|
| 21 |
+
* Fixed a regression in caching of a ``GenericForeignKey`` when the referenced
|
| 22 |
+
model instance uses more than one level of multi-table inheritance
|
| 23 |
+
(:ticket:`28856`).
|
| 24 |
+
|
| 25 |
+
* Reallowed filtering a queryset with ``GeometryField=None`` (:ticket:`28896`).
|
| 26 |
+
|
| 27 |
+
* Corrected admin check to allow a ``OneToOneField`` in
|
| 28 |
+
``ModelAdmin.autocomplete_fields`` (:ticket:`28898`).
|
| 29 |
+
|
| 30 |
+
* Fixed a regression on SQLite where ``DecimalField`` returned a result with
|
| 31 |
+
trailing zeros in the fractional part truncated (:ticket:`28915`).
|
| 32 |
+
|
| 33 |
+
* Fixed crash in the ``testserver`` command startup (:ticket:`28941`).
|
| 34 |
+
|
| 35 |
+
* Fixed crash when coercing a translatable URL pattern to ``str``
|
| 36 |
+
(:ticket:`28947`).
|
| 37 |
+
|
| 38 |
+
* Fixed crash on SQLite when renaming a field in a model referenced by a
|
| 39 |
+
``ManyToManyField`` (:ticket:`28884`).
|
| 40 |
+
|
| 41 |
+
* Fixed a crash when chaining ``values()`` or ``values_list()`` after
|
| 42 |
+
``QuerySet.select_for_update(of=(...))`` (:ticket:`28944`).
|
| 43 |
+
|
| 44 |
+
* Fixed admin changelist crash when using a query expression in the page's
|
| 45 |
+
ordering (:ticket:`28958`).
|
testbed/django__django/docs/releases/2.0.5.txt
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.0.5 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*May 1, 2018*
|
| 6 |
+
|
| 7 |
+
Django 2.0.5 fixes several bugs in 2.0.4.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Corrected the import paths that ``inspectdb`` generates for
|
| 13 |
+
``django.contrib.postgres`` fields (:ticket:`29307`).
|
| 14 |
+
|
| 15 |
+
* Fixed a regression in Django 1.11.8 where altering a field with a unique
|
| 16 |
+
constraint may drop and rebuild more foreign keys than necessary
|
| 17 |
+
(:ticket:`29193`).
|
| 18 |
+
|
| 19 |
+
* Fixed crashes in ``django.contrib.admindocs`` when a view is a callable
|
| 20 |
+
object, such as ``django.contrib.syndication.views.Feed`` (:ticket:`29296`).
|
| 21 |
+
|
| 22 |
+
* Fixed a regression in Django 2.0.4 where ``QuerySet.values()`` or
|
| 23 |
+
``values_list()`` after combining an annotated and unannotated queryset with
|
| 24 |
+
``union()``, ``difference()``, or ``intersection()`` crashed due to mismatching
|
| 25 |
+
columns (:ticket:`29286`).
|
testbed/django__django/docs/releases/2.1.12.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.1.12 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*September 2, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.1.12 fixes a regression in 2.1.11.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Fixed crash of ``KeyTransform()`` for
|
| 13 |
+
``django.contrib.postgres.fields.JSONField`` and
|
| 14 |
+
:class:`~django.contrib.postgres.fields.HStoreField` when using on
|
| 15 |
+
expressions with params (:ticket:`30672`).
|
testbed/django__django/docs/releases/2.1.14.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.1.14 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*November 4, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.1.14 fixes a regression in 2.1.13.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Fixed a crash when using a ``contains``, ``contained_by``, ``has_key``,
|
| 13 |
+
``has_keys``, or ``has_any_keys`` lookup on
|
| 14 |
+
``django.contrib.postgres.fields.JSONField``, if the right or left hand
|
| 15 |
+
side of an expression is a key transform (:ticket:`30826`).
|
testbed/django__django/docs/releases/2.1.15.txt
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.1.15 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*December 2, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.1.15 fixes a security issue and a data loss bug in 2.1.14.
|
| 8 |
+
|
| 9 |
+
CVE-2019-19118: Privilege escalation in the Django admin.
|
| 10 |
+
=========================================================
|
| 11 |
+
|
| 12 |
+
Since Django 2.1, a Django model admin displaying a parent model with related
|
| 13 |
+
model inlines, where the user has view-only permissions to a parent model but
|
| 14 |
+
edit permissions to the inline model, would display a read-only view of the
|
| 15 |
+
parent model but editable forms for the inline.
|
| 16 |
+
|
| 17 |
+
Submitting these forms would not allow direct edits to the parent model, but
|
| 18 |
+
would trigger the parent model's ``save()`` method, and cause pre and post-save
|
| 19 |
+
signal handlers to be invoked. This is a privilege escalation as a user who
|
| 20 |
+
lacks permission to edit a model should not be able to trigger its save-related
|
| 21 |
+
signals.
|
| 22 |
+
|
| 23 |
+
To resolve this issue, the permission handling code of the Django admin
|
| 24 |
+
interface has been changed. Now, if a user has only the "view" permission for a
|
| 25 |
+
parent model, the entire displayed form will not be editable, even if the user
|
| 26 |
+
has permission to edit models included in inlines.
|
| 27 |
+
|
| 28 |
+
This is a backwards-incompatible change, and the Django security team is aware
|
| 29 |
+
that some users of Django were depending on the ability to allow editing of
|
| 30 |
+
inlines in the admin form of an otherwise view-only parent model.
|
| 31 |
+
|
| 32 |
+
Given the complexity of the Django admin, and in-particular the permissions
|
| 33 |
+
related checks, it is the view of the Django security team that this change was
|
| 34 |
+
necessary: that it is not currently feasible to maintain the existing behavior
|
| 35 |
+
while escaping the potential privilege escalation in a way that would avoid a
|
| 36 |
+
recurrence of similar issues in the future, and that would be compatible with
|
| 37 |
+
Django's *safe by default* philosophy.
|
| 38 |
+
|
| 39 |
+
For the time being, developers whose applications are affected by this change
|
| 40 |
+
should replace the use of inlines in read-only parents with custom forms and
|
| 41 |
+
views that explicitly implement the desired functionality. In the longer term,
|
| 42 |
+
adding a documented, supported, and properly-tested mechanism for
|
| 43 |
+
partially-editable multi-model forms to the admin interface may occur in Django
|
| 44 |
+
itself.
|
| 45 |
+
|
| 46 |
+
Bugfixes
|
| 47 |
+
========
|
| 48 |
+
|
| 49 |
+
* Fixed a data loss possibility in the
|
| 50 |
+
:meth:`~django.db.models.query.QuerySet.select_for_update()`. When using
|
| 51 |
+
``'self'`` in the ``of`` argument with :ref:`multi-table inheritance
|
| 52 |
+
<multi-table-inheritance>`, a parent model was locked instead of the
|
| 53 |
+
queryset's model (:ticket:`30953`).
|
testbed/django__django/docs/releases/2.1.2.txt
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.1.2 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*October 1, 2018*
|
| 6 |
+
|
| 7 |
+
Django 2.1.2 fixes a security issue and several bugs in 2.1.1. Also, the latest
|
| 8 |
+
string translations from Transifex are incorporated.
|
| 9 |
+
|
| 10 |
+
CVE-2018-16984: Password hash disclosure to "view only" admin users
|
| 11 |
+
===================================================================
|
| 12 |
+
|
| 13 |
+
If an admin user has the change permission to the user model, only part of the
|
| 14 |
+
password hash is displayed in the change form. Admin users with the view (but
|
| 15 |
+
not change) permission to the user model were displayed the entire hash. While
|
| 16 |
+
it's typically infeasible to reverse a strong password hash, if your site uses
|
| 17 |
+
weaker password hashing algorithms such as MD5 or SHA1, it could be a problem.
|
| 18 |
+
|
| 19 |
+
Bugfixes
|
| 20 |
+
========
|
| 21 |
+
|
| 22 |
+
* Fixed a regression where nonexistent joins in ``F()`` no longer raised
|
| 23 |
+
``FieldError`` (:ticket:`29727`).
|
| 24 |
+
|
| 25 |
+
* Fixed a regression where files starting with a tilde or underscore weren't
|
| 26 |
+
ignored by the migrations loader (:ticket:`29749`).
|
| 27 |
+
|
| 28 |
+
* Made migrations detect changes to ``Meta.default_related_name``
|
| 29 |
+
(:ticket:`29755`).
|
| 30 |
+
|
| 31 |
+
* Added compatibility for ``cx_Oracle`` 7 (:ticket:`29759`).
|
| 32 |
+
|
| 33 |
+
* Fixed a regression in Django 2.0 where unique index names weren't quoted
|
| 34 |
+
(:ticket:`29778`).
|
| 35 |
+
|
| 36 |
+
* Fixed a regression where sliced queries with multiple columns with the same
|
| 37 |
+
name crashed on Oracle 12.1 (:ticket:`29630`).
|
| 38 |
+
|
| 39 |
+
* Fixed a crash when a user with the view (but not change) permission made a
|
| 40 |
+
POST request to an admin user change form (:ticket:`29809`).
|
testbed/django__django/docs/releases/2.1.4.txt
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.1.4 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*December 3, 2018*
|
| 6 |
+
|
| 7 |
+
Django 2.1.4 fixes several bugs in 2.1.3.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Corrected the default password list that ``CommonPasswordValidator`` uses by
|
| 13 |
+
lowercasing all passwords to match the format expected by the validator
|
| 14 |
+
(:ticket:`29952`).
|
| 15 |
+
|
| 16 |
+
* Prevented repetitive calls to ``geos_version_tuple()`` in the ``WKBWriter``
|
| 17 |
+
class in an attempt to fix a random crash involving ``LooseVersion``
|
| 18 |
+
(:ticket:`29959`).
|
| 19 |
+
|
| 20 |
+
* Fixed keep-alive support in ``runserver`` after it was disabled to fix
|
| 21 |
+
another issue in Django 2.0 (:ticket:`29849`).
|
| 22 |
+
|
| 23 |
+
* Fixed admin view-only change form crash when using
|
| 24 |
+
``ModelAdmin.prepopulated_fields`` (:ticket:`29929`).
|
| 25 |
+
|
| 26 |
+
* Fixed "Please correct the errors below" error message when editing an object
|
| 27 |
+
in the admin if the user only has the "view" permission on inlines
|
| 28 |
+
(:ticket:`29930`).
|
testbed/django__django/docs/releases/2.1.7.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.1.7 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*February 11, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.1.7 fixes a packaging error in 2.1.6.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Corrected packaging error from 2.1.6 (:ticket:`30175`).
|
testbed/django__django/docs/releases/2.1.8.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.1.8 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*April 1, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.1.8 fixes a bug in 2.1.7.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Prevented admin inlines for a ``ManyToManyField``\'s implicit through model
|
| 13 |
+
from being editable if the user only has the view permission
|
| 14 |
+
(:ticket:`30289`).
|
testbed/django__django/docs/releases/2.1.9.txt
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
============================
|
| 2 |
+
Django 2.1.9 release notes
|
| 3 |
+
============================
|
| 4 |
+
|
| 5 |
+
*June 3, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.1.9 fixes security issues in 2.1.8.
|
| 8 |
+
|
| 9 |
+
CVE-2019-12308: AdminURLFieldWidget XSS
|
| 10 |
+
---------------------------------------
|
| 11 |
+
|
| 12 |
+
The clickable "Current URL" link generated by ``AdminURLFieldWidget`` displayed
|
| 13 |
+
the provided value without validating it as a safe URL. Thus, an unvalidated
|
| 14 |
+
value stored in the database, or a value provided as a URL query parameter
|
| 15 |
+
payload, could result in an clickable JavaScript link.
|
| 16 |
+
|
| 17 |
+
``AdminURLFieldWidget`` now validates the provided value using
|
| 18 |
+
:class:`~django.core.validators.URLValidator` before displaying the clickable
|
| 19 |
+
link. You may customize the validator by passing a ``validator_class`` kwarg to
|
| 20 |
+
``AdminURLFieldWidget.__init__()``, e.g. when using
|
| 21 |
+
:attr:`~django.contrib.admin.ModelAdmin.formfield_overrides`.
|
| 22 |
+
|
| 23 |
+
Patched bundled jQuery for CVE-2019-11358: Prototype pollution
|
| 24 |
+
--------------------------------------------------------------
|
| 25 |
+
|
| 26 |
+
jQuery before 3.4.0, mishandles ``jQuery.extend(true, {}, ...)`` because of
|
| 27 |
+
``Object.prototype`` pollution. If an unsanitized source object contained an
|
| 28 |
+
enumerable ``__proto__`` property, it could extend the native
|
| 29 |
+
``Object.prototype``.
|
| 30 |
+
|
| 31 |
+
The bundled version of j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 1, 2018*
|
| 6 |
+
|
| 7 |
+
Welcome to Django 2.1!
|
| 8 |
+
|
| 9 |
+
These release notes cover the :ref:`new features <whats-new-2.1>`, as well as
|
| 10 |
+
some :ref:`backwards incompatible changes <backwards-incompatible-2.1>` you'll
|
| 11 |
+
want to be aware of when upgrading from Django 2.0 or earlier. We've
|
| 12 |
+
:ref:`dropped some features<removed-features-2.1>` that have reached the end of
|
| 13 |
+
their deprecation cycle, and we've :ref:`begun the deprecation process for some
|
| 14 |
+
features <deprecated-features-2.1>`.
|
| 15 |
+
|
| 16 |
+
See the :doc:`/howto/upgrade-version` guide if you're updating an existing
|
| 17 |
+
project.
|
| 18 |
+
|
| 19 |
+
Python compatibility
|
| 20 |
+
====================
|
| 21 |
+
|
| 22 |
+
Django 2.1 supports Python 3.5, 3.6, and 3.7. Django 2.0 is the last version to
|
| 23 |
+
support Python 3.4. We **highly recommend** and only officially support the
|
| 24 |
+
latest release of each series.
|
| 25 |
+
|
| 26 |
+
.. _whats-new-2.1:
|
| 27 |
+
|
| 28 |
+
What's new in Django 2.1
|
| 29 |
+
========================
|
| 30 |
+
|
| 31 |
+
Model "view" permission
|
| 32 |
+
-----------------------
|
| 33 |
+
|
| 34 |
+
A "view" permission is added to the model :attr:`Meta.default_permissions
|
| 35 |
+
<django.db.models.Options.default_permissions>`. The new permissions will be
|
| 36 |
+
created automatically when running :djadmin:`migrate`.
|
| 37 |
+
|
| 38 |
+
This allows giving users read-only access to models in the admin.
|
| 39 |
+
:meth:`.ModelAdmin.has_view_permission` is new. The implementation is backwards
|
| 40 |
+
compatible in that there isn't a need to assign the "view" permission to allow
|
| 41 |
+
users who have the "change" permission to edit objects.
|
| 42 |
+
|
| 43 |
+
There are a couple of :ref:`backwards incompatible considerations
|
| 44 |
+
<view_permission_backwards_incompatible>`.
|
| 45 |
+
|
| 46 |
+
Minor features
|
| 47 |
+
--------------
|
| 48 |
+
|
| 49 |
+
:mod:`django.contrib.admin`
|
| 50 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 51 |
+
|
| 52 |
+
* :attr:`.ModelAdmin.search_fields` now accepts any lookup such as
|
| 53 |
+
``field__exact``.
|
| 54 |
+
|
| 55 |
+
* jQuery is upgraded from version 2.2.3 to 3.3.1.
|
| 56 |
+
|
| 57 |
+
* The new :meth:`.ModelAdmin.delete_queryset` method allows customizing the
|
| 58 |
+
deletion process of the "delete selected objects" action.
|
| 59 |
+
|
| 60 |
+
* You can now :ref:`override the default admin site
|
| 61 |
+
<overriding-default-admin-site>`.
|
| 62 |
+
|
| 63 |
+
* The new :attr:`.ModelAdmin.sortable_by` attribute and
|
| 64 |
+
:meth:`.ModelAdmin.get_sortable_by` method allow limiting the columns that
|
| 65 |
+
can be sorted in the change list page.
|
| 66 |
+
|
| 67 |
+
* The ``admin_order_field`` attribute for elements in
|
| 68 |
+
:attr:`.ModelAdmin.list_display` may now be a query expression.
|
| 69 |
+
|
| 70 |
+
* The new :meth:`.ModelAdmin.get_deleted_objects()` method allows customizing
|
| 71 |
+
the deletion process of the delete view and the "delete selected" action.
|
| 72 |
+
|
| 73 |
+
* The ``actions.html``, ``change_list_results.html``, ``date_hierarchy.html``,
|
| 74 |
+
``pagination.html``, ``prepopulated_fields_js.html``, ``search_form.html``,
|
| 75 |
+
and ``submit_line.html`` templates can now be :ref:`overridden per app or
|
| 76 |
+
per model <admin-templates-overridden-per-app-or-model>` (besides overridden
|
| 77 |
+
globally).
|
| 78 |
+
|
| 79 |
+
* The admin change list and change form object tools can now be :ref:`overridden
|
| 80 |
+
per app, per model, or globally <admin-templates-overridden-per-app-or-model>`
|
| 81 |
+
with ``change_list_object_tools.html`` and
|
| 82 |
+
``change_form_object_tools.html`` templates.
|
| 83 |
+
|
| 84 |
+
* :meth:`.InlineModelAdmin.has_add_permission` is now passed the parent object
|
| 85 |
+
as the second positional argument, ``obj``.
|
| 86 |
+
|
| 87 |
+
* Admin actions may now :ref:`specify permissions <admin-action-permissions>`
|
| 88 |
+
to limit their availability to certain users.
|
| 89 |
+
|
| 90 |
+
:mod:`django.contrib.auth`
|
| 91 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 92 |
+
|
| 93 |
+
* :djadmin:`createsuperuser` now gives a prompt to allow bypassing the
|
| 94 |
+
:setting:`AUTH_PASSWORD_VALIDATORS` checks.
|
| 95 |
+
|
| 96 |
+
:mod:`django.contrib.gis`
|
| 97 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 98 |
+
|
| 99 |
+
* The new :meth:`.GEOSGeometry.buffer_with_style` method is a version of
|
| 100 |
+
:meth:`~.GEOSGeometry.buffer` that allows customizing the style of the
|
| 101 |
+
buffer.
|
| 102 |
+
|
| 103 |
+
* :class:`~django.contrib.gis.forms.widgets.OpenLayersWidget` is now based on
|
| 104 |
+
OpenLayers 4.6.5 (previously 3.20.1).
|
| 105 |
+
|
| 106 |
+
:mod:`django.contrib.sessions`
|
| 107 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 108 |
+
|
| 109 |
+
* Added the :setting:`SESSION_COOKIE_SAMESITE` setting to set the ``SameSite``
|
| 110 |
+
cookie flag on session cookies.
|
| 111 |
+
|
| 112 |
+
Cache
|
| 113 |
+
~~~~~
|
| 114 |
+
|
| 115 |
+
* The :ref:`local-memory cache backend <local-memory-caching>` now uses a
|
| 116 |
+
least-recently-used (LRU) culling strategy rather than a pseudo-random one.
|
| 117 |
+
|
| 118 |
+
* The new :meth:`~django.core.cache.cache.touch` method of the :ref:`low-level
|
| 119 |
+
cache API <low-level-cache-api>` updates the timeout of cache keys.
|
| 120 |
+
|
| 121 |
+
CSRF
|
| 122 |
+
~~~~
|
| 123 |
+
|
| 124 |
+
* Added the :setting:`CSRF_COOKIE_SAMESITE` setting to set the ``SameSite``
|
| 125 |
+
cookie flag on CSRF cookies.
|
| 126 |
+
|
| 127 |
+
Forms
|
| 128 |
+
~~~~~
|
| 129 |
+
|
| 130 |
+
* The widget for ``ImageField`` now renders with the HTML attribute
|
| 131 |
+
``accept="image/*"``.
|
| 132 |
+
|
| 133 |
+
Internationalization
|
| 134 |
+
~~~~~~~~~~~~~~~~~~~~
|
| 135 |
+
|
| 136 |
+
* Added the :meth:`~django.utils.translation.get_supported_language_variant`
|
| 137 |
+
function.
|
| 138 |
+
|
| 139 |
+
* Untranslated strings for territorial language variants now use the
|
| 140 |
+
translations of the generic language. For example, untranslated ``pt_BR``
|
| 141 |
+
strings use ``pt`` translations.
|
| 142 |
+
|
| 143 |
+
Management Commands
|
| 144 |
+
~~~~~~~~~~~~~~~~~~~
|
| 145 |
+
|
| 146 |
+
* The new :option:`inspectdb --include-views` option allows creating models
|
| 147 |
+
for database views.
|
| 148 |
+
|
| 149 |
+
* The :class:`~django.core.management.BaseCommand` class now uses a custom help
|
| 150 |
+
formatter so that the standard options like ``--verbosity`` or ``--settings``
|
| 151 |
+
appear last in the help output, giving a more prominent position to subclassed
|
| 152 |
+
command's options.
|
| 153 |
+
|
| 154 |
+
Migrations
|
| 155 |
+
~~~~~~~~~~
|
| 156 |
+
|
| 157 |
+
* Added support for serialization of ``functools.partialmethod`` objects.
|
| 158 |
+
|
| 159 |
+
* To support frozen environments, migrations may be loaded from ``.pyc`` files.
|
| 160 |
+
|
| 161 |
+
Models
|
| 162 |
+
~~~~~~
|
| 163 |
+
|
| 164 |
+
* Models can now use ``__init_subclass__()`` from :pep:`487`.
|
| 165 |
+
|
| 166 |
+
* A ``BinaryField`` may now be set to ``editable=True`` if you wish to include
|
| 167 |
+
it in model forms.
|
| 168 |
+
|
| 169 |
+
* A number of new text database functions are added:
|
| 170 |
+
:class:`~django.db.models.functions.Chr`,
|
| 171 |
+
:class:`~django.db.models.functions.Left`,
|
| 172 |
+
:class:`~django.db.models.functions.LPad`,
|
| 173 |
+
:class:`~django.db.models.functions.LTrim`,
|
| 174 |
+
:class:`~django.db.models.functions.Ord`,
|
| 175 |
+
:class:`~django.db.models.functions.Repeat`,
|
| 176 |
+
:class:`~django.db.models.functions.Replace`,
|
| 177 |
+
:class:`~django.db.models.functions.Right`,
|
| 178 |
+
:class:`~django.db.models.functions.RPad`,
|
| 179 |
+
:class:`~django.db.models.functions.RTrim`, and
|
| 180 |
+
:class:`~django.db.models.functions.Trim`.
|
| 181 |
+
|
| 182 |
+
* The new :class:`~django.db.models.functions.TruncWeek` function truncates
|
| 183 |
+
:class:`~django.db.models.DateField` and
|
| 184 |
+
:class:`~django.db.models.DateTimeField` to the Monday of a week.
|
| 185 |
+
|
| 186 |
+
* Query expressions can now be negated using a minus sign.
|
| 187 |
+
|
| 188 |
+
* :meth:`.QuerySet.order_by` and :meth:`distinct(*fields) <.QuerySet.distinct>`
|
| 189 |
+
now support using field transforms.
|
| 190 |
+
|
| 191 |
+
* :class:`~django.db.models.BooleanField` can now be ``null=True``. This is
|
| 192 |
+
encouraged instead of ``NullBooleanField``, which will likely be deprecated
|
| 193 |
+
in the future.
|
| 194 |
+
|
| 195 |
+
* The new :meth:`.QuerySet.explain` method displays the database's execution
|
| 196 |
+
plan of a queryset's query.
|
| 197 |
+
|
| 198 |
+
* :meth:`.QuerySet.raw` now supports :meth:`~.QuerySet.prefetch_related`.
|
| 199 |
+
|
| 200 |
+
Requests and Responses
|
| 201 |
+
~~~~~~~~~~~~~~~~~~~~~~
|
| 202 |
+
|
| 203 |
+
* Added :meth:`.HttpRequest.get_full_path_info`.
|
| 204 |
+
|
| 205 |
+
* Added the ``samesite`` argument to :meth:`.HttpResponse.set_cookie` to allow
|
| 206 |
+
setting the ``SameSite`` cookie flag.
|
| 207 |
+
|
| 208 |
+
* The new ``as_attachment`` argument for :class:`~django.http.FileResponse`
|
| 209 |
+
sets the ``Content-Disposition`` header to make the browser ask if the user
|
| 210 |
+
wants to download the file. ``FileResponse`` also tries to set the
|
| 211 |
+
``Content-Type`` and ``Content-Length`` headers where appropriate.
|
| 212 |
+
|
| 213 |
+
Templates
|
| 214 |
+
~~~~~~~~~
|
| 215 |
+
|
| 216 |
+
* The new :tfilter:`json_script` filter safely outputs a Python object as JSON,
|
| 217 |
+
wrapped in a ``<script>`` tag, ready for use with JavaScript.
|
| 218 |
+
|
| 219 |
+
Tests
|
| 220 |
+
~~~~~
|
| 221 |
+
|
| 222 |
+
* Added test :class:`~django.test.Client` support for 307 and 308 redirects.
|
| 223 |
+
|
| 224 |
+
* The test :class:`~django.test.Client` now serializes a request data
|
| 225 |
+
dictionary as JSON if ``content_type='application/json'``. You can customize
|
| 226 |
+
the JSON encoder with test client's ``json_encoder`` parameter.
|
| 227 |
+
|
| 228 |
+
* The new :meth:`.SimpleTestCase.assertWarnsMessage` method is a simpler
|
| 229 |
+
version of :meth:`~unittest.TestCase.assertWarnsRegex`.
|
| 230 |
+
|
| 231 |
+
.. _backwards-incompatible-2.1:
|
| 232 |
+
|
| 233 |
+
Backwards incompatible changes in 2.1
|
| 234 |
+
=====================================
|
| 235 |
+
|
| 236 |
+
Database backend API
|
| 237 |
+
--------------------
|
| 238 |
+
|
| 239 |
+
This section describes changes that may be needed in third-party database
|
| 240 |
+
backends.
|
| 241 |
+
|
| 242 |
+
* To adhere to :pep:`249`, exceptions where a database doesn't support a
|
| 243 |
+
feature are changed from :exc:`NotImplementedError` to
|
| 244 |
+
:exc:`django.db.NotSupportedError`.
|
| 245 |
+
|
| 246 |
+
* Renamed the ``allow_sliced_subqueries`` database feature flag to
|
| 247 |
+
``allow_sliced_subqueries_with_in``.
|
| 248 |
+
|
| 249 |
+
* ``DatabaseOperations.distinct_sql()`` now requires an additional ``params``
|
| 250 |
+
argument and returns a tuple of SQL and parameters instead of an SQL string.
|
| 251 |
+
|
| 252 |
+
* ``DatabaseFeatures.introspected_boolean_field_type`` is changed from a method
|
| 253 |
+
to a property.
|
| 254 |
+
|
| 255 |
+
:mod:`django.contrib.gis`
|
| 256 |
+
-------------------------
|
| 257 |
+
|
| 258 |
+
* Support for SpatiaLite 4.0 is removed.
|
| 259 |
+
|
| 260 |
+
Dropped support for MySQL 5.5
|
| 261 |
+
-----------------------------
|
| 262 |
+
|
| 263 |
+
The end of upstream support for MySQL 5.5 is December 2018. Django 2.1 supports
|
| 264 |
+
MySQL 5.6 and higher.
|
| 265 |
+
|
| 266 |
+
Dropped support for PostgreSQL 9.3
|
| 267 |
+
----------------------------------
|
| 268 |
+
|
| 269 |
+
The end of upstream support for PostgreSQL 9.3 is September 2018. Django 2.1
|
| 270 |
+
supports PostgreSQL 9.4 and higher.
|
| 271 |
+
|
| 272 |
+
Removed ``BCryptPasswordHasher`` from the default ``PASSWORD_HASHERS`` setting
|
| 273 |
+
------------------------------------------------------------------------------
|
| 274 |
+
|
| 275 |
+
If you used bcrypt with Django 1.4 or 1.5 (before ``BCryptSHA256PasswordHasher``
|
| 276 |
+
was added in Django 1.6), you might have some passwords that use the
|
| 277 |
+
``BCryptPasswordHasher`` hasher.
|
| 278 |
+
|
| 279 |
+
You can check if that's the case like this::
|
| 280 |
+
|
| 281 |
+
from django.contrib.auth import get_user_model
|
| 282 |
+
|
| 283 |
+
User = get_user_model()
|
| 284 |
+
User.objects.filter(password__startswith="bcrypt$$")
|
| 285 |
+
|
| 286 |
+
If you want to continue to allow those passwords to be used, you'll
|
| 287 |
+
have to define the :setting:`PASSWORD_HASHERS` setting (if you don't already)
|
| 288 |
+
and include ``'django.contrib.auth.hashers.BCryptPasswordHasher'``.
|
| 289 |
+
|
| 290 |
+
Moved ``wrap_label`` widget template context variable
|
| 291 |
+
-----------------------------------------------------
|
| 292 |
+
|
| 293 |
+
To fix the lack of ``<label>`` when using ``RadioSelect`` and
|
| 294 |
+
``CheckboxSelectMultiple`` with ``MultiWidget``, the ``wrap_label`` context
|
| 295 |
+
variable now appears as an attribute of each option. For example, in a custom
|
| 296 |
+
``input_option.html`` template, change ``{% if wrap_label %}`` to
|
| 297 |
+
``{% if widget.wrap_label %}``.
|
| 298 |
+
|
| 299 |
+
``SameSite`` cookies
|
| 300 |
+
--------------------
|
| 301 |
+
|
| 302 |
+
The cookies used for ``django.contrib.sessions``, ``django.contrib.messages``,
|
| 303 |
+
and Django's CSRF protection now set the ``SameSite`` flag to ``Lax`` by
|
| 304 |
+
default. Browsers that respect this flag won't send these cookies on
|
| 305 |
+
cross-origin requests. If you rely on the old behavior, set the
|
| 306 |
+
:setting:`SESSION_COOKIE_SAMESITE` and/or :setting:`CSRF_COOKIE_SAMESITE`
|
| 307 |
+
setting to ``None``.
|
| 308 |
+
|
| 309 |
+
.. _view_permission_backwards_incompatible:
|
| 310 |
+
|
| 311 |
+
Considerations for the new model "view" permission
|
| 312 |
+
--------------------------------------------------
|
| 313 |
+
|
| 314 |
+
Custom admin forms need to take the view-only case into account
|
| 315 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 316 |
+
|
| 317 |
+
With the new "view" permission, existing custom admin forms may raise errors
|
| 318 |
+
when a user doesn't have the change permission because the form might access
|
| 319 |
+
nonexistent fields. Fix this by overriding :meth:`.ModelAdmin.get_form` and
|
| 320 |
+
checking if the user has the "change" permissions and returning the default
|
| 321 |
+
form if not::
|
| 322 |
+
|
| 323 |
+
class MyAdmin(admin.ModelAdmin):
|
| 324 |
+
def get_form(self, request, obj=None, **kwargs):
|
| 325 |
+
if not self.has_change_permission(request, obj):
|
| 326 |
+
return super().get_form(request, obj, **kwargs)
|
| 327 |
+
return CustomForm
|
| 328 |
+
|
| 329 |
+
New default view permission could allow unwanted access to admin views
|
| 330 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 331 |
+
|
| 332 |
+
If you have a custom permission with a codename of the form
|
| 333 |
+
``view_<modelname>``, the new view permission handling in the admin will allow
|
| 334 |
+
view access to the changelist and detail pages for those models. If this is
|
| 335 |
+
unwanted, you must change your custom permission codename.
|
| 336 |
+
|
| 337 |
+
Miscellaneous
|
| 338 |
+
-------------
|
| 339 |
+
|
| 340 |
+
* The minimum supported version of ``mysqlclient`` is increased from 1.3.3 to
|
| 341 |
+
1.3.7.
|
| 342 |
+
|
| 343 |
+
* Support for SQLite < 3.7.15 is removed.
|
| 344 |
+
|
| 345 |
+
* The date format of ``Set-Cookie``'s ``Expires`` directive is changed to
|
| 346 |
+
follow :rfc:`7231#section-7.1.1.1` instead of Netscape's cookie standard.
|
| 347 |
+
Hyphens present in dates like ``Tue, 25-Dec-2018 22:26:13 GMT`` are removed.
|
| 348 |
+
This change should be merely cosmetic except perhaps for antiquated browsers
|
| 349 |
+
that don't parse the new format.
|
| 350 |
+
|
| 351 |
+
* ``allowed_hosts`` is now a required argument of private API
|
| 352 |
+
``django.utils.http.is_safe_url()``.
|
| 353 |
+
|
| 354 |
+
* The ``multiple`` attribute rendered by the
|
| 355 |
+
:class:`~django.forms.SelectMultiple` widget now uses HTML5 boolean syntax
|
| 356 |
+
rather than XHTML's ``multiple="multiple"``.
|
| 357 |
+
|
| 358 |
+
* HTML rendered by form widgets no longer includes a closing slash on void
|
| 359 |
+
elements, e.g. ``<br>``. This is incompatible within XHTML, although some
|
| 360 |
+
widgets already used aspects of HTML5 such as boolean attributes.
|
| 361 |
+
|
| 362 |
+
* The value of :class:`~django.forms.SelectDateWidget`'s empty options is
|
| 363 |
+
changed from 0 to an empty string, which mainly may require some adjustments
|
| 364 |
+
in tests that compare HTML.
|
| 365 |
+
|
| 366 |
+
* :meth:`.User.has_usable_password` and the
|
| 367 |
+
:func:`~django.contrib.auth.hashers.is_password_usable` function no longer
|
| 368 |
+
return ``False`` if the password is ``None`` or an empty string, or if the
|
| 369 |
+
password uses a hasher that's not in the :setting:`PASSWORD_HASHERS` setting.
|
| 370 |
+
This undocumented behavior was a regression in Django 1.6 and prevented users
|
| 371 |
+
with such passwords from requesting a password reset. Audit your code to
|
| 372 |
+
confirm that your usage of these APIs don't rely on the old behavior.
|
| 373 |
+
|
| 374 |
+
* Since migrations are now loaded from ``.pyc`` files, you might need to delete
|
| 375 |
+
them if you're working in a mixed Python 2 and Python 3 environment.
|
| 376 |
+
|
| 377 |
+
* Using ``None`` as a ``django.contrib.postgres.fields.JSONField`` lookup
|
| 378 |
+
value now matches objects that have the specified key and a null value rather
|
| 379 |
+
than objects that don't have the key.
|
| 380 |
+
|
| 381 |
+
* The admin CSS class ``field-box`` is renamed to ``fieldBox`` to prevent
|
| 382 |
+
conflicts with the class given to model fields named "box".
|
| 383 |
+
|
| 384 |
+
* Since the admin's ``actions.html``, ``change_list_results.html``,
|
| 385 |
+
``date_hierarchy.html``, ``pagination.html``, ``prepopulated_fields_js.html``,
|
| 386 |
+
``search_form.html``, and ``submit_line.html`` templates can now be
|
| 387 |
+
overridden per app or per model, you may need to rename existing templates
|
| 388 |
+
with those names that were written for a different purpose.
|
| 389 |
+
|
| 390 |
+
* ``QuerySet.raw()`` now caches its results like regular querysets. Use
|
| 391 |
+
``iterator()`` if you don't want caching.
|
| 392 |
+
|
| 393 |
+
* The database router :meth:`allow_relation` method is called in more cases.
|
| 394 |
+
Improperly written routers may need to be updated accordingly.
|
| 395 |
+
|
| 396 |
+
* Translations are no longer deactivated before running management commands.
|
| 397 |
+
If your custom command requires translations to be deactivated (for example,
|
| 398 |
+
to insert untranslated content into the database), use the new
|
| 399 |
+
:ref:`@no_translations decorator <management-commands-and-locales>`.
|
| 400 |
+
|
| 401 |
+
* Management commands no longer allow the abbreviated forms of the
|
| 402 |
+
``--settings`` and ``--pythonpath`` arguments.
|
| 403 |
+
|
| 404 |
+
* The private ``django.db.models.sql.constants.QUERY_TERMS`` constant is
|
| 405 |
+
removed. The :meth:`~.RegisterLookupMixin.get_lookup`
|
| 406 |
+
and :meth:`~.RegisterLookupMixin.get_lookups` methods
|
| 407 |
+
of the :ref:`Lookup Registration API <lookup-registration-api>` may be
|
| 408 |
+
suitable alternatives. Compared to the ``QUERY_TERMS`` constant, they allow
|
| 409 |
+
your code to also account for any custom lookups that have been registered.
|
| 410 |
+
|
| 411 |
+
* Compatibility with ``py-bcrypt`` is removed as it's unmaintained. Use
|
| 412 |
+
:pypi:`bcrypt` instead.
|
| 413 |
+
|
| 414 |
+
.. _deprecated-features-2.1:
|
| 415 |
+
|
| 416 |
+
Features deprecated in 2.1
|
| 417 |
+
==========================
|
| 418 |
+
|
| 419 |
+
Miscellaneous
|
| 420 |
+
-------------
|
| 421 |
+
|
| 422 |
+
* The ``ForceRHR`` GIS function is deprecated in favor of the new
|
| 423 |
+
:class:`~django.contrib.gis.db.models.functions.ForcePolygonCW` function.
|
| 424 |
+
|
| 425 |
+
* ``django.utils.http.cookie_date()`` is deprecated in favor of
|
| 426 |
+
:func:`~django.utils.http.http_date`, which follows the format of the latest
|
| 427 |
+
RFC.
|
| 428 |
+
|
| 429 |
+
* ``{% load staticfiles %}`` and ``{% load admin_static %}`` are deprecated
|
| 430 |
+
in favor of ``{% load static %}``, which works the same.
|
| 431 |
+
|
| 432 |
+
* ``django.contrib.staticfiles.templatetags.static()`` is deprecated in favor
|
| 433 |
+
of ``django.templatetags.static.static()``.
|
| 434 |
+
|
| 435 |
+
* Support for :meth:`.InlineModelAdmin.has_add_permission` methods that don't
|
| 436 |
+
accept ``obj`` as the second positional argument will be removed in Django
|
| 437 |
+
3.0.
|
| 438 |
+
|
| 439 |
+
.. _removed-features-2.1:
|
| 440 |
+
|
| 441 |
+
Features removed in 2.1
|
| 442 |
+
=======================
|
| 443 |
+
|
| 444 |
+
These features have reached the end of their deprecation cycle and are removed
|
| 445 |
+
in Django 2.1. See :ref:`deprecated-features-1.11` for details, including how
|
| 446 |
+
to remove usage of these features.
|
| 447 |
+
|
| 448 |
+
* ``contrib.auth.views.login()``, ``logout()``, ``password_change()``,
|
| 449 |
+
``password_change_done()``, ``password_reset()``, ``password_reset_done()``,
|
| 450 |
+
``password_reset_confirm()``, and ``password_reset_complete()`` are removed.
|
| 451 |
+
|
| 452 |
+
* The ``extra_context`` parameter of ``contrib.auth.views.logout_then_login()``
|
| 453 |
+
is removed.
|
| 454 |
+
|
| 455 |
+
* ``django.test.runner.setup_databases()`` is removed.
|
| 456 |
+
|
| 457 |
+
* ``django.utils.translation.string_concat()`` is removed.
|
| 458 |
+
|
| 459 |
+
* ``django.core.cache.backends.memcached.PyLibMCCache`` no longer supports
|
| 460 |
+
passing ``pylibmc`` behavior settings as top-level attributes of ``OPTIONS``.
|
| 461 |
+
|
| 462 |
+
* The ``host`` parameter of ``django.utils.http.is_safe_url()`` is removed.
|
| 463 |
+
|
| 464 |
+
* Silencing of exceptions raised while rendering the ``{% include %}`` template
|
| 465 |
+
tag is removed.
|
| 466 |
+
|
| 467 |
+
* ``DatabaseIntrospection.get_indexes()`` is removed.
|
| 468 |
+
|
| 469 |
+
* The ``authenticate()`` method of authentication backends requires ``request``
|
| 470 |
+
as the first positional argument.
|
| 471 |
+
|
| 472 |
+
* The ``django.db.models.permalink()`` decorator is removed.
|
| 473 |
+
|
| 474 |
+
* The ``USE_ETAGS`` setting is removed. ``CommonMiddleware`` and
|
| 475 |
+
``django.utils.cache.patch_response_headers()`` no longer set ETags.
|
| 476 |
+
|
| 477 |
+
* The ``Model._meta.has_auto_field`` attribute is removed.
|
| 478 |
+
|
| 479 |
+
* ``url()``'s support for inline flags in regular expression groups (``(?i)``,
|
| 480 |
+
``(?L)``, ``(?m)``, ``(?s)``, and ``(?u)``) is removed.
|
| 481 |
+
|
| 482 |
+
* Support for ``Widget.render()`` methods without the ``renderer`` argument
|
| 483 |
+
is removed.
|
testbed/django__django/docs/releases/2.2.10.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.10 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*February 3, 2020*
|
| 6 |
+
|
| 7 |
+
Django 2.2.10 fixes a security issue in 2.2.9.
|
| 8 |
+
|
| 9 |
+
CVE-2020-7471: Potential SQL injection via ``StringAgg(delimiter)``
|
| 10 |
+
===================================================================
|
| 11 |
+
|
| 12 |
+
:class:`~django.contrib.postgres.aggregates.StringAgg` aggregation function was
|
| 13 |
+
subject to SQL injection, using a suitably crafted ``delimiter``.
|
testbed/django__django/docs/releases/2.2.13.txt
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.13 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*June 3, 2020*
|
| 6 |
+
|
| 7 |
+
Django 2.2.13 fixes two security issues and a regression in 2.2.12.
|
| 8 |
+
|
| 9 |
+
CVE-2020-13254: Potential data leakage via malformed memcached keys
|
| 10 |
+
===================================================================
|
| 11 |
+
|
| 12 |
+
In cases where a memcached backend does not perform key validation, passing
|
| 13 |
+
malformed cache keys could result in a key collision, and potential data
|
| 14 |
+
leakage. In order to avoid this vulnerability, key validation is added to the
|
| 15 |
+
memcached cache backends.
|
| 16 |
+
|
| 17 |
+
CVE-2020-13596: Possible XSS via admin ``ForeignKeyRawIdWidget``
|
| 18 |
+
================================================================
|
| 19 |
+
|
| 20 |
+
Query parameters for the admin ``ForeignKeyRawIdWidget`` were not properly URL
|
| 21 |
+
encoded, posing an XSS attack vector. ``ForeignKeyRawIdWidget`` now
|
| 22 |
+
ensures query parameters are correctly URL encoded.
|
| 23 |
+
|
| 24 |
+
Bugfixes
|
| 25 |
+
========
|
| 26 |
+
|
| 27 |
+
* Fixed a regression in Django 2.2.12 that affected translation loading for
|
| 28 |
+
apps providing translations for territorial language variants as well as a
|
| 29 |
+
generic language, where the project has different plural equations for the
|
| 30 |
+
language (:ticket:`31570`).
|
| 31 |
+
|
| 32 |
+
* Tracking a jQuery security release, upgraded the version of jQuery used by
|
| 33 |
+
the admin from 3.3.1 to 3.5.1.
|
testbed/django__django/docs/releases/2.2.15.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.15 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*August 3, 2020*
|
| 6 |
+
|
| 7 |
+
Django 2.2.15 fixes two bugs in 2.2.14.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Allowed setting the ``SameSite`` cookie flag in
|
| 13 |
+
:meth:`.HttpResponse.delete_cookie` (:ticket:`31790`).
|
| 14 |
+
|
| 15 |
+
* Fixed crash when sending emails to addresses with display names longer than
|
| 16 |
+
75 chars on Python 3.6.11+, 3.7.8+, and 3.8.4+ (:ticket:`31784`).
|
testbed/django__django/docs/releases/2.2.16.txt
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.16 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*September 1, 2020*
|
| 6 |
+
|
| 7 |
+
Django 2.2.16 fixes two security issues and two data loss bugs in 2.2.15.
|
| 8 |
+
|
| 9 |
+
CVE-2020-24583: Incorrect permissions on intermediate-level directories on Python 3.7+
|
| 10 |
+
======================================================================================
|
| 11 |
+
|
| 12 |
+
On Python 3.7+, :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS` mode was not
|
| 13 |
+
applied to intermediate-level directories created in the process of uploading
|
| 14 |
+
files and to intermediate-level collected static directories when using the
|
| 15 |
+
:djadmin:`collectstatic` management command.
|
| 16 |
+
|
| 17 |
+
You should review and manually fix permissions on existing intermediate-level
|
| 18 |
+
directories.
|
| 19 |
+
|
| 20 |
+
CVE-2020-24584: Permission escalation in intermediate-level directories of the file system cache on Python 3.7+
|
| 21 |
+
===============================================================================================================
|
| 22 |
+
|
| 23 |
+
On Python 3.7+, the intermediate-level directories of the file system cache had
|
| 24 |
+
the system's standard umask rather than ``0o077`` (no group or others
|
| 25 |
+
permissions).
|
| 26 |
+
|
| 27 |
+
Bugfixes
|
| 28 |
+
========
|
| 29 |
+
|
| 30 |
+
* Fixed a data loss possibility in the
|
| 31 |
+
:meth:`~django.db.models.query.QuerySet.select_for_update()`. When using
|
| 32 |
+
related fields pointing to a proxy model in the ``of`` argument, the
|
| 33 |
+
corresponding model was not locked (:ticket:`31866`).
|
| 34 |
+
|
| 35 |
+
* Fixed a data loss possibility, following a regression in Django 2.0, when
|
| 36 |
+
copying model instances with a cached fields value (:ticket:`31863`).
|
testbed/django__django/docs/releases/2.2.17.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.17 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*November 2, 2020*
|
| 6 |
+
|
| 7 |
+
Django 2.2.17 adds compatibility with Python 3.9.
|
testbed/django__django/docs/releases/2.2.18.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.18 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*February 1, 2021*
|
| 6 |
+
|
| 7 |
+
Django 2.2.18 fixes a security issue with severity "low" in 2.2.17.
|
| 8 |
+
|
| 9 |
+
CVE-2021-3281: Potential directory-traversal via ``archive.extract()``
|
| 10 |
+
======================================================================
|
| 11 |
+
|
| 12 |
+
The ``django.utils.archive.extract()`` function, used by
|
| 13 |
+
:option:`startapp --template` and :option:`startproject --template`, allowed
|
| 14 |
+
directory-traversal via an archive with absolute paths or relative paths with
|
| 15 |
+
dot segments.
|
testbed/django__django/docs/releases/2.2.19.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.19 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*February 19, 2021*
|
| 6 |
+
|
| 7 |
+
Django 2.2.19 fixes a security issue in 2.2.18.
|
| 8 |
+
|
| 9 |
+
CVE-2021-23336: Web cache poisoning via ``django.utils.http.limited_parse_qsl()``
|
| 10 |
+
=================================================================================
|
| 11 |
+
|
| 12 |
+
Django contains a copy of :func:`urllib.parse.parse_qsl` which was added to
|
| 13 |
+
backport some security fixes. A further security fix has been issued recently
|
| 14 |
+
such that ``parse_qsl()`` no longer allows using ``;`` as a query parameter
|
| 15 |
+
separator by default. Django now includes this fix. See :bpo:`42967` for
|
| 16 |
+
further details.
|
testbed/django__django/docs/releases/2.2.20.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.20 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*April 6, 2021*
|
| 6 |
+
|
| 7 |
+
Django 2.2.20 fixes a security issue with severity "low" in 2.2.19.
|
| 8 |
+
|
| 9 |
+
CVE-2021-28658: Potential directory-traversal via uploaded files
|
| 10 |
+
================================================================
|
| 11 |
+
|
| 12 |
+
``MultiPartParser`` allowed directory-traversal via uploaded files with
|
| 13 |
+
suitably crafted file names.
|
| 14 |
+
|
| 15 |
+
Built-in upload handlers were not affected by this vulnerability.
|
testbed/django__django/docs/releases/2.2.23.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 2.2.23 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*May 13, 2021*
|
| 6 |
+
|
| 7 |
+
Django 2.2.23 fixes a regression in 2.2.21.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Fixed a regression in Django 2.2.21 where saving ``FileField`` would raise a
|
| 13 |
+
``SuspiciousFileOperation`` even when a custom
|
| 14 |
+
:attr:`~django.db.models.FileField.upload_to` returns a valid file path
|
| 15 |
+
(:ticket:`32718`).
|
testbed/django__django/docs/releases/2.2.3.txt
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.2.3 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*July 1, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.2.3 fixes a security issue and several bugs in 2.2.2. Also, the latest
|
| 8 |
+
string translations from Transifex are incorporated.
|
| 9 |
+
|
| 10 |
+
CVE-2019-12781: Incorrect HTTP detection with reverse-proxy connecting via HTTPS
|
| 11 |
+
--------------------------------------------------------------------------------
|
| 12 |
+
|
| 13 |
+
When deployed behind a reverse-proxy connecting to Django via HTTPS,
|
| 14 |
+
:attr:`django.http.HttpRequest.scheme` would incorrectly detect client
|
| 15 |
+
requests made via HTTP as using HTTPS. This entails incorrect results for
|
| 16 |
+
:meth:`~django.http.HttpRequest.is_secure`, and
|
| 17 |
+
:meth:`~django.http.HttpRequest.build_absolute_uri`, and that HTTP
|
| 18 |
+
requests would not be redirected to HTTPS in accordance with
|
| 19 |
+
:setting:`SECURE_SSL_REDIRECT`.
|
| 20 |
+
|
| 21 |
+
``HttpRequest.scheme`` now respects :setting:`SECURE_PROXY_SSL_HEADER`, if it is
|
| 22 |
+
configured, and the appropriate header is set on the request, for both HTTP and
|
| 23 |
+
HTTPS requests.
|
| 24 |
+
|
| 25 |
+
If you deploy Django behind a reverse-proxy that forwards HTTP requests, and
|
| 26 |
+
that connects to Django via HTTPS, be sure to verify that your application
|
| 27 |
+
correctly handles code paths relying on ``scheme``, ``is_secure()``,
|
| 28 |
+
``build_absolute_uri()``, and ``SECURE_SSL_REDIRECT``.
|
| 29 |
+
|
| 30 |
+
Bugfixes
|
| 31 |
+
========
|
| 32 |
+
|
| 33 |
+
* Fixed a regression in Django 2.2 where :class:`~django.db.models.Avg`,
|
| 34 |
+
:class:`~django.db.models.StdDev`, and :class:`~django.db.models.Variance`
|
| 35 |
+
crash with ``filter`` argument (:ticket:`30542`).
|
| 36 |
+
|
| 37 |
+
* Fixed a regression in Django 2.2.2 where auto-reloader crashes with
|
| 38 |
+
``AttributeError``, e.g. when using ``ipdb`` (:ticket:`30588`).
|
testbed/django__django/docs/releases/2.2.5.txt
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.2.5 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*September 2, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.2.5 fixes several bugs in 2.2.4.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Relaxed the system check added in Django 2.2 for models to reallow use of the
|
| 13 |
+
same ``db_table`` by multiple models when database routers are installed
|
| 14 |
+
(:ticket:`30673`).
|
| 15 |
+
|
| 16 |
+
* Fixed crash of ``KeyTransform()`` for
|
| 17 |
+
``django.contrib.postgres.fields.JSONField`` and
|
| 18 |
+
:class:`~django.contrib.postgres.fields.HStoreField` when using on
|
| 19 |
+
expressions with params (:ticket:`30672`).
|
| 20 |
+
|
| 21 |
+
* Fixed a regression in Django 2.2 where
|
| 22 |
+
:attr:`ModelAdmin.list_filter <django.contrib.admin.ModelAdmin.list_filter>`
|
| 23 |
+
choices to foreign objects don't respect a model's ``Meta.ordering``
|
| 24 |
+
(:ticket:`30449`).
|
testbed/django__django/docs/releases/2.2.7.txt
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 2.2.7 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*November 4, 2019*
|
| 6 |
+
|
| 7 |
+
Django 2.2.7 fixes several bugs in 2.2.6.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Fixed a crash when using a ``contains``, ``contained_by``, ``has_key``,
|
| 13 |
+
``has_keys``, or ``has_any_keys`` lookup on
|
| 14 |
+
``django.contrib.postgres.fields.JSONField``, if the right or left hand
|
| 15 |
+
side of an expression is a key transform (:ticket:`30826`).
|
| 16 |
+
|
| 17 |
+
* Prevented :option:`migrate --plan` from showing that ``RunPython`` operations
|
| 18 |
+
are irreversible when ``reverse_code`` callables don't have docstrings or
|
| 19 |
+
when showing a forward migration plan (:ticket:`30870`).
|
| 20 |
+
|
| 21 |
+
* Fixed migrations crash on PostgreSQL when adding an
|
| 22 |
+
:class:`~django.db.models.Index` with fields ordering and
|
| 23 |
+
:attr:`~.Index.opclasses` (:ticket:`30903`).
|
| 24 |
+
|
| 25 |
+
* Restored the ability to override
|
| 26 |
+
:meth:`~django.db.models.Model.get_FOO_display` (:ticket:`30931`).
|
testbed/django__django/docs/releases/3.0.10.txt
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
===========================
|
| 2 |
+
Django 3.0.10 release notes
|
| 3 |
+
===========================
|
| 4 |
+
|
| 5 |
+
*September 1, 2020*
|
| 6 |
+
|
| 7 |
+
Django 3.0.10 fixes two security issues and two data loss bugs in 3.0.9.
|
| 8 |
+
|
| 9 |
+
CVE-2020-24583: Incorrect permissions on intermediate-level directories on Python 3.7+
|
| 10 |
+
======================================================================================
|
| 11 |
+
|
| 12 |
+
On Python 3.7+, :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS` mode was not
|
| 13 |
+
applied to intermediate-level directories created in the process of uploading
|
| 14 |
+
files and to intermediate-level collected static directories when using the
|
| 15 |
+
:djadmin:`collectstatic` management command.
|
| 16 |
+
|
| 17 |
+
You should review and manually fix permissions on existing intermediate-level
|
| 18 |
+
directories.
|
| 19 |
+
|
| 20 |
+
CVE-2020-24584: Permission escalation in intermediate-level directories of the file system cache on Python 3.7+
|
| 21 |
+
===============================================================================================================
|
| 22 |
+
|
| 23 |
+
On Python 3.7+, the intermediate-level directories of the file system cache had
|
| 24 |
+
the system's standard umask rather than ``0o077`` (no group or others
|
| 25 |
+
permissions).
|
| 26 |
+
|
| 27 |
+
Bugfixes
|
| 28 |
+
========
|
| 29 |
+
|
| 30 |
+
* Fixed a data loss possibility in the
|
| 31 |
+
:meth:`~django.db.models.query.QuerySet.select_for_update()`. When using
|
| 32 |
+
related fields pointing to a proxy model in the ``of`` argument, the
|
| 33 |
+
corresponding model was not locked (:ticket:`31866`).
|
| 34 |
+
|
| 35 |
+
* Fixed a data loss possibility, following a regression in Django 2.0, when
|
| 36 |
+
copying model instances with a cached fields value (:ticket:`31863`).
|
testbed/django__django/docs/releases/3.0.6.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 3.0.6 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*May 4, 2020*
|
| 6 |
+
|
| 7 |
+
Django 3.0.6 fixes a bug in 3.0.5.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* Fixed a regression in Django 3.0 that caused a crash when filtering a
|
| 13 |
+
``Subquery()`` annotation of a queryset containing a single related field
|
| 14 |
+
against a ``SimpleLazyObject`` (:ticket:`31420`).
|
testbed/django__django/docs/releases/3.1.1.txt
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 3.1.1 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*September 1, 2020*
|
| 6 |
+
|
| 7 |
+
Django 3.1.1 fixes two security issues and several bugs in 3.1.
|
| 8 |
+
|
| 9 |
+
CVE-2020-24583: Incorrect permissions on intermediate-level directories on Python 3.7+
|
| 10 |
+
======================================================================================
|
| 11 |
+
|
| 12 |
+
On Python 3.7+, :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS` mode was not
|
| 13 |
+
applied to intermediate-level directories created in the process of uploading
|
| 14 |
+
files and to intermediate-level collected static directories when using the
|
| 15 |
+
:djadmin:`collectstatic` management command.
|
| 16 |
+
|
| 17 |
+
You should review and manually fix permissions on existing intermediate-level
|
| 18 |
+
directories.
|
| 19 |
+
|
| 20 |
+
CVE-2020-24584: Permission escalation in intermediate-level directories of the file system cache on Python 3.7+
|
| 21 |
+
===============================================================================================================
|
| 22 |
+
|
| 23 |
+
On Python 3.7+, the intermediate-level directories of the file system cache had
|
| 24 |
+
the system's standard umask rather than ``0o077`` (no group or others
|
| 25 |
+
permissions).
|
| 26 |
+
|
| 27 |
+
Bugfixes
|
| 28 |
+
========
|
| 29 |
+
|
| 30 |
+
* Fixed wrapping of translated action labels in the admin's navigation sidebar
|
| 31 |
+
for East Asian languages (:ticket:`31853`).
|
| 32 |
+
|
| 33 |
+
* Fixed wrapping of long model names in the admin's navigation sidebar
|
| 34 |
+
(:ticket:`31854`).
|
| 35 |
+
|
| 36 |
+
* Fixed encoding session data while upgrading multiple instances of the same
|
| 37 |
+
project to Django 3.1 (:ticket:`31864`).
|
| 38 |
+
|
| 39 |
+
* Adjusted admin's navigation sidebar template to reduce debug logging when
|
| 40 |
+
rendering (:ticket:`31865`).
|
| 41 |
+
|
| 42 |
+
* Fixed a data loss possibility in the
|
| 43 |
+
:meth:`~django.db.models.query.QuerySet.select_for_update()`. When using
|
| 44 |
+
related fields pointing to a proxy model in the ``of`` argument, the
|
| 45 |
+
corresponding model was not locked (:ticket:`31866`).
|
| 46 |
+
|
| 47 |
+
* Fixed a data loss possibility, following a regression in Django 2.0, when
|
| 48 |
+
copying model instances with a cached fields value (:ticket:`31863`).
|
| 49 |
+
|
| 50 |
+
* Fixed a regression in Django 3.1 that caused a crash when decoding an invalid
|
| 51 |
+
session data (:ticket:`31895`).
|
| 52 |
+
|
| 53 |
+
* Reverted a deprecation in Django 3.1 that caused a crash when passing
|
| 54 |
+
deprecated keyword arguments to a queryset in
|
| 55 |
+
``TemplateView.get_context_data()`` (:ticket:`31877`).
|
| 56 |
+
|
| 57 |
+
* Enforced thread sensitivity of the :class:`MiddlewareMixin.process_request()
|
| 58 |
+
<django.utils.deprecation.MiddlewareMixin>` and ``process_response()`` hooks
|
| 59 |
+
when in an async context (:ticket:`31905`).
|
| 60 |
+
|
| 61 |
+
* Fixed ``__in`` lookup on key transforms for
|
| 62 |
+
:class:`~django.db.models.JSONField` with MariaDB, MySQL, Oracle, and SQLite
|
| 63 |
+
(:ticket:`31936`).
|
| 64 |
+
|
| 65 |
+
* Fixed a regression in Django 3.1 that caused permission errors in
|
| 66 |
+
``CommonPasswordValidator`` and ``settings.py`` generated by the
|
| 67 |
+
:djadmin:`startproject` command, when user didn't have permissions to all
|
| 68 |
+
intermediate directories in a Django installation path (:ticket:`31912`).
|
| 69 |
+
|
| 70 |
+
* Fixed detecting an async ``get_response`` callable in various builtin
|
| 71 |
+
middlewares (:ticket:`31928`).
|
| 72 |
+
|
| 73 |
+
* Fixed a ``QuerySet.order_by()`` crash on PostgreSQL when ordering and
|
| 74 |
+
grouping by :class:`~django.db.models.JSONField` with a custom
|
| 75 |
+
:attr:`~django.db.models.JSONField.decoder` (:ticket:`31956`). As a
|
| 76 |
+
consequence, fetching a ``JSONField`` with raw SQL now returns a string
|
| 77 |
+
instead of preloaded data. You will need to explicitly call ``json.loads()``
|
| 78 |
+
in such cases.
|
| 79 |
+
|
| 80 |
+
* Fixed a ``QuerySet.delete()`` crash on MySQL, following a performance
|
| 81 |
+
regression in Django 3.1 on MariaDB 10.3.2+, when filtering against an
|
| 82 |
+
aggregate function (:ticket:`31965`).
|
| 83 |
+
|
| 84 |
+
* Fixed a ``django.contrib.admin.EmptyFieldListFilter`` crash when using on
|
| 85 |
+
reverse relations (:ticket:`31952`).
|
| 86 |
+
|
| 87 |
+
* Prevented content overflowing in the admin changelist view when the
|
| 88 |
+
navigation sidebar is enabled (:ticket:`31901`).
|
testbed/django__django/docs/releases/3.1.3.txt
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 3.1.3 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*November 2, 2020*
|
| 6 |
+
|
| 7 |
+
Django 3.1.3 fixes several bugs in 3.1.2 and adds compatibility with Python
|
| 8 |
+
3.9.
|
| 9 |
+
|
| 10 |
+
Bugfixes
|
| 11 |
+
========
|
| 12 |
+
|
| 13 |
+
* Fixed a regression in Django 3.1.2 that caused the incorrect height of the
|
| 14 |
+
admin changelist search bar (:ticket:`32072`).
|
| 15 |
+
|
| 16 |
+
* Fixed a regression in Django 3.1.2 that caused the incorrect width of the
|
| 17 |
+
admin changelist search bar on a filtered page (:ticket:`32091`).
|
| 18 |
+
|
| 19 |
+
* Fixed displaying Unicode characters in
|
| 20 |
+
:class:`forms.JSONField <django.forms.JSONField>` and read-only
|
| 21 |
+
:class:`models.JSONField <django.db.models.JSONField>` values in the admin
|
| 22 |
+
(:ticket:`32080`).
|
| 23 |
+
|
| 24 |
+
* Fixed a regression in Django 3.1 that caused a crash of
|
| 25 |
+
:class:`~django.contrib.postgres.aggregates.ArrayAgg` and
|
| 26 |
+
:class:`~django.contrib.postgres.aggregates.StringAgg` with ``ordering``
|
| 27 |
+
on key transforms for :class:`~django.db.models.JSONField` (:ticket:`32096`).
|
| 28 |
+
|
| 29 |
+
* Fixed a regression in Django 3.1 that caused a crash of ``__in`` lookup when
|
| 30 |
+
using key transforms for :class:`~django.db.models.JSONField` in the lookup
|
| 31 |
+
value (:ticket:`32096`).
|
| 32 |
+
|
| 33 |
+
* Fixed a regression in Django 3.1 that caused a crash of
|
| 34 |
+
:class:`~django.db.models.ExpressionWrapper` with key transforms for
|
| 35 |
+
:class:`~django.db.models.JSONField` (:ticket:`32096`).
|
| 36 |
+
|
| 37 |
+
* Fixed a regression in Django 3.1 that caused a migrations crash on PostgreSQL
|
| 38 |
+
when adding an
|
| 39 |
+
:class:`~django.contrib.postgres.constraints.ExclusionConstraint` with key
|
| 40 |
+
transforms for :class:`~django.db.models.JSONField` in ``expressions``
|
| 41 |
+
(:ticket:`32096`).
|
| 42 |
+
|
| 43 |
+
* Fixed a regression in Django 3.1 where
|
| 44 |
+
:exc:`ProtectedError.protected_objects <django.db.models.ProtectedError>` and
|
| 45 |
+
:exc:`RestrictedError.restricted_objects <django.db.models.RestrictedError>`
|
| 46 |
+
attributes returned iterators instead of :py:class:`set` of objects
|
| 47 |
+
(:ticket:`32107`).
|
| 48 |
+
|
| 49 |
+
* Fixed a regression in Django 3.1.2 that caused incorrect form input layout on
|
| 50 |
+
small screens in the admin change form view (:ticket:`32069`).
|
| 51 |
+
|
| 52 |
+
* Fixed a regression in Django 3.1 that invalidated pre-Django 3.1 password
|
| 53 |
+
reset tokens (:ticket:`32130`).
|
| 54 |
+
|
| 55 |
+
* Added support for ``asgiref`` 3.3 (:ticket:`32128`).
|
| 56 |
+
|
| 57 |
+
* Fixed a regression in Django 3.1 that caused incorrect textarea layout on
|
| 58 |
+
medium-sized screens in the admin change form view with the sidebar open
|
| 59 |
+
(:ticket:`32127`).
|
| 60 |
+
|
| 61 |
+
* Fixed a regression in Django 3.0.7 that didn't use ``Subquery()`` aliases in
|
| 62 |
+
the ``GROUP BY`` clause (:ticket:`32152`).
|
testbed/django__django/docs/releases/3.1.6.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 3.1.6 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*February 1, 2021*
|
| 6 |
+
|
| 7 |
+
Django 3.1.6 fixes a security issue with severity "low" and a bug in 3.1.5.
|
| 8 |
+
|
| 9 |
+
CVE-2021-3281: Potential directory-traversal via ``archive.extract()``
|
| 10 |
+
======================================================================
|
| 11 |
+
|
| 12 |
+
The ``django.utils.archive.extract()`` function, used by
|
| 13 |
+
:option:`startapp --template` and :option:`startproject --template`, allowed
|
| 14 |
+
directory-traversal via an archive with absolute paths or relative paths with
|
| 15 |
+
dot segments.
|
| 16 |
+
|
| 17 |
+
Bugfixes
|
| 18 |
+
========
|
| 19 |
+
|
| 20 |
+
* Fixed an admin layout issue in Django 3.1 where changelist filter controls
|
| 21 |
+
would become squashed (:ticket:`32391`).
|
testbed/django__django/docs/releases/3.1.txt
ADDED
|
@@ -0,0 +1,866 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
========================
|
| 2 |
+
Django 3.1 release notes
|
| 3 |
+
========================
|
| 4 |
+
|
| 5 |
+
*August 4, 2020*
|
| 6 |
+
|
| 7 |
+
Welcome to Django 3.1!
|
| 8 |
+
|
| 9 |
+
These release notes cover the :ref:`new features <whats-new-3.1>`, as well as
|
| 10 |
+
some :ref:`backwards incompatible changes <backwards-incompatible-3.1>` you'll
|
| 11 |
+
want to be aware of when upgrading from Django 3.0 or earlier. We've
|
| 12 |
+
:ref:`dropped some features<removed-features-3.1>` that have reached the end of
|
| 13 |
+
their deprecation cycle, and we've :ref:`begun the deprecation process for
|
| 14 |
+
some features <deprecated-features-3.1>`.
|
| 15 |
+
|
| 16 |
+
See the :doc:`/howto/upgrade-version` guide if you're updating an existing
|
| 17 |
+
project.
|
| 18 |
+
|
| 19 |
+
Python compatibility
|
| 20 |
+
====================
|
| 21 |
+
|
| 22 |
+
Django 3.1 supports Python 3.6, 3.7, 3.8, and 3.9 (as of 3.1.3). We **highly
|
| 23 |
+
recommend** and only officially support the latest release of each series.
|
| 24 |
+
|
| 25 |
+
.. _whats-new-3.1:
|
| 26 |
+
|
| 27 |
+
What's new in Django 3.1
|
| 28 |
+
========================
|
| 29 |
+
|
| 30 |
+
Asynchronous views and middleware support
|
| 31 |
+
-----------------------------------------
|
| 32 |
+
|
| 33 |
+
Django now supports a fully asynchronous request path, including:
|
| 34 |
+
|
| 35 |
+
* :ref:`Asynchronous views <async-views>`
|
| 36 |
+
* :ref:`Asynchronous middleware <async-middleware>`
|
| 37 |
+
* :ref:`Asynchronous tests and test client <async-tests>`
|
| 38 |
+
|
| 39 |
+
To get started with async views, you need to declare a view using
|
| 40 |
+
``async def``::
|
| 41 |
+
|
| 42 |
+
async def my_view(request):
|
| 43 |
+
await asyncio.sleep(0.5)
|
| 44 |
+
return HttpResponse("Hello, async world!")
|
| 45 |
+
|
| 46 |
+
All asynchronous features are supported whether you are running under WSGI or
|
| 47 |
+
ASGI mode. However, there will be performance penalties using async code in
|
| 48 |
+
WSGI mode. You can read more about the specifics in :doc:`/topics/async`
|
| 49 |
+
documentation.
|
| 50 |
+
|
| 51 |
+
You are free to mix async and sync views, middleware, and tests as much as you
|
| 52 |
+
want. Django will ensure that you always end up with the right execution
|
| 53 |
+
context. We expect most projects will keep the majority of their views
|
| 54 |
+
synchronous, and only have a select few running in async mode - but it is
|
| 55 |
+
entirely your choice.
|
| 56 |
+
|
| 57 |
+
Django's ORM, cache layer, and other pieces of code that do long-running
|
| 58 |
+
network calls do not yet support async access. We expect to add support for
|
| 59 |
+
them in upcoming releases. Async views are ideal, however, if you are doing a
|
| 60 |
+
lot of API or HTTP calls inside your view, you can now natively do all those
|
| 61 |
+
HTTP calls in parallel to considerably speed up your view's execution.
|
| 62 |
+
|
| 63 |
+
Asynchronous support should be entirely backwards-compatible and we have tried
|
| 64 |
+
to ensure that it has no speed regressions for your existing, synchronous code.
|
| 65 |
+
It should have no noticeable effect on any existing Django projects.
|
| 66 |
+
|
| 67 |
+
JSONField for all supported database backends
|
| 68 |
+
---------------------------------------------
|
| 69 |
+
|
| 70 |
+
Django now includes :class:`.models.JSONField` and
|
| 71 |
+
:class:`forms.JSONField <django.forms.JSONField>` that can be used on all
|
| 72 |
+
supported database backends. Both fields support the use of custom JSON
|
| 73 |
+
encoders and decoders. The model field supports the introspection,
|
| 74 |
+
:ref:`lookups, and transforms <querying-jsonfield>` that were previously
|
| 75 |
+
PostgreSQL-only::
|
| 76 |
+
|
| 77 |
+
from django.db import models
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class ContactInfo(models.Model):
|
| 81 |
+
data = models.JSONField()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
ContactInfo.objects.create(
|
| 85 |
+
data={
|
| 86 |
+
"name": "John",
|
| 87 |
+
"cities": ["London", "Cambridge"],
|
| 88 |
+
"pets": {"dogs": ["Rufus", "Meg"]},
|
| 89 |
+
}
|
| 90 |
+
)
|
| 91 |
+
ContactInfo.objects.filter(
|
| 92 |
+
data__name="John",
|
| 93 |
+
data__pets__has_key="dogs",
|
| 94 |
+
data__cities__contains="London",
|
| 95 |
+
).delete()
|
| 96 |
+
|
| 97 |
+
If your project uses ``django.contrib.postgres.fields.JSONField``, plus the
|
| 98 |
+
related form field and transforms, you should adjust to use the new fields,
|
| 99 |
+
and generate and apply a database migration. For now, the old fields and
|
| 100 |
+
transforms are left as a reference to the new ones and are :ref:`deprecated as
|
| 101 |
+
of this release <deprecated-jsonfield>`.
|
| 102 |
+
|
| 103 |
+
.. _default-hashing-algorithm-usage:
|
| 104 |
+
|
| 105 |
+
``DEFAULT_HASHING_ALGORITHM`` settings
|
| 106 |
+
--------------------------------------
|
| 107 |
+
|
| 108 |
+
The new ``DEFAULT_HASHING_ALGORITHM`` transitional setting allows specifying
|
| 109 |
+
the default hashing algorithm to use for encoding cookies, password reset
|
| 110 |
+
tokens in the admin site, user sessions, and signatures created by
|
| 111 |
+
:class:`django.core.signing.Signer` and :meth:`django.core.signing.dumps`.
|
| 112 |
+
|
| 113 |
+
Support for SHA-256 was added in Django 3.1. If you are upgrading multiple
|
| 114 |
+
instances of the same project to Django 3.1, you should set
|
| 115 |
+
``DEFAULT_HASHING_ALGORITHM`` to ``'sha1'`` during the transition, in order to
|
| 116 |
+
allow compatibility with the older versions of Django. Note that this requires
|
| 117 |
+
Django 3.1.1+. Once the transition to 3.1 is complete you can stop overriding
|
| 118 |
+
``DEFAULT_HASHING_ALGORITHM``.
|
| 119 |
+
|
| 120 |
+
This setting is deprecated as of this release, because support for tokens,
|
| 121 |
+
cookies, sessions, and signatures that use SHA-1 algorithm will be removed in
|
| 122 |
+
Django 4.0.
|
| 123 |
+
|
| 124 |
+
Minor features
|
| 125 |
+
--------------
|
| 126 |
+
|
| 127 |
+
:mod:`django.contrib.admin`
|
| 128 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 129 |
+
|
| 130 |
+
* The new ``django.contrib.admin.EmptyFieldListFilter`` for
|
| 131 |
+
:attr:`.ModelAdmin.list_filter` allows filtering on empty values (empty
|
| 132 |
+
strings and nulls) in the admin changelist view.
|
| 133 |
+
|
| 134 |
+
* Filters in the right sidebar of the admin changelist view now contain a link
|
| 135 |
+
to clear all filters.
|
| 136 |
+
|
| 137 |
+
* The admin now has a sidebar on larger screens for easier navigation. It is
|
| 138 |
+
enabled by default but can be disabled by using a custom ``AdminSite`` and
|
| 139 |
+
setting :attr:`.AdminSite.enable_nav_sidebar` to ``False``.
|
| 140 |
+
|
| 141 |
+
Rendering the sidebar requires access to the current request in order to set
|
| 142 |
+
CSS and ARIA role affordances. This requires using
|
| 143 |
+
``'django.template.context_processors.request'`` in the
|
| 144 |
+
``'context_processors'`` option of :setting:`OPTIONS <TEMPLATES-OPTIONS>`.
|
| 145 |
+
|
| 146 |
+
* Initially empty ``extra`` inlines can now be removed, in the same way as
|
| 147 |
+
dynamically created ones.
|
| 148 |
+
|
| 149 |
+
* ``XRegExp`` is upgraded from version 2.0.0 to 3.2.0.
|
| 150 |
+
|
| 151 |
+
* jQuery is upgraded from version 3.4.1 to 3.5.1.
|
| 152 |
+
|
| 153 |
+
* Select2 library is upgraded from version 4.0.7 to 4.0.13.
|
| 154 |
+
|
| 155 |
+
:mod:`django.contrib.auth`
|
| 156 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 157 |
+
|
| 158 |
+
* The default iteration count for the PBKDF2 password hasher is increased from
|
| 159 |
+
180,000 to 216,000.
|
| 160 |
+
|
| 161 |
+
* The new :setting:`PASSWORD_RESET_TIMEOUT` setting allows defining the number
|
| 162 |
+
of seconds a password reset link is valid for. This is encouraged instead of
|
| 163 |
+
the deprecated ``PASSWORD_RESET_TIMEOUT_DAYS`` setting, which will be removed
|
| 164 |
+
in Django 4.0.
|
| 165 |
+
|
| 166 |
+
* The password reset mechanism now uses the SHA-256 hashing algorithm. Support
|
| 167 |
+
for tokens that use the old hashing algorithm remains until Django 4.0.
|
| 168 |
+
|
| 169 |
+
* :meth:`.AbstractBaseUser.get_session_auth_hash` now uses the SHA-256 hashing
|
| 170 |
+
algorithm. Support for user sessions that use the old hashing algorithm
|
| 171 |
+
remains until Django 4.0.
|
| 172 |
+
|
| 173 |
+
:mod:`django.contrib.contenttypes`
|
| 174 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 175 |
+
|
| 176 |
+
* The new :option:`remove_stale_contenttypes --include-stale-apps` option
|
| 177 |
+
allows removing stale content types from previously installed apps that have
|
| 178 |
+
been removed from :setting:`INSTALLED_APPS`.
|
| 179 |
+
|
| 180 |
+
:mod:`django.contrib.gis`
|
| 181 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 182 |
+
|
| 183 |
+
* :lookup:`relate` lookup is now supported on MariaDB.
|
| 184 |
+
|
| 185 |
+
* Added the :attr:`.LinearRing.is_counterclockwise` property.
|
| 186 |
+
|
| 187 |
+
* :class:`~django.contrib.gis.db.models.functions.AsGeoJSON` is now supported
|
| 188 |
+
on Oracle.
|
| 189 |
+
|
| 190 |
+
* Added the :class:`~django.contrib.gis.db.models.functions.AsWKB` and
|
| 191 |
+
:class:`~django.contrib.gis.db.models.functions.AsWKT` functions.
|
| 192 |
+
|
| 193 |
+
* Added support for PostGIS 3 and GDAL 3.
|
| 194 |
+
|
| 195 |
+
:mod:`django.contrib.humanize`
|
| 196 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 197 |
+
|
| 198 |
+
* :tfilter:`intword` template filter now supports negative integers.
|
| 199 |
+
|
| 200 |
+
:mod:`django.contrib.postgres`
|
| 201 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 202 |
+
|
| 203 |
+
* The new :class:`~django.contrib.postgres.indexes.BloomIndex` class allows
|
| 204 |
+
creating ``bloom`` indexes in the database. The new
|
| 205 |
+
:class:`~django.contrib.postgres.operations.BloomExtension` migration
|
| 206 |
+
operation installs the ``bloom`` extension to add support for this index.
|
| 207 |
+
|
| 208 |
+
* :meth:`~django.db.models.Model.get_FOO_display` now supports
|
| 209 |
+
:class:`~django.contrib.postgres.fields.ArrayField` and
|
| 210 |
+
:class:`~django.contrib.postgres.fields.RangeField`.
|
| 211 |
+
|
| 212 |
+
* The new :lookup:`rangefield.lower_inc`, :lookup:`rangefield.lower_inf`,
|
| 213 |
+
:lookup:`rangefield.upper_inc`, and :lookup:`rangefield.upper_inf` lookups
|
| 214 |
+
allow querying :class:`~django.contrib.postgres.fields.RangeField` by a bound
|
| 215 |
+
type.
|
| 216 |
+
|
| 217 |
+
* :lookup:`rangefield.contained_by` now supports
|
| 218 |
+
:class:`~django.db.models.SmallAutoField`,
|
| 219 |
+
:class:`~django.db.models.AutoField`,
|
| 220 |
+
:class:`~django.db.models.BigAutoField`,
|
| 221 |
+
:class:`~django.db.models.SmallIntegerField`, and
|
| 222 |
+
:class:`~django.db.models.DecimalField`.
|
| 223 |
+
|
| 224 |
+
* :class:`~django.contrib.postgres.search.SearchQuery` now supports
|
| 225 |
+
``'websearch'`` search type on PostgreSQL 11+.
|
| 226 |
+
|
| 227 |
+
* :class:`SearchQuery.value <django.contrib.postgres.search.SearchQuery>` now
|
| 228 |
+
supports query expressions.
|
| 229 |
+
|
| 230 |
+
* The new :class:`~django.contrib.postgres.search.SearchHeadline` class allows
|
| 231 |
+
highlighting search results.
|
| 232 |
+
|
| 233 |
+
* :lookup:`search` lookup now supports query expressions.
|
| 234 |
+
|
| 235 |
+
* The new ``cover_density`` parameter of
|
| 236 |
+
:class:`~django.contrib.postgres.search.SearchRank` allows ranking by cover
|
| 237 |
+
density.
|
| 238 |
+
|
| 239 |
+
* The new ``normalization`` parameter of
|
| 240 |
+
:class:`~django.contrib.postgres.search.SearchRank` allows rank
|
| 241 |
+
normalization.
|
| 242 |
+
|
| 243 |
+
* The new :attr:`.ExclusionConstraint.deferrable` attribute allows creating
|
| 244 |
+
deferrable exclusion constraints.
|
| 245 |
+
|
| 246 |
+
:mod:`django.contrib.sessions`
|
| 247 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 248 |
+
|
| 249 |
+
* The :setting:`SESSION_COOKIE_SAMESITE` setting now allows ``'None'`` (string)
|
| 250 |
+
value to explicitly state that the cookie is sent with all same-site and
|
| 251 |
+
cross-site requests.
|
| 252 |
+
|
| 253 |
+
:mod:`django.contrib.staticfiles`
|
| 254 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 255 |
+
|
| 256 |
+
* The :setting:`STATICFILES_DIRS` setting now supports :class:`pathlib.Path`.
|
| 257 |
+
|
| 258 |
+
Cache
|
| 259 |
+
~~~~~
|
| 260 |
+
|
| 261 |
+
* The :func:`~django.views.decorators.cache.cache_control` decorator and
|
| 262 |
+
:func:`~django.utils.cache.patch_cache_control` method now support multiple
|
| 263 |
+
field names in the ``no-cache`` directive for the ``Cache-Control`` header,
|
| 264 |
+
according to :rfc:`7234#section-5.2.2.2`.
|
| 265 |
+
|
| 266 |
+
* :meth:`~django.core.cache.cache.delete` now returns ``True`` if the key was
|
| 267 |
+
successfully deleted, ``False`` otherwise.
|
| 268 |
+
|
| 269 |
+
CSRF
|
| 270 |
+
~~~~
|
| 271 |
+
|
| 272 |
+
* The :setting:`CSRF_COOKIE_SAMESITE` setting now allows ``'None'`` (string)
|
| 273 |
+
value to explicitly state that the cookie is sent with all same-site and
|
| 274 |
+
cross-site requests.
|
| 275 |
+
|
| 276 |
+
Email
|
| 277 |
+
~~~~~
|
| 278 |
+
|
| 279 |
+
* The :setting:`EMAIL_FILE_PATH` setting, used by the :ref:`file email backend
|
| 280 |
+
<topic-email-file-backend>`, now supports :class:`pathlib.Path`.
|
| 281 |
+
|
| 282 |
+
Error Reporting
|
| 283 |
+
~~~~~~~~~~~~~~~
|
| 284 |
+
|
| 285 |
+
* :class:`django.views.debug.SafeExceptionReporterFilter` now filters sensitive
|
| 286 |
+
values from ``request.META`` in exception reports.
|
| 287 |
+
|
| 288 |
+
* The new :attr:`.SafeExceptionReporterFilter.cleansed_substitute` and
|
| 289 |
+
:attr:`.SafeExceptionReporterFilter.hidden_settings` attributes allow
|
| 290 |
+
customization of sensitive settings and ``request.META`` filtering in
|
| 291 |
+
exception reports.
|
| 292 |
+
|
| 293 |
+
* The technical 404 debug view now respects
|
| 294 |
+
:setting:`DEFAULT_EXCEPTION_REPORTER_FILTER` when applying settings
|
| 295 |
+
filtering.
|
| 296 |
+
|
| 297 |
+
* The new :setting:`DEFAULT_EXCEPTION_REPORTER` allows providing a
|
| 298 |
+
:class:`django.views.debug.ExceptionReporter` subclass to customize exception
|
| 299 |
+
report generation. See :ref:`custom-error-reports` for details.
|
| 300 |
+
|
| 301 |
+
File Storage
|
| 302 |
+
~~~~~~~~~~~~
|
| 303 |
+
|
| 304 |
+
* ``FileSystemStorage.save()`` method now supports :class:`pathlib.Path`.
|
| 305 |
+
|
| 306 |
+
* :class:`~django.db.models.FileField` and
|
| 307 |
+
:class:`~django.db.models.ImageField` now accept a callable for ``storage``.
|
| 308 |
+
This allows you to modify the used storage at runtime, selecting different
|
| 309 |
+
storages for different environments, for example.
|
| 310 |
+
|
| 311 |
+
Forms
|
| 312 |
+
~~~~~
|
| 313 |
+
|
| 314 |
+
* :class:`~django.forms.ModelChoiceIterator`, used by
|
| 315 |
+
:class:`~django.forms.ModelChoiceField` and
|
| 316 |
+
:class:`~django.forms.ModelMultipleChoiceField`, now uses
|
| 317 |
+
:class:`~django.forms.ModelChoiceIteratorValue` that can be used by widgets
|
| 318 |
+
to access model instances. See :ref:`iterating-relationship-choices` for
|
| 319 |
+
details.
|
| 320 |
+
|
| 321 |
+
* :class:`django.forms.DateTimeField` now accepts dates in a subset of ISO 8601
|
| 322 |
+
datetime formats, including optional timezone, e.g. ``2019-10-10T06:47``,
|
| 323 |
+
``2019-10-10T06:47:23+04:00``, or ``2019-10-10T06:47:23Z``. The timezone will
|
| 324 |
+
always be retained if provided, with timezone-aware datetimes being returned
|
| 325 |
+
even when :setting:`USE_TZ` is ``False``.
|
| 326 |
+
|
| 327 |
+
Additionally, ``DateTimeField`` now uses ``DATE_INPUT_FORMATS`` in addition
|
| 328 |
+
to ``DATETIME_INPUT_FORMATS`` when converting a field input to a ``datetime``
|
| 329 |
+
value.
|
| 330 |
+
|
| 331 |
+
* :attr:`.MultiWidget.widgets` now accepts a dictionary which allows
|
| 332 |
+
customizing subwidget ``name`` attributes.
|
| 333 |
+
|
| 334 |
+
* The new :attr:`.BoundField.widget_type` property can be used to dynamically
|
| 335 |
+
adjust form rendering based upon the widget type.
|
| 336 |
+
|
| 337 |
+
Internationalization
|
| 338 |
+
~~~~~~~~~~~~~~~~~~~~
|
| 339 |
+
|
| 340 |
+
* The :setting:`LANGUAGE_COOKIE_SAMESITE` setting now allows ``'None'``
|
| 341 |
+
(string) value to explicitly state that the cookie is sent with all same-site
|
| 342 |
+
and cross-site requests.
|
| 343 |
+
|
| 344 |
+
* Added support and translations for the Algerian Arabic, Igbo, Kyrgyz, Tajik,
|
| 345 |
+
and Turkmen languages.
|
| 346 |
+
|
| 347 |
+
Management Commands
|
| 348 |
+
~~~~~~~~~~~~~~~~~~~
|
| 349 |
+
|
| 350 |
+
* The new :option:`check --database` option allows specifying database aliases
|
| 351 |
+
for running the ``database`` system checks. Previously these checks were
|
| 352 |
+
enabled for all configured :setting:`DATABASES` by passing the ``database``
|
| 353 |
+
tag to the command.
|
| 354 |
+
|
| 355 |
+
* The new :option:`migrate --check` option makes the command exit with a
|
| 356 |
+
non-zero status when unapplied migrations are detected.
|
| 357 |
+
|
| 358 |
+
* The new ``returncode`` argument for
|
| 359 |
+
:attr:`~django.core.management.CommandError` allows customizing the exit
|
| 360 |
+
status for management commands.
|
| 361 |
+
|
| 362 |
+
* The new :option:`dbshell -- ARGUMENTS <dbshell -->` option allows passing
|
| 363 |
+
extra arguments to the command-line client for the database.
|
| 364 |
+
|
| 365 |
+
* The :djadmin:`flush` and :djadmin:`sqlflush` commands now include SQL to
|
| 366 |
+
reset sequences on SQLite.
|
| 367 |
+
|
| 368 |
+
Models
|
| 369 |
+
~~~~~~
|
| 370 |
+
|
| 371 |
+
* The new :class:`~django.db.models.functions.ExtractIsoWeekDay` function
|
| 372 |
+
extracts ISO-8601 week days from :class:`~django.db.models.DateField` and
|
| 373 |
+
:class:`~django.db.models.DateTimeField`, and the new :lookup:`iso_week_day`
|
| 374 |
+
lookup allows querying by an ISO-8601 day of week.
|
| 375 |
+
|
| 376 |
+
* :meth:`.QuerySet.explain` now supports:
|
| 377 |
+
|
| 378 |
+
* ``TREE`` format on MySQL 8.0.16+,
|
| 379 |
+
* ``analyze`` option on MySQL 8.0.18+ and MariaDB.
|
| 380 |
+
|
| 381 |
+
* Added :class:`~django.db.models.PositiveBigIntegerField` which acts much like
|
| 382 |
+
a :class:`~django.db.models.PositiveIntegerField` except that it only allows
|
| 383 |
+
values under a certain (database-dependent) limit. Values from ``0`` to
|
| 384 |
+
``9223372036854775807`` are safe in all databases supported by Django.
|
| 385 |
+
|
| 386 |
+
* The new :class:`~django.db.models.RESTRICT` option for
|
| 387 |
+
:attr:`~django.db.models.ForeignKey.on_delete` argument of ``ForeignKey`` and
|
| 388 |
+
``OneToOneField`` emulates the behavior of the SQL constraint ``ON DELETE
|
| 389 |
+
RESTRICT``.
|
| 390 |
+
|
| 391 |
+
* :attr:`.CheckConstraint.check` now supports boolean expressions.
|
| 392 |
+
|
| 393 |
+
* The :meth:`.RelatedManager.add`, :meth:`~.RelatedManager.create`, and
|
| 394 |
+
:meth:`~.RelatedManager.set` methods now accept callables as values in the
|
| 395 |
+
``through_defaults`` argument.
|
| 396 |
+
|
| 397 |
+
* The new ``is_dst`` parameter of the :meth:`.QuerySet.datetimes` determines
|
| 398 |
+
the treatment of nonexistent and ambiguous datetimes.
|
| 399 |
+
|
| 400 |
+
* The new :class:`~django.db.models.F` expression ``bitxor()`` method allows
|
| 401 |
+
:ref:`bitwise XOR operation <using-f-expressions-in-filters>`.
|
| 402 |
+
|
| 403 |
+
* :meth:`.QuerySet.bulk_create` now sets the primary key on objects when using
|
| 404 |
+
MariaDB 10.5+.
|
| 405 |
+
|
| 406 |
+
* The ``DatabaseOperations.sql_flush()`` method now generates more efficient
|
| 407 |
+
SQL on MySQL by using ``DELETE`` instead of ``TRUNCATE`` statements for
|
| 408 |
+
tables which don't require resetting sequences.
|
| 409 |
+
|
| 410 |
+
* SQLite functions are now marked as :py:meth:`deterministic
|
| 411 |
+
<sqlite3.Connection.create_function>` on Python 3.8+. This allows using them
|
| 412 |
+
in check constraints and partial indexes.
|
| 413 |
+
|
| 414 |
+
* The new :attr:`.UniqueConstraint.deferrable` attribute allows creating
|
| 415 |
+
deferrable unique constraints.
|
| 416 |
+
|
| 417 |
+
Pagination
|
| 418 |
+
~~~~~~~~~~
|
| 419 |
+
|
| 420 |
+
* :class:`~django.core.paginator.Paginator` can now be iterated over to yield
|
| 421 |
+
its pages.
|
| 422 |
+
|
| 423 |
+
Requests and Responses
|
| 424 |
+
~~~~~~~~~~~~~~~~~~~~~~
|
| 425 |
+
|
| 426 |
+
* If :setting:`ALLOWED_HOSTS` is empty and ``DEBUG=True``, subdomains of
|
| 427 |
+
localhost are now allowed in the ``Host`` header, e.g. ``static.localhost``.
|
| 428 |
+
|
| 429 |
+
* :meth:`.HttpResponse.set_cookie` and :meth:`.HttpResponse.set_signed_cookie`
|
| 430 |
+
now allow using ``samesite='None'`` (string) to explicitly state that the
|
| 431 |
+
cookie is sent with all same-site and cross-site requests.
|
| 432 |
+
|
| 433 |
+
* The new :meth:`.HttpRequest.accepts` method returns whether the request
|
| 434 |
+
accepts the given MIME type according to the ``Accept`` HTTP header.
|
| 435 |
+
|
| 436 |
+
.. _whats-new-security-3.1:
|
| 437 |
+
|
| 438 |
+
Security
|
| 439 |
+
~~~~~~~~
|
| 440 |
+
|
| 441 |
+
* The :setting:`SECURE_REFERRER_POLICY` setting now defaults to
|
| 442 |
+
``'same-origin'``. With this configured,
|
| 443 |
+
:class:`~django.middleware.security.SecurityMiddleware` sets the
|
| 444 |
+
:ref:`referrer-policy` header to ``same-origin`` on all responses that do not
|
| 445 |
+
already have it. This prevents the ``Referer`` header being sent to other
|
| 446 |
+
origins. If you need the previous behavior, explicitly set
|
| 447 |
+
:setting:`SECURE_REFERRER_POLICY` to ``None``.
|
| 448 |
+
|
| 449 |
+
* The default algorithm of :class:`django.core.signing.Signer`,
|
| 450 |
+
:meth:`django.core.signing.loads`, and :meth:`django.core.signing.dumps` is
|
| 451 |
+
changed to the SHA-256. Support for signatures made with the old SHA-1
|
| 452 |
+
algorithm remains until Django 4.0.
|
| 453 |
+
|
| 454 |
+
Also, the new ``algorithm`` parameter of the
|
| 455 |
+
:class:`~django.core.signing.Signer` allows customizing the hashing
|
| 456 |
+
algorithm.
|
| 457 |
+
|
| 458 |
+
Templates
|
| 459 |
+
~~~~~~~~~
|
| 460 |
+
|
| 461 |
+
* The renamed :ttag:`translate` and :ttag:`blocktranslate` template tags are
|
| 462 |
+
introduced for internationalization in template code. The older :ttag:`trans`
|
| 463 |
+
and :ttag:`blocktrans` template tags aliases continue to work, and will be
|
| 464 |
+
retained for the foreseeable future.
|
| 465 |
+
|
| 466 |
+
* The :ttag:`include` template tag now accepts iterables of template names.
|
| 467 |
+
|
| 468 |
+
Tests
|
| 469 |
+
~~~~~
|
| 470 |
+
|
| 471 |
+
* :class:`~django.test.SimpleTestCase` now implements the ``debug()`` method to
|
| 472 |
+
allow running a test without collecting the result and catching exceptions.
|
| 473 |
+
This can be used to support running tests under a debugger.
|
| 474 |
+
|
| 475 |
+
* The new :setting:`MIGRATE <TEST_MIGRATE>` test database setting allows
|
| 476 |
+
disabling of migrations during a test database creation.
|
| 477 |
+
|
| 478 |
+
* Django test runner now supports a :option:`test --buffer` option to discard
|
| 479 |
+
output for passing tests.
|
| 480 |
+
|
| 481 |
+
* :class:`~django.test.runner.DiscoverRunner` now skips running the system
|
| 482 |
+
checks on databases not :ref:`referenced by tests<testing-multi-db>`.
|
| 483 |
+
|
| 484 |
+
* :class:`~django.test.TransactionTestCase` teardown is now faster on MySQL
|
| 485 |
+
due to :djadmin:`flush` command improvements. As a side effect the latter
|
| 486 |
+
doesn't automatically reset sequences on teardown anymore. Enable
|
| 487 |
+
:attr:`.TransactionTestCase.reset_sequences` if your tests require this
|
| 488 |
+
feature.
|
| 489 |
+
|
| 490 |
+
URLs
|
| 491 |
+
~~~~
|
| 492 |
+
|
| 493 |
+
* :ref:`Path converters <registering-custom-path-converters>` can now raise
|
| 494 |
+
``ValueError`` in ``to_url()`` to indicate no match when reversing URLs.
|
| 495 |
+
|
| 496 |
+
Utilities
|
| 497 |
+
~~~~~~~~~
|
| 498 |
+
|
| 499 |
+
* :func:`~django.utils.encoding.filepath_to_uri` now supports
|
| 500 |
+
:class:`pathlib.Path`.
|
| 501 |
+
|
| 502 |
+
* :func:`~django.utils.dateparse.parse_duration` now supports comma separators
|
| 503 |
+
for decimal fractions in the ISO 8601 format.
|
| 504 |
+
|
| 505 |
+
* :func:`~django.utils.dateparse.parse_datetime`,
|
| 506 |
+
:func:`~django.utils.dateparse.parse_duration`, and
|
| 507 |
+
:func:`~django.utils.dateparse.parse_time` now support comma separators for
|
| 508 |
+
milliseconds.
|
| 509 |
+
|
| 510 |
+
Miscellaneous
|
| 511 |
+
~~~~~~~~~~~~~
|
| 512 |
+
|
| 513 |
+
* The SQLite backend now supports :class:`pathlib.Path` for the ``NAME``
|
| 514 |
+
setting.
|
| 515 |
+
|
| 516 |
+
* The ``settings.py`` generated by the :djadmin:`startproject` command now uses
|
| 517 |
+
:class:`pathlib.Path` instead of :mod:`os.path` for building filesystem
|
| 518 |
+
paths.
|
| 519 |
+
|
| 520 |
+
* The :setting:`TIME_ZONE <DATABASE-TIME_ZONE>` setting is now allowed on
|
| 521 |
+
databases that support time zones.
|
| 522 |
+
|
| 523 |
+
.. _backwards-incompatible-3.1:
|
| 524 |
+
|
| 525 |
+
Backwards incompatible changes in 3.1
|
| 526 |
+
=====================================
|
| 527 |
+
|
| 528 |
+
Database backend API
|
| 529 |
+
--------------------
|
| 530 |
+
|
| 531 |
+
This section describes changes that may be needed in third-party database
|
| 532 |
+
backends.
|
| 533 |
+
|
| 534 |
+
* ``DatabaseOperations.fetch_returned_insert_columns()`` now requires an
|
| 535 |
+
additional ``returning_params`` argument.
|
| 536 |
+
|
| 537 |
+
* ``connection.timezone`` property is now ``'UTC'`` by default, or the
|
| 538 |
+
:setting:`TIME_ZONE <DATABASE-TIME_ZONE>` when :setting:`USE_TZ` is ``True``
|
| 539 |
+
on databases that support time zones. Previously, it was ``None`` on
|
| 540 |
+
databases that support time zones.
|
| 541 |
+
|
| 542 |
+
* ``connection._nodb_connection`` property is changed to the
|
| 543 |
+
``connection._nodb_cursor()`` method and now returns a context manager that
|
| 544 |
+
yields a cursor and automatically closes the cursor and connection upon
|
| 545 |
+
exiting the ``with`` statement.
|
| 546 |
+
|
| 547 |
+
* ``DatabaseClient.runshell()`` now requires an additional ``parameters``
|
| 548 |
+
argument as a list of extra arguments to pass on to the command-line client.
|
| 549 |
+
|
| 550 |
+
* The ``sequences`` positional argument of ``DatabaseOperations.sql_flush()``
|
| 551 |
+
is replaced by the boolean keyword-only argument ``reset_sequences``. If
|
| 552 |
+
``True``, the sequences of the truncated tables will be reset.
|
| 553 |
+
|
| 554 |
+
* The ``allow_cascade`` argument of ``DatabaseOperations.sql_flush()`` is now a
|
| 555 |
+
keyword-only argument.
|
| 556 |
+
|
| 557 |
+
* The ``using`` positional argument of
|
| 558 |
+
``DatabaseOperations.execute_sql_flush()`` is removed. The method now uses
|
| 559 |
+
the database of the called instance.
|
| 560 |
+
|
| 561 |
+
* Third-party database backends must implement support for ``JSONField`` or set
|
| 562 |
+
``DatabaseFeatures.supports_json_field`` to ``False``. If storing primitives
|
| 563 |
+
is not supported, set ``DatabaseFeatures.supports_primitives_in_json_field``
|
| 564 |
+
to ``False``. If there is a true datatype for JSON, set
|
| 565 |
+
``DatabaseFeatures.has_native_json_field`` to ``True``. If
|
| 566 |
+
:lookup:`jsonfield.contains` and :lookup:`jsonfield.contained_by` are not
|
| 567 |
+
supported, set ``DatabaseFeatures.supports_json_field_contains`` to
|
| 568 |
+
``False``.
|
| 569 |
+
|
| 570 |
+
* Third party database backends must implement introspection for ``JSONField``
|
| 571 |
+
or set ``can_introspect_json_field`` to ``False``.
|
| 572 |
+
|
| 573 |
+
Dropped support for MariaDB 10.1
|
| 574 |
+
--------------------------------
|
| 575 |
+
|
| 576 |
+
Upstream support for MariaDB 10.1 ends in October 2020. Django 3.1 supports
|
| 577 |
+
MariaDB 10.2 and higher.
|
| 578 |
+
|
| 579 |
+
``contrib.admin`` browser support
|
| 580 |
+
---------------------------------
|
| 581 |
+
|
| 582 |
+
The admin no longer supports the legacy Internet Explorer browser. See
|
| 583 |
+
:ref:`the admin FAQ <admin-browser-support>` for details on supported browsers.
|
| 584 |
+
|
| 585 |
+
:attr:`AbstractUser.first_name <django.contrib.auth.models.User.first_name>` ``max_length`` increased to 150
|
| 586 |
+
------------------------------------------------------------------------------------------------------------
|
| 587 |
+
|
| 588 |
+
A migration for :attr:`django.contrib.auth.models.User.first_name` is included.
|
| 589 |
+
If you have a custom user model inheriting from ``AbstractUser``, you'll need
|
| 590 |
+
to generate and apply a database migration for your user model.
|
| 591 |
+
|
| 592 |
+
If you want to preserve the 30 character limit for first names, use a custom
|
| 593 |
+
form::
|
| 594 |
+
|
| 595 |
+
from django import forms
|
| 596 |
+
from django.contrib.auth.forms import UserChangeForm
|
| 597 |
+
|
| 598 |
+
|
| 599 |
+
class MyUserChangeForm(UserChangeForm):
|
| 600 |
+
first_name = forms.CharField(max_length=30, required=False)
|
| 601 |
+
|
| 602 |
+
If you wish to keep this restriction in the admin when editing users, set
|
| 603 |
+
``UserAdmin.form`` to use this form::
|
| 604 |
+
|
| 605 |
+
from django.contrib.auth.admin import UserAdmin
|
| 606 |
+
from django.contrib.auth.models import User
|
| 607 |
+
|
| 608 |
+
|
| 609 |
+
class MyUserAdmin(UserAdmin):
|
| 610 |
+
form = MyUserChangeForm
|
| 611 |
+
|
| 612 |
+
|
| 613 |
+
admin.site.unregister(User)
|
| 614 |
+
admin.site.register(User, MyUserAdmin)
|
| 615 |
+
|
| 616 |
+
Miscellaneous
|
| 617 |
+
-------------
|
| 618 |
+
|
| 619 |
+
* The cache keys used by :ttag:`cache` and generated by
|
| 620 |
+
:func:`~django.core.cache.utils.make_template_fragment_key` are different
|
| 621 |
+
from the keys generated by older versions of Django. After upgrading to
|
| 622 |
+
Django 3.1, the first request to any previously cached template fragment will
|
| 623 |
+
be a cache miss.
|
| 624 |
+
|
| 625 |
+
* The logic behind the decision to return a redirection fallback or a 204 HTTP
|
| 626 |
+
response from the :func:`~django.views.i18n.set_language` view is now based
|
| 627 |
+
on the ``Accept`` HTTP header instead of the ``X-Requested-With`` HTTP header
|
| 628 |
+
presence.
|
| 629 |
+
|
| 630 |
+
* The compatibility imports of ``django.core.exceptions.EmptyResultSet`` in
|
| 631 |
+
``django.db.models.query``, ``django.db.models.sql``, and
|
| 632 |
+
``django.db.models.sql.datastructures`` are removed.
|
| 633 |
+
|
| 634 |
+
* The compatibility import of ``django.core.exceptions.FieldDoesNotExist`` in
|
| 635 |
+
``django.db.models.fields`` is removed.
|
| 636 |
+
|
| 637 |
+
* The compatibility imports of ``django.forms.utils.pretty_name()`` and
|
| 638 |
+
``django.forms.boundfield.BoundField`` in ``django.forms.forms`` are removed.
|
| 639 |
+
|
| 640 |
+
* The compatibility imports of ``Context``, ``ContextPopException``, and
|
| 641 |
+
``RequestContext`` in ``django.template.base`` are removed.
|
| 642 |
+
|
| 643 |
+
* The compatibility import of
|
| 644 |
+
``django.contrib.admin.helpers.ACTION_CHECKBOX_NAME`` in
|
| 645 |
+
``django.contrib.admin`` is removed.
|
| 646 |
+
|
| 647 |
+
* The :setting:`STATIC_URL` and :setting:`MEDIA_URL` settings set to relative
|
| 648 |
+
paths are now prefixed by the server-provided value of ``SCRIPT_NAME`` (or
|
| 649 |
+
``/`` if not set). This change should not affect settings set to valid URLs
|
| 650 |
+
or absolute paths.
|
| 651 |
+
|
| 652 |
+
* :class:`~django.middleware.http.ConditionalGetMiddleware` no longer adds the
|
| 653 |
+
``ETag`` header to responses with an empty
|
| 654 |
+
:attr:`~django.http.HttpResponse.content`.
|
| 655 |
+
|
| 656 |
+
* ``django.utils.decorators.classproperty()`` decorator is made public and
|
| 657 |
+
moved to :class:`django.utils.functional.classproperty()`.
|
| 658 |
+
|
| 659 |
+
* :tfilter:`floatformat` template filter now outputs (positive) ``0`` for
|
| 660 |
+
negative numbers which round to zero.
|
| 661 |
+
|
| 662 |
+
* :attr:`Meta.ordering <django.db.models.Options.ordering>` and
|
| 663 |
+
:attr:`Meta.unique_together <django.db.models.Options.unique_together>`
|
| 664 |
+
options on models in ``django.contrib`` modules that were formerly tuples are
|
| 665 |
+
now lists.
|
| 666 |
+
|
| 667 |
+
* The admin calendar widget now handles two-digit years according to the Open
|
| 668 |
+
Group Specification, i.e. values between 69 and 99 are mapped to the previous
|
| 669 |
+
century, and values between 0 and 68 are mapped to the current century.
|
| 670 |
+
|
| 671 |
+
* Date-only formats are removed from the default list for
|
| 672 |
+
:setting:`DATETIME_INPUT_FORMATS`.
|
| 673 |
+
|
| 674 |
+
* The :class:`~django.forms.FileInput` widget no longer renders with the
|
| 675 |
+
``required`` HTML attribute when initial data exists.
|
| 676 |
+
|
| 677 |
+
* The undocumented ``django.views.debug.ExceptionReporterFilter`` class is
|
| 678 |
+
removed. As per the :ref:`custom-error-reports` documentation, classes to be
|
| 679 |
+
used with :setting:`DEFAULT_EXCEPTION_REPORTER_FILTER` need to inherit from
|
| 680 |
+
:class:`django.views.debug.SafeExceptionReporterFilter`.
|
| 681 |
+
|
| 682 |
+
* The cache timeout set by :func:`~django.views.decorators.cache.cache_page`
|
| 683 |
+
decorator now takes precedence over the ``max-age`` directive from the
|
| 684 |
+
``Cache-Control`` header.
|
| 685 |
+
|
| 686 |
+
* Providing a non-local remote field in the :attr:`.ForeignKey.to_field`
|
| 687 |
+
argument now raises :class:`~django.core.exceptions.FieldError`.
|
| 688 |
+
|
| 689 |
+
* :setting:`SECURE_REFERRER_POLICY` now defaults to ``'same-origin'``. See the
|
| 690 |
+
*What's New* :ref:`Security section <whats-new-security-3.1>` above for more
|
| 691 |
+
details.
|
| 692 |
+
|
| 693 |
+
* :djadmin:`check` management command now runs the ``database`` system checks
|
| 694 |
+
only for database aliases specified using :option:`check --database` option.
|
| 695 |
+
|
| 696 |
+
* :djadmin:`migrate` management command now runs the ``database`` system checks
|
| 697 |
+
only for a database to migrate.
|
| 698 |
+
|
| 699 |
+
* The admin CSS classes ``row1`` and ``row2`` are removed in favor of
|
| 700 |
+
``:nth-child(odd)`` and ``:nth-child(even)`` pseudo-classes.
|
| 701 |
+
|
| 702 |
+
* The :func:`~django.contrib.auth.hashers.make_password` function now requires
|
| 703 |
+
its argument to be a string or bytes. Other types should be explicitly cast
|
| 704 |
+
to one of these.
|
| 705 |
+
|
| 706 |
+
* The undocumented ``version`` parameter to the
|
| 707 |
+
:class:`~django.contrib.gis.db.models.functions.AsKML` function is removed.
|
| 708 |
+
|
| 709 |
+
* :ref:`JSON and YAML serializers <serialization-formats>`, used by
|
| 710 |
+
:djadmin:`dumpdata`, now dump all data with Unicode by default. If you need
|
| 711 |
+
the previous behavior, pass ``ensure_ascii=True`` to JSON serializer, or
|
| 712 |
+
``allow_unicode=False`` to YAML serializer.
|
| 713 |
+
|
| 714 |
+
* The auto-reloader no longer monitors changes in built-in Django translation
|
| 715 |
+
files.
|
| 716 |
+
|
| 717 |
+
* The minimum supported version of ``mysqlclient`` is increased from 1.3.13 to
|
| 718 |
+
1.4.0.
|
| 719 |
+
|
| 720 |
+
* The undocumented ``django.contrib.postgres.forms.InvalidJSONInput`` and
|
| 721 |
+
``django.contrib.postgres.forms.JSONString`` are moved to
|
| 722 |
+
``django.forms.fields``.
|
| 723 |
+
|
| 724 |
+
* The undocumented ``django.contrib.postgres.fields.jsonb.JsonAdapter`` class
|
| 725 |
+
is removed.
|
| 726 |
+
|
| 727 |
+
* The :ttag:`{% localize off %} <localize>` tag and :tfilter:`unlocalize`
|
| 728 |
+
filter no longer respect :setting:`DECIMAL_SEPARATOR` setting.
|
| 729 |
+
|
| 730 |
+
* The minimum supported version of ``asgiref`` is increased from 3.2 to
|
| 731 |
+
3.2.10.
|
| 732 |
+
|
| 733 |
+
* The :doc:`Media </topics/forms/media>` class now renders ``<script>`` tags
|
| 734 |
+
without the ``type`` attribute to follow `WHATWG recommendations
|
| 735 |
+
<https://html.spec.whatwg.org/multipage/scripting.html#the-script-element>`_.
|
| 736 |
+
|
| 737 |
+
* :class:`~django.forms.ModelChoiceIterator`, used by
|
| 738 |
+
:class:`~django.forms.ModelChoiceField` and
|
| 739 |
+
:class:`~django.forms.ModelMultipleChoiceField`, now yields 2-tuple choices
|
| 740 |
+
containing :class:`~django.forms.ModelChoiceIteratorValue` instances as the
|
| 741 |
+
first ``value`` element in each choice. In most cases this proxies
|
| 742 |
+
transparently, but if you need the ``field`` value itself, use the
|
| 743 |
+
:attr:`.ModelChoiceIteratorValue.value` attribute instead.
|
| 744 |
+
|
| 745 |
+
.. _deprecated-features-3.1:
|
| 746 |
+
|
| 747 |
+
Features deprecated in 3.1
|
| 748 |
+
==========================
|
| 749 |
+
|
| 750 |
+
.. _deprecated-jsonfield:
|
| 751 |
+
|
| 752 |
+
PostgreSQL ``JSONField``
|
| 753 |
+
------------------------
|
| 754 |
+
|
| 755 |
+
``django.contrib.postgres.fields.JSONField`` and
|
| 756 |
+
``django.contrib.postgres.forms.JSONField`` are deprecated in favor of
|
| 757 |
+
:class:`.models.JSONField` and
|
| 758 |
+
:class:`forms.JSONField <django.forms.JSONField>`.
|
| 759 |
+
|
| 760 |
+
The undocumented ``django.contrib.postgres.fields.jsonb.KeyTransform`` and
|
| 761 |
+
``django.contrib.postgres.fields.jsonb.KeyTextTransform`` are also deprecated
|
| 762 |
+
in favor of the transforms in ``django.db.models.fields.json``.
|
| 763 |
+
|
| 764 |
+
The new ``JSONField``\s, ``KeyTransform``, and ``KeyTextTransform`` can be used
|
| 765 |
+
on all supported database backends.
|
| 766 |
+
|
| 767 |
+
Miscellaneous
|
| 768 |
+
-------------
|
| 769 |
+
|
| 770 |
+
* ``PASSWORD_RESET_TIMEOUT_DAYS`` setting is deprecated in favor of
|
| 771 |
+
:setting:`PASSWORD_RESET_TIMEOUT`.
|
| 772 |
+
|
| 773 |
+
* The undocumented usage of the :lookup:`isnull` lookup with non-boolean values
|
| 774 |
+
as the right-hand side is deprecated, use ``True`` or ``False`` instead.
|
| 775 |
+
|
| 776 |
+
* The barely documented ``django.db.models.query_utils.InvalidQuery`` exception
|
| 777 |
+
class is deprecated in favor of
|
| 778 |
+
:class:`~django.core.exceptions.FieldDoesNotExist` and
|
| 779 |
+
:class:`~django.core.exceptions.FieldError`.
|
| 780 |
+
|
| 781 |
+
* The ``django-admin.py`` entry point is deprecated in favor of
|
| 782 |
+
``django-admin``.
|
| 783 |
+
|
| 784 |
+
* The ``HttpRequest.is_ajax()`` method is deprecated as it relied on a
|
| 785 |
+
jQuery-specific way of signifying AJAX calls, while current usage tends to
|
| 786 |
+
use the JavaScript `Fetch API
|
| 787 |
+
<https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>`_. Depending on
|
| 788 |
+
your use case, you can either write your own AJAX detection method, or use
|
| 789 |
+
the new :meth:`.HttpRequest.accepts` method if your code depends on the
|
| 790 |
+
client ``Accept`` HTTP header.
|
| 791 |
+
|
| 792 |
+
If you are writing your own AJAX detection method, ``request.is_ajax()`` can
|
| 793 |
+
be reproduced exactly as
|
| 794 |
+
``request.headers.get('x-requested-with') == 'XMLHttpRequest'``.
|
| 795 |
+
|
| 796 |
+
* Passing ``None`` as the first argument to
|
| 797 |
+
``django.utils.deprecation.MiddlewareMixin.__init__()`` is deprecated.
|
| 798 |
+
|
| 799 |
+
* The encoding format of cookies values used by
|
| 800 |
+
:class:`~django.contrib.messages.storage.cookie.CookieStorage` is different
|
| 801 |
+
from the format generated by older versions of Django. Support for the old
|
| 802 |
+
format remains until Django 4.0.
|
| 803 |
+
|
| 804 |
+
* The encoding format of sessions is different from the format generated by
|
| 805 |
+
older versions of Django. Support for the old format remains until Django
|
| 806 |
+
4.0.
|
| 807 |
+
|
| 808 |
+
* The purely documentational ``providing_args`` argument for
|
| 809 |
+
:class:`~django.dispatch.Signal` is deprecated. If you rely on this
|
| 810 |
+
argument as documentation, you can move the text to a code comment or
|
| 811 |
+
docstring.
|
| 812 |
+
|
| 813 |
+
* Calling ``django.utils.crypto.get_random_string()`` without a ``length``
|
| 814 |
+
argument is deprecated.
|
| 815 |
+
|
| 816 |
+
* The ``list`` message for :class:`~django.forms.ModelMultipleChoiceField` is
|
| 817 |
+
deprecated in favor of ``invalid_list``.
|
| 818 |
+
|
| 819 |
+
* Passing raw column aliases to :meth:`.QuerySet.order_by` is deprecated. The
|
| 820 |
+
same result can be achieved by passing aliases in a
|
| 821 |
+
:class:`~django.db.models.expressions.RawSQL` instead beforehand.
|
| 822 |
+
|
| 823 |
+
* The ``NullBooleanField`` model field is deprecated in favor of
|
| 824 |
+
``BooleanField(null=True, blank=True)``.
|
| 825 |
+
|
| 826 |
+
* ``django.conf.urls.url()`` alias of :func:`django.urls.re_path` is
|
| 827 |
+
deprecated.
|
| 828 |
+
|
| 829 |
+
* The ``{% ifequal %}`` and ``{% ifnotequal %}`` template tags are deprecated
|
| 830 |
+
in favor of :ttag:`{% if %}<if>`. ``{% if %}`` covers all use cases, but if
|
| 831 |
+
you need to continue using these tags, they can be extracted from Django to a
|
| 832 |
+
module and included as a built-in tag in the :class:`'builtins'
|
| 833 |
+
<django.template.backends.django.DjangoTemplates>` option in
|
| 834 |
+
:setting:`OPTIONS <TEMPLATES-OPTIONS>`.
|
| 835 |
+
|
| 836 |
+
* ``DEFAULT_HASHING_ALGORITHM`` transitional setting is deprecated.
|
| 837 |
+
|
| 838 |
+
.. _removed-features-3.1:
|
| 839 |
+
|
| 840 |
+
Features removed in 3.1
|
| 841 |
+
=======================
|
| 842 |
+
|
| 843 |
+
These features have reached the end of their deprecation cycle and are removed
|
| 844 |
+
in Django 3.1.
|
| 845 |
+
|
| 846 |
+
See :ref:`deprecated-features-2.2` for details on these changes, including how
|
| 847 |
+
to remove usage of these features.
|
| 848 |
+
|
| 849 |
+
* ``django.utils.timezone.FixedOffset`` is removed.
|
| 850 |
+
|
| 851 |
+
* ``django.core.paginator.QuerySetPaginator`` is removed.
|
| 852 |
+
|
| 853 |
+
* A model's ``Meta.ordering`` doesn't affect ``GROUP BY`` queries.
|
| 854 |
+
|
| 855 |
+
* ``django.contrib.postgres.fields.FloatRangeField`` and
|
| 856 |
+
``django.contrib.postgres.forms.FloatRangeField`` are removed.
|
| 857 |
+
|
| 858 |
+
* The ``FILE_CHARSET`` setting is removed.
|
| 859 |
+
|
| 860 |
+
* ``django.contrib.staticfiles.storage.CachedStaticFilesStorage`` is removed.
|
| 861 |
+
|
| 862 |
+
* The ``RemoteUserBackend.configure_user()`` method requires ``request`` as the
|
| 863 |
+
first positional argument.
|
| 864 |
+
|
| 865 |
+
* Support for ``SimpleTestCase.allow_database_queries`` and
|
| 866 |
+
``TransactionTestCase.multi_db`` is removed.
|
testbed/django__django/docs/releases/3.2.txt
ADDED
|
@@ -0,0 +1,791 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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welcome to Django 3.2!
|
| 8 |
+
|
| 9 |
+
These release notes cover the :ref:`new features <whats-new-3.2>`, as well as
|
| 10 |
+
some :ref:`backwards incompatible changes <backwards-incompatible-3.2>` you'll
|
| 11 |
+
want to be aware of when upgrading from Django 3.1 or earlier. We've
|
| 12 |
+
:ref:`begun the deprecation process for some features
|
| 13 |
+
<deprecated-features-3.2>`.
|
| 14 |
+
|
| 15 |
+
See the :doc:`/howto/upgrade-version` guide if you're updating an existing
|
| 16 |
+
project.
|
| 17 |
+
|
| 18 |
+
Django 3.2 is designated as a :term:`long-term support release
|
| 19 |
+
<Long-term support release>`. It will receive security updates for at least
|
| 20 |
+
three years after its release. Support for the previous LTS, Django 2.2, will
|
| 21 |
+
end in April 2022.
|
| 22 |
+
|
| 23 |
+
Python compatibility
|
| 24 |
+
====================
|
| 25 |
+
|
| 26 |
+
Django 3.2 supports Python 3.6, 3.7, 3.8, 3.9, and 3.10 (as of 3.2.9). We
|
| 27 |
+
**highly recommend** and only officially support the latest release of each
|
| 28 |
+
series.
|
| 29 |
+
|
| 30 |
+
.. _whats-new-3.2:
|
| 31 |
+
|
| 32 |
+
What's new in Django 3.2
|
| 33 |
+
========================
|
| 34 |
+
|
| 35 |
+
Automatic :class:`~django.apps.AppConfig` discovery
|
| 36 |
+
---------------------------------------------------
|
| 37 |
+
|
| 38 |
+
Most pluggable applications define an :class:`~django.apps.AppConfig` subclass
|
| 39 |
+
in an ``apps.py`` submodule. Many define a ``default_app_config`` variable
|
| 40 |
+
pointing to this class in their ``__init__.py``.
|
| 41 |
+
|
| 42 |
+
When the ``apps.py`` submodule exists and defines a single
|
| 43 |
+
:class:`~django.apps.AppConfig` subclass, Django now uses that configuration
|
| 44 |
+
automatically, so you can remove ``default_app_config``.
|
| 45 |
+
|
| 46 |
+
``default_app_config`` made it possible to declare only the application's path
|
| 47 |
+
in :setting:`INSTALLED_APPS` (e.g. ``'django.contrib.admin'``) rather than the
|
| 48 |
+
app config's path (e.g. ``'django.contrib.admin.apps.AdminConfig'``). It was
|
| 49 |
+
introduced for backwards-compatibility with the former style, with the intent
|
| 50 |
+
to switch the ecosystem to the latter, but the switch didn't happen.
|
| 51 |
+
|
| 52 |
+
With automatic ``AppConfig`` discovery, ``default_app_config`` is no longer
|
| 53 |
+
needed. As a consequence, it's deprecated.
|
| 54 |
+
|
| 55 |
+
See :ref:`configuring-applications-ref` for full details.
|
| 56 |
+
|
| 57 |
+
Customizing type of auto-created primary keys
|
| 58 |
+
---------------------------------------------
|
| 59 |
+
|
| 60 |
+
When defining a model, if no field in a model is defined with
|
| 61 |
+
:attr:`primary_key=True <django.db.models.Field.primary_key>` an implicit
|
| 62 |
+
primary key is added. The type of this implicit primary key can now be
|
| 63 |
+
controlled via the :setting:`DEFAULT_AUTO_FIELD` setting and
|
| 64 |
+
:attr:`AppConfig.default_auto_field <django.apps.AppConfig.default_auto_field>`
|
| 65 |
+
attribute. No more needing to override primary keys in all models.
|
| 66 |
+
|
| 67 |
+
Maintaining the historical behavior, the default value for
|
| 68 |
+
:setting:`DEFAULT_AUTO_FIELD` is :class:`~django.db.models.AutoField`. Starting
|
| 69 |
+
with 3.2 new projects are generated with :setting:`DEFAULT_AUTO_FIELD` set to
|
| 70 |
+
:class:`~django.db.models.BigAutoField`. Also, new apps are generated with
|
| 71 |
+
:attr:`AppConfig.default_auto_field <django.apps.AppConfig.default_auto_field>`
|
| 72 |
+
set to :class:`~django.db.models.BigAutoField`. In a future Django release the
|
| 73 |
+
default value of :setting:`DEFAULT_AUTO_FIELD` will be changed to
|
| 74 |
+
:class:`~django.db.models.BigAutoField`.
|
| 75 |
+
|
| 76 |
+
To avoid unwanted migrations in the future, either explicitly set
|
| 77 |
+
:setting:`DEFAULT_AUTO_FIELD` to :class:`~django.db.models.AutoField`::
|
| 78 |
+
|
| 79 |
+
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
| 80 |
+
|
| 81 |
+
or configure it on a per-app basis::
|
| 82 |
+
|
| 83 |
+
from django.apps import AppConfig
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
class MyAppConfig(AppConfig):
|
| 87 |
+
default_auto_field = "django.db.models.AutoField"
|
| 88 |
+
name = "my_app"
|
| 89 |
+
|
| 90 |
+
or on a per-model basis::
|
| 91 |
+
|
| 92 |
+
from django.db import models
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class MyModel(models.Model):
|
| 96 |
+
id = models.AutoField(primary_key=True)
|
| 97 |
+
|
| 98 |
+
In anticipation of the changing default, a system check will provide a warning
|
| 99 |
+
if you do not have an explicit setting for :setting:`DEFAULT_AUTO_FIELD`.
|
| 100 |
+
|
| 101 |
+
When changing the value of :setting:`DEFAULT_AUTO_FIELD`, migrations for the
|
| 102 |
+
primary key of existing auto-created through tables cannot be generated
|
| 103 |
+
currently. See the :setting:`DEFAULT_AUTO_FIELD` docs for details on migrating
|
| 104 |
+
such tables.
|
| 105 |
+
|
| 106 |
+
.. _new_functional_indexes:
|
| 107 |
+
|
| 108 |
+
Functional indexes
|
| 109 |
+
------------------
|
| 110 |
+
|
| 111 |
+
The new :attr:`*expressions <django.db.models.Index.expressions>` positional
|
| 112 |
+
argument of :class:`Index() <django.db.models.Index>` enables creating
|
| 113 |
+
functional indexes on expressions and database functions. For example::
|
| 114 |
+
|
| 115 |
+
from django.db import models
|
| 116 |
+
from django.db.models import F, Index, Value
|
| 117 |
+
from django.db.models.functions import Lower, Upper
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
class MyModel(models.Model):
|
| 121 |
+
first_name = models.CharField(max_length=255)
|
| 122 |
+
last_name = models.CharField(max_length=255)
|
| 123 |
+
height = models.IntegerField()
|
| 124 |
+
weight = models.IntegerField()
|
| 125 |
+
|
| 126 |
+
class Meta:
|
| 127 |
+
indexes = [
|
| 128 |
+
Index(
|
| 129 |
+
Lower("first_name"),
|
| 130 |
+
Upper("last_name").desc(),
|
| 131 |
+
name="first_last_name_idx",
|
| 132 |
+
),
|
| 133 |
+
Index(
|
| 134 |
+
F("height") / (F("weight") + Value(5)),
|
| 135 |
+
name="calc_idx",
|
| 136 |
+
),
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
Functional indexes are added to models using the
|
| 140 |
+
:attr:`Meta.indexes <django.db.models.Options.indexes>` option.
|
| 141 |
+
|
| 142 |
+
``pymemcache`` support
|
| 143 |
+
----------------------
|
| 144 |
+
|
| 145 |
+
The new ``django.core.cache.backends.memcached.PyMemcacheCache`` cache backend
|
| 146 |
+
allows using the :pypi:`pymemcache` library for memcached. ``pymemcache`` 3.4.0
|
| 147 |
+
or higher is required. For more details, see the :doc:`documentation on caching
|
| 148 |
+
in Django </topics/cache>`.
|
| 149 |
+
|
| 150 |
+
New decorators for the admin site
|
| 151 |
+
---------------------------------
|
| 152 |
+
|
| 153 |
+
The new :func:`~django.contrib.admin.display` decorator allows for easily
|
| 154 |
+
adding options to custom display functions that can be used with
|
| 155 |
+
:attr:`~django.contrib.admin.ModelAdmin.list_display` or
|
| 156 |
+
:attr:`~django.contrib.admin.ModelAdmin.readonly_fields`.
|
| 157 |
+
|
| 158 |
+
Likewise, the new :func:`~django.contrib.admin.action` decorator allows for
|
| 159 |
+
easily adding options to action functions that can be used with
|
| 160 |
+
:attr:`~django.contrib.admin.ModelAdmin.actions`.
|
| 161 |
+
|
| 162 |
+
Using the ``@display`` decorator has the advantage that it is now
|
| 163 |
+
possible to use the ``@property`` decorator when needing to specify attributes
|
| 164 |
+
on the custom method. Prior to this it was necessary to use the ``property()``
|
| 165 |
+
function instead after assigning the required attributes to the method.
|
| 166 |
+
|
| 167 |
+
Using decorators has the advantage that these options are more discoverable as
|
| 168 |
+
they can be suggested by completion utilities in code editors. They are merely
|
| 169 |
+
a convenience and still set the same attributes on the functions under the
|
| 170 |
+
hood.
|
| 171 |
+
|
| 172 |
+
Minor features
|
| 173 |
+
--------------
|
| 174 |
+
|
| 175 |
+
:mod:`django.contrib.admin`
|
| 176 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 177 |
+
|
| 178 |
+
* :attr:`.ModelAdmin.search_fields` now allows searching against quoted phrases
|
| 179 |
+
with spaces.
|
| 180 |
+
|
| 181 |
+
* Read-only related fields are now rendered as navigable links if target models
|
| 182 |
+
are registered in the admin.
|
| 183 |
+
|
| 184 |
+
* The admin now supports theming, and includes a dark theme that is enabled
|
| 185 |
+
according to browser settings. See :ref:`admin-theming` for more details.
|
| 186 |
+
|
| 187 |
+
* :attr:`.ModelAdmin.autocomplete_fields` now respects
|
| 188 |
+
:attr:`ForeignKey.to_field <django.db.models.ForeignKey.to_field>` and
|
| 189 |
+
:attr:`ForeignKey.limit_choices_to
|
| 190 |
+
<django.db.models.ForeignKey.limit_choices_to>` when searching a related
|
| 191 |
+
model.
|
| 192 |
+
|
| 193 |
+
* The admin now installs a final catch-all view that redirects unauthenticated
|
| 194 |
+
users to the login page, regardless of whether the URL is otherwise valid.
|
| 195 |
+
This protects against a potential model enumeration privacy issue.
|
| 196 |
+
|
| 197 |
+
Although not recommended, you may set the new
|
| 198 |
+
:attr:`.AdminSite.final_catch_all_view` to ``False`` to disable the
|
| 199 |
+
catch-all view.
|
| 200 |
+
|
| 201 |
+
:mod:`django.contrib.auth`
|
| 202 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 203 |
+
|
| 204 |
+
* The default iteration count for the PBKDF2 password hasher is increased from
|
| 205 |
+
216,000 to 260,000.
|
| 206 |
+
|
| 207 |
+
* The default variant for the Argon2 password hasher is changed to Argon2id.
|
| 208 |
+
``memory_cost`` and ``parallelism`` are increased to 102,400 and 8
|
| 209 |
+
respectively to match the ``argon2-cffi`` defaults.
|
| 210 |
+
|
| 211 |
+
Increasing the ``memory_cost`` pushes the required memory from 512 KB to 100
|
| 212 |
+
MB. This is still rather conservative but can lead to problems in memory
|
| 213 |
+
constrained environments. If this is the case, the existing hasher can be
|
| 214 |
+
subclassed to override the defaults.
|
| 215 |
+
|
| 216 |
+
* The default salt entropy for the Argon2, MD5, PBKDF2, SHA-1 password hashers
|
| 217 |
+
is increased from 71 to 128 bits.
|
| 218 |
+
|
| 219 |
+
:mod:`django.contrib.contenttypes`
|
| 220 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 221 |
+
|
| 222 |
+
* The new ``absolute_max`` argument for
|
| 223 |
+
:func:`~django.contrib.contenttypes.forms.generic_inlineformset_factory`
|
| 224 |
+
allows customizing the maximum number of forms that can be instantiated when
|
| 225 |
+
supplying ``POST`` data. See :ref:`formsets-absolute-max` for more details.
|
| 226 |
+
|
| 227 |
+
* The new ``can_delete_extra`` argument for
|
| 228 |
+
:func:`~django.contrib.contenttypes.forms.generic_inlineformset_factory`
|
| 229 |
+
allows removal of the option to delete extra forms. See
|
| 230 |
+
:attr:`~.BaseFormSet.can_delete_extra` for more information.
|
| 231 |
+
|
| 232 |
+
:mod:`django.contrib.gis`
|
| 233 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 234 |
+
|
| 235 |
+
* The :meth:`.GDALRaster.transform` method now supports
|
| 236 |
+
:class:`~django.contrib.gis.gdal.SpatialReference`.
|
| 237 |
+
|
| 238 |
+
* The :class:`~django.contrib.gis.gdal.DataSource` class now supports
|
| 239 |
+
:class:`pathlib.Path`.
|
| 240 |
+
|
| 241 |
+
* The :class:`~django.contrib.gis.utils.LayerMapping` class now supports
|
| 242 |
+
:class:`pathlib.Path`.
|
| 243 |
+
|
| 244 |
+
:mod:`django.contrib.postgres`
|
| 245 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 246 |
+
|
| 247 |
+
* The new :attr:`.ExclusionConstraint.include` attribute allows creating
|
| 248 |
+
covering exclusion constraints on PostgreSQL 12+.
|
| 249 |
+
|
| 250 |
+
* The new ``ExclusionConstraint.opclasses`` attribute allows setting PostgreSQL
|
| 251 |
+
operator classes.
|
| 252 |
+
|
| 253 |
+
* The new :attr:`.JSONBAgg.ordering` attribute determines the ordering of the
|
| 254 |
+
aggregated elements.
|
| 255 |
+
|
| 256 |
+
* The new :attr:`.JSONBAgg.distinct` attribute determines if aggregated values
|
| 257 |
+
will be distinct.
|
| 258 |
+
|
| 259 |
+
* The :class:`~django.contrib.postgres.operations.CreateExtension` operation
|
| 260 |
+
now checks that the extension already exists in the database and skips the
|
| 261 |
+
migration if so.
|
| 262 |
+
|
| 263 |
+
* The new :class:`~django.contrib.postgres.operations.CreateCollation` and
|
| 264 |
+
:class:`~django.contrib.postgres.operations.RemoveCollation` operations
|
| 265 |
+
allow creating and dropping collations on PostgreSQL. See
|
| 266 |
+
:ref:`manage-postgresql-collations` for more details.
|
| 267 |
+
|
| 268 |
+
* Lookups for :class:`~django.contrib.postgres.fields.ArrayField` now allow
|
| 269 |
+
(non-nested) arrays containing expressions as right-hand sides.
|
| 270 |
+
|
| 271 |
+
* The new :class:`OpClass() <django.contrib.postgres.indexes.OpClass>`
|
| 272 |
+
expression allows creating functional indexes on expressions with a custom
|
| 273 |
+
operator class. See :ref:`new_functional_indexes` for more details.
|
| 274 |
+
|
| 275 |
+
:mod:`django.contrib.sitemaps`
|
| 276 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 277 |
+
|
| 278 |
+
* The new :class:`~django.contrib.sitemaps.Sitemap` attributes
|
| 279 |
+
:attr:`~django.contrib.sitemaps.Sitemap.alternates`,
|
| 280 |
+
:attr:`~django.contrib.sitemaps.Sitemap.languages` and
|
| 281 |
+
:attr:`~django.contrib.sitemaps.Sitemap.x_default` allow
|
| 282 |
+
generating sitemap *alternates* to localized versions of your pages.
|
| 283 |
+
|
| 284 |
+
:mod:`django.contrib.syndication`
|
| 285 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 286 |
+
|
| 287 |
+
* The new ``item_comments`` hook allows specifying a comments URL per feed
|
| 288 |
+
item.
|
| 289 |
+
|
| 290 |
+
Database backends
|
| 291 |
+
~~~~~~~~~~~~~~~~~
|
| 292 |
+
|
| 293 |
+
* Third-party database backends can now skip or mark as expected failures
|
| 294 |
+
tests in Django's test suite using the new
|
| 295 |
+
``DatabaseFeatures.django_test_skips`` and
|
| 296 |
+
``django_test_expected_failures`` attributes.
|
| 297 |
+
|
| 298 |
+
Decorators
|
| 299 |
+
~~~~~~~~~~
|
| 300 |
+
|
| 301 |
+
* The new :func:`~django.views.decorators.common.no_append_slash` decorator
|
| 302 |
+
allows individual views to be excluded from :setting:`APPEND_SLASH` URL
|
| 303 |
+
normalization.
|
| 304 |
+
|
| 305 |
+
Error Reporting
|
| 306 |
+
~~~~~~~~~~~~~~~
|
| 307 |
+
|
| 308 |
+
* Custom :class:`~django.views.debug.ExceptionReporter` subclasses can now
|
| 309 |
+
define the :attr:`~django.views.debug.ExceptionReporter.html_template_path`
|
| 310 |
+
and :attr:`~django.views.debug.ExceptionReporter.text_template_path`
|
| 311 |
+
properties to override the templates used to render exception reports.
|
| 312 |
+
|
| 313 |
+
File Uploads
|
| 314 |
+
~~~~~~~~~~~~
|
| 315 |
+
|
| 316 |
+
* The new :meth:`FileUploadHandler.upload_interrupted()
|
| 317 |
+
<django.core.files.uploadhandler.FileUploadHandler.upload_interrupted>`
|
| 318 |
+
callback allows handling interrupted uploads.
|
| 319 |
+
|
| 320 |
+
Forms
|
| 321 |
+
~~~~~
|
| 322 |
+
|
| 323 |
+
* The new ``absolute_max`` argument for :func:`.formset_factory`,
|
| 324 |
+
:func:`.inlineformset_factory`, and :func:`.modelformset_factory` allows
|
| 325 |
+
customizing the maximum number of forms that can be instantiated when
|
| 326 |
+
supplying ``POST`` data. See :ref:`formsets-absolute-max` for more details.
|
| 327 |
+
|
| 328 |
+
* The new ``can_delete_extra`` argument for :func:`.formset_factory`,
|
| 329 |
+
:func:`.inlineformset_factory`, and :func:`.modelformset_factory` allows
|
| 330 |
+
removal of the option to delete extra forms. See
|
| 331 |
+
:attr:`~.BaseFormSet.can_delete_extra` for more information.
|
| 332 |
+
|
| 333 |
+
* :class:`~django.forms.formsets.BaseFormSet` now reports a user facing error,
|
| 334 |
+
rather than raising an exception, when the management form is missing or has
|
| 335 |
+
been tampered with. To customize this error message, pass the
|
| 336 |
+
``error_messages`` argument with the key ``'missing_management_form'`` when
|
| 337 |
+
instantiating the formset.
|
| 338 |
+
|
| 339 |
+
Generic Views
|
| 340 |
+
~~~~~~~~~~~~~
|
| 341 |
+
|
| 342 |
+
* The ``week_format`` attributes of
|
| 343 |
+
:class:`~django.views.generic.dates.WeekMixin` and
|
| 344 |
+
:class:`~django.views.generic.dates.WeekArchiveView` now support the
|
| 345 |
+
``'%V'`` ISO 8601 week format.
|
| 346 |
+
|
| 347 |
+
Management Commands
|
| 348 |
+
~~~~~~~~~~~~~~~~~~~
|
| 349 |
+
|
| 350 |
+
* :djadmin:`loaddata` now supports fixtures stored in XZ archives (``.xz``) and
|
| 351 |
+
LZMA archives (``.lzma``).
|
| 352 |
+
|
| 353 |
+
* :djadmin:`dumpdata` now can compress data in the ``bz2``, ``gz``, ``lzma``,
|
| 354 |
+
or ``xz`` formats.
|
| 355 |
+
|
| 356 |
+
* :djadmin:`makemigrations` can now be called without an active database
|
| 357 |
+
connection. In that case, check for a consistent migration history is
|
| 358 |
+
skipped.
|
| 359 |
+
|
| 360 |
+
* :attr:`.BaseCommand.requires_system_checks` now supports specifying a list of
|
| 361 |
+
tags. System checks registered in the chosen tags will be checked for errors
|
| 362 |
+
prior to executing the command. In previous versions, either all or none
|
| 363 |
+
of the system checks were performed.
|
| 364 |
+
|
| 365 |
+
* Support for colored terminal output on Windows is updated. Various modern
|
| 366 |
+
terminal environments are automatically detected, and the options for
|
| 367 |
+
enabling support in other cases are improved. See :ref:`syntax-coloring` for
|
| 368 |
+
more details.
|
| 369 |
+
|
| 370 |
+
Migrations
|
| 371 |
+
~~~~~~~~~~
|
| 372 |
+
|
| 373 |
+
* The new ``Operation.migration_name_fragment`` property allows providing a
|
| 374 |
+
filename fragment that will be used to name a migration containing only that
|
| 375 |
+
operation.
|
| 376 |
+
|
| 377 |
+
* Migrations now support serialization of pure and concrete path objects from
|
| 378 |
+
:mod:`pathlib`, and :class:`os.PathLike` instances.
|
| 379 |
+
|
| 380 |
+
Models
|
| 381 |
+
~~~~~~
|
| 382 |
+
|
| 383 |
+
* The new ``no_key`` parameter for :meth:`.QuerySet.select_for_update()`,
|
| 384 |
+
supported on PostgreSQL, allows acquiring weaker locks that don't block the
|
| 385 |
+
creation of rows that reference locked rows through a foreign key.
|
| 386 |
+
|
| 387 |
+
* :class:`When() <django.db.models.expressions.When>` expression now allows
|
| 388 |
+
using the ``condition`` argument with ``lookups``.
|
| 389 |
+
|
| 390 |
+
* The new :attr:`.Index.include` and :attr:`.UniqueConstraint.include`
|
| 391 |
+
attributes allow creating covering indexes and covering unique constraints on
|
| 392 |
+
PostgreSQL 11+.
|
| 393 |
+
|
| 394 |
+
* The new :attr:`.UniqueConstraint.opclasses` attribute allows setting
|
| 395 |
+
PostgreSQL operator classes.
|
| 396 |
+
|
| 397 |
+
* The :meth:`.QuerySet.update` method now respects the ``order_by()`` clause on
|
| 398 |
+
MySQL and MariaDB.
|
| 399 |
+
|
| 400 |
+
* :class:`FilteredRelation() <django.db.models.FilteredRelation>` now supports
|
| 401 |
+
nested relations.
|
| 402 |
+
|
| 403 |
+
* The ``of`` argument of :meth:`.QuerySet.select_for_update()` is now allowed
|
| 404 |
+
on MySQL 8.0.1+.
|
| 405 |
+
|
| 406 |
+
* :class:`Value() <django.db.models.Value>` expression now
|
| 407 |
+
automatically resolves its ``output_field`` to the appropriate
|
| 408 |
+
:class:`Field <django.db.models.Field>` subclass based on the type of
|
| 409 |
+
its provided ``value`` for :py:class:`bool`, :py:class:`bytes`,
|
| 410 |
+
:py:class:`float`, :py:class:`int`, :py:class:`str`,
|
| 411 |
+
:py:class:`datetime.date`, :py:class:`datetime.datetime`,
|
| 412 |
+
:py:class:`datetime.time`, :py:class:`datetime.timedelta`,
|
| 413 |
+
:py:class:`decimal.Decimal`, and :py:class:`uuid.UUID` instances. As a
|
| 414 |
+
consequence, resolving an ``output_field`` for database functions and
|
| 415 |
+
combined expressions may now crash with mixed types when using ``Value()``.
|
| 416 |
+
You will need to explicitly set the ``output_field`` in such cases.
|
| 417 |
+
|
| 418 |
+
* The new :meth:`.QuerySet.alias` method allows creating reusable aliases for
|
| 419 |
+
expressions that don't need to be selected but are used for filtering,
|
| 420 |
+
ordering, or as a part of complex expressions.
|
| 421 |
+
|
| 422 |
+
* The new :class:`~django.db.models.functions.Collate` function allows
|
| 423 |
+
filtering and ordering by specified database collations.
|
| 424 |
+
|
| 425 |
+
* The ``field_name`` argument of :meth:`.QuerySet.in_bulk()` now accepts
|
| 426 |
+
distinct fields if there's only one field specified in
|
| 427 |
+
:meth:`.QuerySet.distinct`.
|
| 428 |
+
|
| 429 |
+
* The new ``tzinfo`` parameter of the
|
| 430 |
+
:class:`~django.db.models.functions.TruncDate` and
|
| 431 |
+
:class:`~django.db.models.functions.TruncTime` database functions allows
|
| 432 |
+
truncating datetimes in a specific timezone.
|
| 433 |
+
|
| 434 |
+
* The new ``db_collation`` argument for
|
| 435 |
+
:attr:`CharField <django.db.models.CharField.db_collation>` and
|
| 436 |
+
:attr:`TextField <django.db.models.TextField.db_collation>` allows setting a
|
| 437 |
+
database collation for the field.
|
| 438 |
+
|
| 439 |
+
* Added the :class:`~django.db.models.functions.Random` database function.
|
| 440 |
+
|
| 441 |
+
* :ref:`aggregation-functions`, :class:`F() <django.db.models.F>`,
|
| 442 |
+
:class:`OuterRef() <django.db.models.OuterRef>`, and other expressions now
|
| 443 |
+
allow using transforms. See :ref:`using-transforms-in-expressions` for
|
| 444 |
+
details.
|
| 445 |
+
|
| 446 |
+
* The new ``durable`` argument for :func:`~django.db.transaction.atomic`
|
| 447 |
+
guarantees that changes made in the atomic block will be committed if the
|
| 448 |
+
block exits without errors. A nested atomic block marked as durable will
|
| 449 |
+
raise a ``RuntimeError``.
|
| 450 |
+
|
| 451 |
+
* Added the :class:`~django.db.models.functions.JSONObject` database function.
|
| 452 |
+
|
| 453 |
+
Pagination
|
| 454 |
+
~~~~~~~~~~
|
| 455 |
+
|
| 456 |
+
* The new :meth:`django.core.paginator.Paginator.get_elided_page_range` method
|
| 457 |
+
allows generating a page range with some of the values elided. If there are a
|
| 458 |
+
large number of pages, this can be helpful for generating a reasonable number
|
| 459 |
+
of page links in a template.
|
| 460 |
+
|
| 461 |
+
Requests and Responses
|
| 462 |
+
~~~~~~~~~~~~~~~~~~~~~~
|
| 463 |
+
|
| 464 |
+
* Response headers are now stored in :attr:`.HttpResponse.headers`. This can be
|
| 465 |
+
used instead of the original dict-like interface of ``HttpResponse`` objects.
|
| 466 |
+
Both interfaces will continue to be supported. See
|
| 467 |
+
:ref:`setting-header-fields` for details.
|
| 468 |
+
|
| 469 |
+
* The new ``headers`` parameter of :class:`~django.http.HttpResponse`,
|
| 470 |
+
:class:`~django.template.response.SimpleTemplateResponse`, and
|
| 471 |
+
:class:`~django.template.response.TemplateResponse` allows setting response
|
| 472 |
+
:attr:`~django.http.HttpResponse.headers` on instantiation.
|
| 473 |
+
|
| 474 |
+
Security
|
| 475 |
+
~~~~~~~~
|
| 476 |
+
|
| 477 |
+
* The :setting:`SECRET_KEY` setting is now checked for a valid value upon first
|
| 478 |
+
access, rather than when settings are first loaded. This enables running
|
| 479 |
+
management commands that do not rely on the ``SECRET_KEY`` without needing to
|
| 480 |
+
provide a value. As a consequence of this, calling
|
| 481 |
+
:func:`~django.conf.settings.configure` without providing a valid
|
| 482 |
+
``SECRET_KEY``, and then going on to access ``settings.SECRET_KEY`` will now
|
| 483 |
+
raise an :exc:`~django.core.exceptions.ImproperlyConfigured` exception.
|
| 484 |
+
|
| 485 |
+
* The new ``Signer.sign_object()`` and ``Signer.unsign_object()`` methods allow
|
| 486 |
+
signing complex data structures. See :ref:`signing-complex-data` for more
|
| 487 |
+
details.
|
| 488 |
+
|
| 489 |
+
Also, :func:`signing.dumps() <django.core.signing.dumps>` and
|
| 490 |
+
:func:`~django.core.signing.loads` become shortcuts for
|
| 491 |
+
:meth:`.TimestampSigner.sign_object` and
|
| 492 |
+
:meth:`~.TimestampSigner.unsign_object`.
|
| 493 |
+
|
| 494 |
+
Serialization
|
| 495 |
+
~~~~~~~~~~~~~
|
| 496 |
+
|
| 497 |
+
* The new :ref:`JSONL <serialization-formats-jsonl>` serializer allows using
|
| 498 |
+
the JSON Lines format with :djadmin:`dumpdata` and :djadmin:`loaddata`. This
|
| 499 |
+
can be useful for populating large databases because data is loaded line by
|
| 500 |
+
line into memory, rather than being loaded all at once.
|
| 501 |
+
|
| 502 |
+
Signals
|
| 503 |
+
~~~~~~~
|
| 504 |
+
|
| 505 |
+
* :meth:`Signal.send_robust() <django.dispatch.Signal.send_robust>` now logs
|
| 506 |
+
exceptions.
|
| 507 |
+
|
| 508 |
+
Templates
|
| 509 |
+
~~~~~~~~~
|
| 510 |
+
|
| 511 |
+
* :tfilter:`floatformat` template filter now allows using the ``g`` suffix to
|
| 512 |
+
force grouping by the :setting:`THOUSAND_SEPARATOR` for the active locale.
|
| 513 |
+
|
| 514 |
+
* Templates cached with :ref:`Cached template loaders<template-loaders>` are
|
| 515 |
+
now correctly reloaded in development.
|
| 516 |
+
|
| 517 |
+
Tests
|
| 518 |
+
~~~~~
|
| 519 |
+
|
| 520 |
+
* Objects assigned to class attributes in :meth:`.TestCase.setUpTestData` are
|
| 521 |
+
now isolated for each test method. Such objects are now required to support
|
| 522 |
+
creating deep copies with :py:func:`copy.deepcopy`. Assigning objects which
|
| 523 |
+
don't support ``deepcopy()`` is deprecated and will be removed in Django 4.1.
|
| 524 |
+
|
| 525 |
+
* :class:`~django.test.runner.DiscoverRunner` now enables
|
| 526 |
+
:py:mod:`faulthandler` by default. This can be disabled by using the
|
| 527 |
+
:option:`test --no-faulthandler` option.
|
| 528 |
+
|
| 529 |
+
* :class:`~django.test.runner.DiscoverRunner` and the
|
| 530 |
+
:djadmin:`test` management command can now track timings, including database
|
| 531 |
+
setup and total run time. This can be enabled by using the :option:`test
|
| 532 |
+
--timing` option.
|
| 533 |
+
|
| 534 |
+
* :class:`~django.test.Client` now preserves the request query string when
|
| 535 |
+
following 307 and 308 redirects.
|
| 536 |
+
|
| 537 |
+
* The new :meth:`.TestCase.captureOnCommitCallbacks` method captures callback
|
| 538 |
+
functions passed to :func:`transaction.on_commit()
|
| 539 |
+
<django.db.transaction.on_commit>` in a list. This allows you to test such
|
| 540 |
+
callbacks without using the slower :class:`.TransactionTestCase`.
|
| 541 |
+
|
| 542 |
+
* :meth:`TransactionTestCase.assertQuerysetEqual()
|
| 543 |
+
<django.test.TransactionTestCase.assertQuerySetEqual>` now supports direct
|
| 544 |
+
comparison against another queryset rather than being restricted to
|
| 545 |
+
comparison against a list of string representations of objects when using the
|
| 546 |
+
default value for the ``transform`` argument.
|
| 547 |
+
|
| 548 |
+
Utilities
|
| 549 |
+
~~~~~~~~~
|
| 550 |
+
|
| 551 |
+
* The new ``depth`` parameter of ``django.utils.timesince.timesince()`` and
|
| 552 |
+
``django.utils.timesince.timeuntil()`` functions allows specifying the number
|
| 553 |
+
of adjacent time units to return.
|
| 554 |
+
|
| 555 |
+
Validators
|
| 556 |
+
~~~~~~~~~~
|
| 557 |
+
|
| 558 |
+
* Built-in validators now include the provided value in the ``params`` argument
|
| 559 |
+
of a raised :exc:`~django.core.exceptions.ValidationError`. This allows
|
| 560 |
+
custom error messages to use the ``%(value)s`` placeholder.
|
| 561 |
+
|
| 562 |
+
* The :class:`.ValidationError` equality operator now ignores ``messages`` and
|
| 563 |
+
``params`` ordering.
|
| 564 |
+
|
| 565 |
+
.. _backwards-incompatible-3.2:
|
| 566 |
+
|
| 567 |
+
Backwards incompatible changes in 3.2
|
| 568 |
+
=====================================
|
| 569 |
+
|
| 570 |
+
Database backend API
|
| 571 |
+
--------------------
|
| 572 |
+
|
| 573 |
+
This section describes changes that may be needed in third-party database
|
| 574 |
+
backends.
|
| 575 |
+
|
| 576 |
+
* The new ``DatabaseFeatures.introspected_field_types`` property replaces these
|
| 577 |
+
features:
|
| 578 |
+
|
| 579 |
+
* ``can_introspect_autofield``
|
| 580 |
+
* ``can_introspect_big_integer_field``
|
| 581 |
+
* ``can_introspect_binary_field``
|
| 582 |
+
* ``can_introspect_decimal_field``
|
| 583 |
+
* ``can_introspect_duration_field``
|
| 584 |
+
* ``can_introspect_ip_address_field``
|
| 585 |
+
* ``can_introspect_positive_integer_field``
|
| 586 |
+
* ``can_introspect_small_integer_field``
|
| 587 |
+
* ``can_introspect_time_field``
|
| 588 |
+
* ``introspected_big_auto_field_type``
|
| 589 |
+
* ``introspected_small_auto_field_type``
|
| 590 |
+
* ``introspected_boolean_field_type``
|
| 591 |
+
|
| 592 |
+
* To enable support for covering indexes (:attr:`.Index.include`) and covering
|
| 593 |
+
unique constraints (:attr:`.UniqueConstraint.include`), set
|
| 594 |
+
``DatabaseFeatures.supports_covering_indexes`` to ``True``.
|
| 595 |
+
|
| 596 |
+
* Third-party database backends must implement support for column database
|
| 597 |
+
collations on ``CharField``\s and ``TextField``\s or set
|
| 598 |
+
``DatabaseFeatures.supports_collation_on_charfield`` and
|
| 599 |
+
``DatabaseFeatures.supports_collation_on_textfield`` to ``False``. If
|
| 600 |
+
non-deterministic collations are not supported, set
|
| 601 |
+
``supports_non_deterministic_collations`` to ``False``.
|
| 602 |
+
|
| 603 |
+
* ``DatabaseOperations.random_function_sql()`` is removed in favor of the new
|
| 604 |
+
:class:`~django.db.models.functions.Random` database function.
|
| 605 |
+
|
| 606 |
+
* ``DatabaseOperations.date_trunc_sql()`` and
|
| 607 |
+
``DatabaseOperations.time_trunc_sql()`` now take the optional ``tzname``
|
| 608 |
+
argument in order to truncate in a specific timezone.
|
| 609 |
+
|
| 610 |
+
* ``DatabaseClient.runshell()`` now gets arguments and an optional dictionary
|
| 611 |
+
with environment variables to the underlying command-line client from
|
| 612 |
+
``DatabaseClient.settings_to_cmd_args_env()`` method. Third-party database
|
| 613 |
+
backends must implement ``DatabaseClient.settings_to_cmd_args_env()`` or
|
| 614 |
+
override ``DatabaseClient.runshell()``.
|
| 615 |
+
|
| 616 |
+
* Third-party database backends must implement support for functional indexes
|
| 617 |
+
(:attr:`.Index.expressions`) or set
|
| 618 |
+
``DatabaseFeatures.supports_expression_indexes`` to ``False``. If ``COLLATE``
|
| 619 |
+
is not a part of the ``CREATE INDEX`` statement, set
|
| 620 |
+
``DatabaseFeatures.collate_as_index_expression`` to ``True``.
|
| 621 |
+
|
| 622 |
+
:mod:`django.contrib.admin`
|
| 623 |
+
---------------------------
|
| 624 |
+
|
| 625 |
+
* Pagination links in the admin are now 1-indexed instead of 0-indexed, i.e.
|
| 626 |
+
the query string for the first page is ``?p=1`` instead of ``?p=0``.
|
| 627 |
+
|
| 628 |
+
* The new admin catch-all view will break URL patterns routed after the admin
|
| 629 |
+
URLs and matching the admin URL prefix. You can either adjust your URL
|
| 630 |
+
ordering or, if necessary, set :attr:`AdminSite.final_catch_all_view
|
| 631 |
+
<django.contrib.admin.AdminSite.final_catch_all_view>` to ``False``,
|
| 632 |
+
disabling the catch-all view. See :ref:`whats-new-3.2` for more details.
|
| 633 |
+
|
| 634 |
+
* Minified JavaScript files are no longer included with the admin. If you
|
| 635 |
+
require these files to be minified, consider using a third party app or
|
| 636 |
+
external build tool. The minified vendored JavaScript files packaged with the
|
| 637 |
+
admin (e.g. :ref:`jquery.min.js <contrib-admin-jquery>`) are still included.
|
| 638 |
+
|
| 639 |
+
* :attr:`.ModelAdmin.prepopulated_fields` no longer strips English stop words,
|
| 640 |
+
such as ``'a'`` or ``'an'``.
|
| 641 |
+
|
| 642 |
+
:mod:`django.contrib.gis`
|
| 643 |
+
-------------------------
|
| 644 |
+
|
| 645 |
+
* Support for PostGIS 2.2 is removed.
|
| 646 |
+
|
| 647 |
+
* The Oracle backend now clones polygons (and geometry collections containing
|
| 648 |
+
polygons) before reorienting them and saving them to the database. They are
|
| 649 |
+
no longer mutated in place. You might notice this if you use the polygons
|
| 650 |
+
after a model is saved.
|
| 651 |
+
|
| 652 |
+
Dropped support for PostgreSQL 9.5
|
| 653 |
+
----------------------------------
|
| 654 |
+
|
| 655 |
+
Upstream support for PostgreSQL 9.5 ends in February 2021. Django 3.2 supports
|
| 656 |
+
PostgreSQL 9.6 and higher.
|
| 657 |
+
|
| 658 |
+
Dropped support for MySQL 5.6
|
| 659 |
+
-----------------------------
|
| 660 |
+
|
| 661 |
+
The end of upstream support for MySQL 5.6 is April 2021. Django 3.2 supports
|
| 662 |
+
MySQL 5.7 and higher.
|
| 663 |
+
|
| 664 |
+
Miscellaneous
|
| 665 |
+
-------------
|
| 666 |
+
|
| 667 |
+
* Django now supports non-``pytz`` time zones, such as Python 3.9+'s
|
| 668 |
+
:mod:`zoneinfo` module and its backport.
|
| 669 |
+
|
| 670 |
+
* The undocumented ``SpatiaLiteOperations.proj4_version()`` method is renamed
|
| 671 |
+
to ``proj_version()``.
|
| 672 |
+
|
| 673 |
+
* :func:`~django.utils.text.slugify` now removes leading and trailing dashes
|
| 674 |
+
and underscores.
|
| 675 |
+
|
| 676 |
+
* The :tfilter:`intcomma` and :tfilter:`intword` template filters no longer
|
| 677 |
+
depend on the ``USE_L10N`` setting.
|
| 678 |
+
|
| 679 |
+
* Support for ``argon2-cffi`` < 19.1.0 is removed.
|
| 680 |
+
|
| 681 |
+
* The cache keys no longer includes the language when internationalization is
|
| 682 |
+
disabled (``USE_I18N = False``) and localization is enabled
|
| 683 |
+
(``USE_L10N = True``). After upgrading to Django 3.2 in such configurations,
|
| 684 |
+
the first request to any previously cached value will be a cache miss.
|
| 685 |
+
|
| 686 |
+
* ``ForeignKey.validate()`` now uses
|
| 687 |
+
:attr:`~django.db.models.Model._base_manager` rather than
|
| 688 |
+
:attr:`~django.db.models.Model._default_manager` to check that related
|
| 689 |
+
instances exist.
|
| 690 |
+
|
| 691 |
+
* When an application defines an :class:`~django.apps.AppConfig` subclass in
|
| 692 |
+
an ``apps.py`` submodule, Django now uses this configuration automatically,
|
| 693 |
+
even if it isn't enabled with ``default_app_config``. Set ``default = False``
|
| 694 |
+
in the :class:`~django.apps.AppConfig` subclass if you need to prevent this
|
| 695 |
+
behavior. See :ref:`whats-new-3.2` for more details.
|
| 696 |
+
|
| 697 |
+
* Instantiating an abstract model now raises ``TypeError``.
|
| 698 |
+
|
| 699 |
+
* Keyword arguments to :func:`~django.test.utils.setup_databases` are now
|
| 700 |
+
keyword-only.
|
| 701 |
+
|
| 702 |
+
* The undocumented ``django.utils.http.limited_parse_qsl()`` function is
|
| 703 |
+
removed. Please use :func:`urllib.parse.parse_qsl` instead.
|
| 704 |
+
|
| 705 |
+
* ``django.test.utils.TestContextDecorator`` now uses
|
| 706 |
+
:py:meth:`~unittest.TestCase.addCleanup` so that cleanups registered in the
|
| 707 |
+
:py:meth:`~unittest.TestCase.setUp` method are called before
|
| 708 |
+
``TestContextDecorator.disable()``.
|
| 709 |
+
|
| 710 |
+
* ``SessionMiddleware`` now raises a
|
| 711 |
+
:exc:`~django.contrib.sessions.exceptions.SessionInterrupted` exception
|
| 712 |
+
instead of :exc:`~django.core.exceptions.SuspiciousOperation` when a session
|
| 713 |
+
is destroyed in a concurrent request.
|
| 714 |
+
|
| 715 |
+
* The :class:`django.db.models.Field` equality operator now correctly
|
| 716 |
+
distinguishes inherited field instances across models. Additionally, the
|
| 717 |
+
ordering of such fields is now defined.
|
| 718 |
+
|
| 719 |
+
* The undocumented ``django.core.files.locks.lock()`` function now returns
|
| 720 |
+
``False`` if the file cannot be locked, instead of raising
|
| 721 |
+
:exc:`BlockingIOError`.
|
| 722 |
+
|
| 723 |
+
* The password reset mechanism now invalidates tokens when the user email is
|
| 724 |
+
changed.
|
| 725 |
+
|
| 726 |
+
* :djadmin:`makemessages` command no longer processes invalid locales specified
|
| 727 |
+
using :option:`makemessages --locale` option, when they contain hyphens
|
| 728 |
+
(``'-'``).
|
| 729 |
+
|
| 730 |
+
* The ``django.contrib.auth.forms.ReadOnlyPasswordHashField`` form field is now
|
| 731 |
+
:attr:`~django.forms.Field.disabled` by default. Therefore
|
| 732 |
+
``UserChangeForm.clean_password()`` is no longer required to return the
|
| 733 |
+
initial value.
|
| 734 |
+
|
| 735 |
+
* The ``cache.get_many()``, ``get_or_set()``, ``has_key()``, ``incr()``,
|
| 736 |
+
``decr()``, ``incr_version()``, and ``decr_version()`` cache operations now
|
| 737 |
+
correctly handle ``None`` stored in the cache, in the same way as any other
|
| 738 |
+
value, instead of behaving as though the key didn't exist.
|
| 739 |
+
|
| 740 |
+
Due to a ``python-memcached`` limitation, the previous behavior is kept for
|
| 741 |
+
the deprecated ``MemcachedCache`` backend.
|
| 742 |
+
|
| 743 |
+
* The minimum supported version of SQLite is increased from 3.8.3 to 3.9.0.
|
| 744 |
+
|
| 745 |
+
* :class:`~django.contrib.messages.storage.cookie.CookieStorage` now stores
|
| 746 |
+
messages in the :rfc:`6265` compliant format. Support for cookies that use
|
| 747 |
+
the old format remains until Django 4.1.
|
| 748 |
+
|
| 749 |
+
* The minimum supported version of ``asgiref`` is increased from 3.2.10 to
|
| 750 |
+
3.3.2.
|
| 751 |
+
|
| 752 |
+
.. _deprecated-features-3.2:
|
| 753 |
+
|
| 754 |
+
Features deprecated in 3.2
|
| 755 |
+
==========================
|
| 756 |
+
|
| 757 |
+
Miscellaneous
|
| 758 |
+
-------------
|
| 759 |
+
|
| 760 |
+
* Assigning objects which don't support creating deep copies with
|
| 761 |
+
:py:func:`copy.deepcopy` to class attributes in
|
| 762 |
+
:meth:`.TestCase.setUpTestData` is deprecated.
|
| 763 |
+
|
| 764 |
+
* Using a boolean value in :attr:`.BaseCommand.requires_system_checks` is
|
| 765 |
+
deprecated. Use ``'__all__'`` instead of ``True``, and ``[]`` (an empty list)
|
| 766 |
+
instead of ``False``.
|
| 767 |
+
|
| 768 |
+
* The ``whitelist`` argument and ``domain_whitelist`` attribute of
|
| 769 |
+
:class:`~django.core.validators.EmailValidator` are deprecated. Use
|
| 770 |
+
``allowlist`` instead of ``whitelist``, and ``domain_allowlist`` instead of
|
| 771 |
+
``domain_whitelist``. You may need to rename ``whitelist`` in existing
|
| 772 |
+
migrations.
|
| 773 |
+
|
| 774 |
+
* The ``default_app_config`` application configuration variable is deprecated,
|
| 775 |
+
due to the now automatic ``AppConfig`` discovery. See :ref:`whats-new-3.2`
|
| 776 |
+
for more details.
|
| 777 |
+
|
| 778 |
+
* Automatically calling ``repr()`` on a queryset in
|
| 779 |
+
``TransactionTestCase.assertQuerysetEqual()``, when compared to string
|
| 780 |
+
values, is deprecated. If you need the previous behavior, explicitly set
|
| 781 |
+
``transform`` to ``repr``.
|
| 782 |
+
|
| 783 |
+
* The ``django.core.cache.backends.memcached.MemcachedCache`` backend is
|
| 784 |
+
deprecated as ``python-memcached`` has some problems and seems to be
|
| 785 |
+
unmaintained. Use ``django.core.cache.backends.memcached.PyMemcacheCache``
|
| 786 |
+
or ``django.core.cache.backends.memcached.PyLibMCCache`` instead.
|
| 787 |
+
|
| 788 |
+
* The format of messages used by
|
| 789 |
+
``django.contrib.messages.storage.cookie.CookieStorage`` is different from
|
| 790 |
+
the format generated by older versions of Django. Support for the old format
|
| 791 |
+
remains until Django 4.1.
|
testbed/django__django/docs/releases/4.0.txt
ADDED
|
@@ -0,0 +1,773 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
========================
|
| 2 |
+
Django 4.0 release notes
|
| 3 |
+
========================
|
| 4 |
+
|
| 5 |
+
*December 7, 2021*
|
| 6 |
+
|
| 7 |
+
Welcome to Django 4.0!
|
| 8 |
+
|
| 9 |
+
These release notes cover the :ref:`new features <whats-new-4.0>`, as well as
|
| 10 |
+
some :ref:`backwards incompatible changes <backwards-incompatible-4.0>` you'll
|
| 11 |
+
want to be aware of when upgrading from Django 3.2 or earlier. We've
|
| 12 |
+
:ref:`begun the deprecation process for some features
|
| 13 |
+
<deprecated-features-4.0>`.
|
| 14 |
+
|
| 15 |
+
See the :doc:`/howto/upgrade-version` guide if you're updating an existing
|
| 16 |
+
project.
|
| 17 |
+
|
| 18 |
+
Python compatibility
|
| 19 |
+
====================
|
| 20 |
+
|
| 21 |
+
Django 4.0 supports Python 3.8, 3.9, and 3.10. We **highly recommend** and only
|
| 22 |
+
officially support the latest release of each series.
|
| 23 |
+
|
| 24 |
+
The Django 3.2.x series is the last to support Python 3.6 and 3.7.
|
| 25 |
+
|
| 26 |
+
.. _whats-new-4.0:
|
| 27 |
+
|
| 28 |
+
What's new in Django 4.0
|
| 29 |
+
========================
|
| 30 |
+
|
| 31 |
+
``zoneinfo`` default timezone implementation
|
| 32 |
+
--------------------------------------------
|
| 33 |
+
|
| 34 |
+
The Python standard library's :mod:`zoneinfo` is now the default timezone
|
| 35 |
+
implementation in Django.
|
| 36 |
+
|
| 37 |
+
This is the next step in the migration from using ``pytz`` to using
|
| 38 |
+
:mod:`zoneinfo`. Django 3.2 allowed the use of non-``pytz`` time zones. Django
|
| 39 |
+
4.0 makes ``zoneinfo`` the default implementation. Support for ``pytz`` is now
|
| 40 |
+
deprecated and will be removed in Django 5.0.
|
| 41 |
+
|
| 42 |
+
:mod:`zoneinfo` is part of the Python standard library from Python 3.9. The
|
| 43 |
+
``backports.zoneinfo`` package is automatically installed alongside Django if
|
| 44 |
+
you are using Python 3.8.
|
| 45 |
+
|
| 46 |
+
The move to ``zoneinfo`` should be largely transparent. Selection of the
|
| 47 |
+
current timezone, conversion of datetime instances to the current timezone in
|
| 48 |
+
forms and templates, as well as operations on aware datetimes in UTC are
|
| 49 |
+
unaffected.
|
| 50 |
+
|
| 51 |
+
However, if you are working with non-UTC time zones, and using the ``pytz``
|
| 52 |
+
``normalize()`` and ``localize()`` APIs, possibly with the :setting:`TIME_ZONE
|
| 53 |
+
<DATABASE-TIME_ZONE>` setting, you will need to audit your code, since ``pytz``
|
| 54 |
+
and ``zoneinfo`` are not entirely equivalent.
|
| 55 |
+
|
| 56 |
+
To give time for such an audit, the transitional ``USE_DEPRECATED_PYTZ``
|
| 57 |
+
setting allows continued use of ``pytz`` during the 4.x release cycle. This
|
| 58 |
+
setting will be removed in Django 5.0.
|
| 59 |
+
|
| 60 |
+
In addition, a `pytz_deprecation_shim`_ package, created by the ``zoneinfo``
|
| 61 |
+
author, can be used to assist with the migration from ``pytz``. This package
|
| 62 |
+
provides shims to help you safely remove ``pytz``, and has a detailed
|
| 63 |
+
`migration guide`_ showing how to move to the new ``zoneinfo`` APIs.
|
| 64 |
+
|
| 65 |
+
Using `pytz_deprecation_shim`_ and the ``USE_DEPRECATED_PYTZ``
|
| 66 |
+
transitional setting is recommended if you need a gradual update path.
|
| 67 |
+
|
| 68 |
+
.. _pytz_deprecation_shim: https://pytz-deprecation-shim.readthedocs.io/en/latest/index.html
|
| 69 |
+
.. _migration guide: https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
|
| 70 |
+
|
| 71 |
+
Functional unique constraints
|
| 72 |
+
-----------------------------
|
| 73 |
+
|
| 74 |
+
The new :attr:`*expressions <django.db.models.UniqueConstraint.expressions>`
|
| 75 |
+
positional argument of
|
| 76 |
+
:class:`UniqueConstraint() <django.db.models.UniqueConstraint>` enables
|
| 77 |
+
creating functional unique constraints on expressions and database functions.
|
| 78 |
+
For example::
|
| 79 |
+
|
| 80 |
+
from django.db import models
|
| 81 |
+
from django.db.models import UniqueConstraint
|
| 82 |
+
from django.db.models.functions import Lower
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class MyModel(models.Model):
|
| 86 |
+
first_name = models.CharField(max_length=255)
|
| 87 |
+
last_name = models.CharField(max_length=255)
|
| 88 |
+
|
| 89 |
+
class Meta:
|
| 90 |
+
constraints = [
|
| 91 |
+
UniqueConstraint(
|
| 92 |
+
Lower("first_name"),
|
| 93 |
+
Lower("last_name").desc(),
|
| 94 |
+
name="first_last_name_unique",
|
| 95 |
+
),
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
Functional unique constraints are added to models using the
|
| 99 |
+
:attr:`Meta.constraints <django.db.models.Options.constraints>` option.
|
| 100 |
+
|
| 101 |
+
``scrypt`` password hasher
|
| 102 |
+
--------------------------
|
| 103 |
+
|
| 104 |
+
The new :ref:`scrypt password hasher <scrypt-usage>` is more secure and
|
| 105 |
+
recommended over PBKDF2. However, it's not the default as it requires OpenSSL
|
| 106 |
+
1.1+ and more memory.
|
| 107 |
+
|
| 108 |
+
Redis cache backend
|
| 109 |
+
-------------------
|
| 110 |
+
|
| 111 |
+
The new ``django.core.cache.backends.redis.RedisCache`` cache backend provides
|
| 112 |
+
built-in support for caching with Redis. :pypi:`redis-py <redis>` 3.0.0 or
|
| 113 |
+
higher is required. For more details, see the :ref:`documentation on caching
|
| 114 |
+
with Redis in Django <redis>`.
|
| 115 |
+
|
| 116 |
+
Template based form rendering
|
| 117 |
+
-----------------------------
|
| 118 |
+
|
| 119 |
+
:class:`Forms <django.forms.Form>`, :doc:`Formsets </topics/forms/formsets>`,
|
| 120 |
+
and :class:`~django.forms.ErrorList` are now rendered using the template engine
|
| 121 |
+
to enhance customization. See the new :meth:`~django.forms.Form.render`,
|
| 122 |
+
:meth:`~django.forms.Form.get_context`, and
|
| 123 |
+
:attr:`~django.forms.Form.template_name` for ``Form`` and
|
| 124 |
+
:ref:`formset rendering <formset-rendering>` for ``Formset``.
|
| 125 |
+
|
| 126 |
+
Minor features
|
| 127 |
+
--------------
|
| 128 |
+
|
| 129 |
+
:mod:`django.contrib.admin`
|
| 130 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 131 |
+
|
| 132 |
+
* The ``admin/base.html`` template now has a new block ``header`` which
|
| 133 |
+
contains the admin site header.
|
| 134 |
+
|
| 135 |
+
* The new :meth:`.ModelAdmin.get_formset_kwargs` method allows customizing the
|
| 136 |
+
keyword arguments passed to the constructor of a formset.
|
| 137 |
+
|
| 138 |
+
* The navigation sidebar now has a quick filter toolbar.
|
| 139 |
+
|
| 140 |
+
* The new context variable ``model`` which contains the model class for each
|
| 141 |
+
model is added to the :meth:`.AdminSite.each_context` method.
|
| 142 |
+
|
| 143 |
+
* The new :attr:`.ModelAdmin.search_help_text` attribute allows specifying a
|
| 144 |
+
descriptive text for the search box.
|
| 145 |
+
|
| 146 |
+
* The :attr:`.InlineModelAdmin.verbose_name_plural` attribute now fallbacks to
|
| 147 |
+
the :attr:`.InlineModelAdmin.verbose_name` + ``'s'``.
|
| 148 |
+
|
| 149 |
+
* jQuery is upgraded from version 3.5.1 to 3.6.0.
|
| 150 |
+
|
| 151 |
+
:mod:`django.contrib.admindocs`
|
| 152 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 153 |
+
|
| 154 |
+
* The admindocs now allows esoteric setups where :setting:`ROOT_URLCONF` is not
|
| 155 |
+
a string.
|
| 156 |
+
|
| 157 |
+
* The model section of the ``admindocs`` now shows cached properties.
|
| 158 |
+
|
| 159 |
+
:mod:`django.contrib.auth`
|
| 160 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 161 |
+
|
| 162 |
+
* The default iteration count for the PBKDF2 password hasher is increased from
|
| 163 |
+
260,000 to 320,000.
|
| 164 |
+
|
| 165 |
+
* The new
|
| 166 |
+
:attr:`LoginView.next_page <django.contrib.auth.views.LoginView.next_page>`
|
| 167 |
+
attribute and
|
| 168 |
+
:meth:`~django.contrib.auth.views.LoginView.get_default_redirect_url` method
|
| 169 |
+
allow customizing the redirect after login.
|
| 170 |
+
|
| 171 |
+
:mod:`django.contrib.gis`
|
| 172 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 173 |
+
|
| 174 |
+
* Added support for SpatiaLite 5.
|
| 175 |
+
|
| 176 |
+
* :class:`~django.contrib.gis.gdal.GDALRaster` now allows creating rasters in
|
| 177 |
+
any GDAL virtual filesystem.
|
| 178 |
+
|
| 179 |
+
* The new :class:`~django.contrib.gis.admin.GISModelAdmin` class allows
|
| 180 |
+
customizing the widget used for ``GeometryField``. This is encouraged instead
|
| 181 |
+
of deprecated ``GeoModelAdmin`` and ``OSMGeoAdmin``.
|
| 182 |
+
|
| 183 |
+
:mod:`django.contrib.postgres`
|
| 184 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 185 |
+
|
| 186 |
+
* The PostgreSQL backend now supports connecting by a service name. See
|
| 187 |
+
:ref:`postgresql-connection-settings` for more details.
|
| 188 |
+
|
| 189 |
+
* The new :class:`~django.contrib.postgres.operations.AddConstraintNotValid`
|
| 190 |
+
operation allows creating check constraints on PostgreSQL without verifying
|
| 191 |
+
that all existing rows satisfy the new constraint.
|
| 192 |
+
|
| 193 |
+
* The new :class:`~django.contrib.postgres.operations.ValidateConstraint`
|
| 194 |
+
operation allows validating check constraints which were created using
|
| 195 |
+
:class:`~django.contrib.postgres.operations.AddConstraintNotValid` on
|
| 196 |
+
PostgreSQL.
|
| 197 |
+
|
| 198 |
+
* The new
|
| 199 |
+
:class:`ArraySubquery() <django.contrib.postgres.expressions.ArraySubquery>`
|
| 200 |
+
expression allows using subqueries to construct lists of values on
|
| 201 |
+
PostgreSQL.
|
| 202 |
+
|
| 203 |
+
* The new :lookup:`trigram_word_similar` lookup, and the
|
| 204 |
+
:class:`TrigramWordDistance()
|
| 205 |
+
<django.contrib.postgres.search.TrigramWordDistance>` and
|
| 206 |
+
:class:`TrigramWordSimilarity()
|
| 207 |
+
<django.contrib.postgres.search.TrigramWordSimilarity>` expressions allow
|
| 208 |
+
using trigram word similarity.
|
| 209 |
+
|
| 210 |
+
:mod:`django.contrib.staticfiles`
|
| 211 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 212 |
+
|
| 213 |
+
* :class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` now
|
| 214 |
+
replaces paths to JavaScript source map references with their hashed
|
| 215 |
+
counterparts.
|
| 216 |
+
|
| 217 |
+
* The new ``manifest_storage`` argument of
|
| 218 |
+
:class:`~django.contrib.staticfiles.storage.ManifestFilesMixin` and
|
| 219 |
+
:class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage`
|
| 220 |
+
allows customizing the manifest file storage.
|
| 221 |
+
|
| 222 |
+
Cache
|
| 223 |
+
~~~~~
|
| 224 |
+
|
| 225 |
+
* The new async API for ``django.core.cache.backends.base.BaseCache`` begins
|
| 226 |
+
the process of making cache backends async-compatible. The new async methods
|
| 227 |
+
all have ``a`` prefixed names, e.g. ``aadd()``, ``aget()``, ``aset()``,
|
| 228 |
+
``aget_or_set()``, or ``adelete_many()``.
|
| 229 |
+
|
| 230 |
+
Going forward, the ``a`` prefix will be used for async variants of methods
|
| 231 |
+
generally.
|
| 232 |
+
|
| 233 |
+
CSRF
|
| 234 |
+
~~~~
|
| 235 |
+
|
| 236 |
+
* CSRF protection now consults the ``Origin`` header, if present. To facilitate
|
| 237 |
+
this, :ref:`some changes <csrf-trusted-origins-changes-4.0>` to the
|
| 238 |
+
:setting:`CSRF_TRUSTED_ORIGINS` setting are required.
|
| 239 |
+
|
| 240 |
+
Forms
|
| 241 |
+
~~~~~
|
| 242 |
+
|
| 243 |
+
* :class:`~django.forms.ModelChoiceField` now includes the provided value in
|
| 244 |
+
the ``params`` argument of a raised
|
| 245 |
+
:exc:`~django.core.exceptions.ValidationError` for the ``invalid_choice``
|
| 246 |
+
error message. This allows custom error messages to use the ``%(value)s``
|
| 247 |
+
placeholder.
|
| 248 |
+
|
| 249 |
+
* :class:`~django.forms.formsets.BaseFormSet` now renders non-form errors with
|
| 250 |
+
an additional class of ``nonform`` to help distinguish them from
|
| 251 |
+
form-specific errors.
|
| 252 |
+
|
| 253 |
+
* :class:`~django.forms.formsets.BaseFormSet` now allows customizing the widget
|
| 254 |
+
used when deleting forms via
|
| 255 |
+
:attr:`~django.forms.formsets.BaseFormSet.can_delete` by setting the
|
| 256 |
+
:attr:`~django.forms.formsets.BaseFormSet.deletion_widget` attribute or
|
| 257 |
+
overriding :meth:`~django.forms.formsets.BaseFormSet.get_deletion_widget`
|
| 258 |
+
method.
|
| 259 |
+
|
| 260 |
+
Internationalization
|
| 261 |
+
~~~~~~~~~~~~~~~~~~~~
|
| 262 |
+
|
| 263 |
+
* Added support and translations for the Malay language.
|
| 264 |
+
|
| 265 |
+
Generic Views
|
| 266 |
+
~~~~~~~~~~~~~
|
| 267 |
+
|
| 268 |
+
* :class:`~django.views.generic.edit.DeleteView` now uses
|
| 269 |
+
:class:`~django.views.generic.edit.FormMixin`, allowing you to provide a
|
| 270 |
+
:class:`~django.forms.Form` subclass, with a checkbox for example, to confirm
|
| 271 |
+
deletion. In addition, this allows ``DeleteView`` to function with
|
| 272 |
+
:class:`django.contrib.messages.views.SuccessMessageMixin`.
|
| 273 |
+
|
| 274 |
+
In accordance with ``FormMixin``, object deletion for POST requests is
|
| 275 |
+
handled in ``form_valid()``. Custom delete logic in ``delete()`` handlers
|
| 276 |
+
should be moved to ``form_valid()``, or a shared helper method, as needed.
|
| 277 |
+
|
| 278 |
+
Logging
|
| 279 |
+
~~~~~~~
|
| 280 |
+
|
| 281 |
+
* The alias of the database used in an SQL call is now passed as extra context
|
| 282 |
+
along with each message to the :ref:`django-db-logger` logger.
|
| 283 |
+
|
| 284 |
+
Management Commands
|
| 285 |
+
~~~~~~~~~~~~~~~~~~~
|
| 286 |
+
|
| 287 |
+
* The :djadmin:`runserver` management command now supports the
|
| 288 |
+
:option:`--skip-checks` option.
|
| 289 |
+
|
| 290 |
+
* On PostgreSQL, :djadmin:`dbshell` now supports specifying a password file.
|
| 291 |
+
|
| 292 |
+
* The :djadmin:`shell` command now respects :py:data:`sys.__interactivehook__`
|
| 293 |
+
at startup. This allows loading shell history between interactive sessions.
|
| 294 |
+
As a consequence, ``readline`` is no longer loaded if running in *isolated*
|
| 295 |
+
mode.
|
| 296 |
+
|
| 297 |
+
* The new :attr:`BaseCommand.suppressed_base_arguments
|
| 298 |
+
<django.core.management.BaseCommand.suppressed_base_arguments>` attribute
|
| 299 |
+
allows suppressing unsupported default command options in the help output.
|
| 300 |
+
|
| 301 |
+
* The new :option:`startapp --exclude` and :option:`startproject --exclude`
|
| 302 |
+
options allow excluding directories from the template.
|
| 303 |
+
|
| 304 |
+
Models
|
| 305 |
+
~~~~~~
|
| 306 |
+
|
| 307 |
+
* New :meth:`QuerySet.contains(obj) <.QuerySet.contains>` method returns
|
| 308 |
+
whether the queryset contains the given object. This tries to perform the
|
| 309 |
+
query in the simplest and fastest way possible.
|
| 310 |
+
|
| 311 |
+
* The new ``precision`` argument of the
|
| 312 |
+
:class:`Round() <django.db.models.functions.Round>` database function allows
|
| 313 |
+
specifying the number of decimal places after rounding.
|
| 314 |
+
|
| 315 |
+
* :meth:`.QuerySet.bulk_create` now sets the primary key on objects when using
|
| 316 |
+
SQLite 3.35+.
|
| 317 |
+
|
| 318 |
+
* :class:`~django.db.models.DurationField` now supports multiplying and
|
| 319 |
+
dividing by scalar values on SQLite.
|
| 320 |
+
|
| 321 |
+
* :meth:`.QuerySet.bulk_update` now returns the number of objects updated.
|
| 322 |
+
|
| 323 |
+
* The new :attr:`.Expression.empty_result_set_value` attribute allows
|
| 324 |
+
specifying a value to return when the function is used over an empty result
|
| 325 |
+
set.
|
| 326 |
+
|
| 327 |
+
* The ``skip_locked`` argument of :meth:`.QuerySet.select_for_update()` is now
|
| 328 |
+
allowed on MariaDB 10.6+.
|
| 329 |
+
|
| 330 |
+
* :class:`~django.db.models.Lookup` expressions may now be used in ``QuerySet``
|
| 331 |
+
annotations, aggregations, and directly in filters.
|
| 332 |
+
|
| 333 |
+
* The new :ref:`default <aggregate-default>` argument for built-in aggregates
|
| 334 |
+
allows specifying a value to be returned when the queryset (or grouping)
|
| 335 |
+
contains no entries, rather than ``None``.
|
| 336 |
+
|
| 337 |
+
Requests and Responses
|
| 338 |
+
~~~~~~~~~~~~~~~~~~~~~~
|
| 339 |
+
|
| 340 |
+
* The :class:`~django.middleware.security.SecurityMiddleware` now adds the
|
| 341 |
+
:ref:`Cross-Origin Opener Policy <cross-origin-opener-policy>` header with a
|
| 342 |
+
value of ``'same-origin'`` to prevent cross-origin popups from sharing the
|
| 343 |
+
same browsing context. You can prevent this header from being added by
|
| 344 |
+
setting the :setting:`SECURE_CROSS_ORIGIN_OPENER_POLICY` setting to ``None``.
|
| 345 |
+
|
| 346 |
+
Signals
|
| 347 |
+
~~~~~~~
|
| 348 |
+
|
| 349 |
+
* The new ``stdout`` argument for :func:`~django.db.models.signals.pre_migrate`
|
| 350 |
+
and :func:`~django.db.models.signals.post_migrate` signals allows redirecting
|
| 351 |
+
output to a stream-like object. It should be preferred over
|
| 352 |
+
:py:data:`sys.stdout` and :py:func:`print` when emitting verbose output in
|
| 353 |
+
order to allow proper capture when testing.
|
| 354 |
+
|
| 355 |
+
Templates
|
| 356 |
+
~~~~~~~~~
|
| 357 |
+
|
| 358 |
+
* :tfilter:`floatformat` template filter now allows using the ``u`` suffix to
|
| 359 |
+
force disabling localization.
|
| 360 |
+
|
| 361 |
+
Tests
|
| 362 |
+
~~~~~
|
| 363 |
+
|
| 364 |
+
* The new ``serialized_aliases`` argument of
|
| 365 |
+
:func:`django.test.utils.setup_databases` determines which
|
| 366 |
+
:setting:`DATABASES` aliases test databases should have their state
|
| 367 |
+
serialized to allow usage of the
|
| 368 |
+
:ref:`serialized_rollback <test-case-serialized-rollback>` feature.
|
| 369 |
+
|
| 370 |
+
* Django test runner now supports a :option:`--buffer <test --buffer>` option
|
| 371 |
+
with parallel tests.
|
| 372 |
+
|
| 373 |
+
* The new ``logger`` argument to :class:`~django.test.runner.DiscoverRunner`
|
| 374 |
+
allows a Python :py:ref:`logger <logger>` to be used for logging.
|
| 375 |
+
|
| 376 |
+
* The new :meth:`.DiscoverRunner.log` method provides a way to log messages
|
| 377 |
+
that uses the ``DiscoverRunner.logger``, or prints to the console if not set.
|
| 378 |
+
|
| 379 |
+
* Django test runner now supports a :option:`--shuffle <test --shuffle>` option
|
| 380 |
+
to execute tests in a random order.
|
| 381 |
+
|
| 382 |
+
* The :option:`test --parallel` option now supports the value ``auto`` to run
|
| 383 |
+
one test process for each processor core.
|
| 384 |
+
|
| 385 |
+
* :meth:`.TestCase.captureOnCommitCallbacks` now captures new callbacks added
|
| 386 |
+
while executing :func:`.transaction.on_commit` callbacks.
|
| 387 |
+
|
| 388 |
+
.. _backwards-incompatible-4.0:
|
| 389 |
+
|
| 390 |
+
Backwards incompatible changes in 4.0
|
| 391 |
+
=====================================
|
| 392 |
+
|
| 393 |
+
Database backend API
|
| 394 |
+
--------------------
|
| 395 |
+
|
| 396 |
+
This section describes changes that may be needed in third-party database
|
| 397 |
+
backends.
|
| 398 |
+
|
| 399 |
+
* ``DatabaseOperations.year_lookup_bounds_for_date_field()`` and
|
| 400 |
+
``year_lookup_bounds_for_datetime_field()`` methods now take the optional
|
| 401 |
+
``iso_year`` argument in order to support bounds for ISO-8601 week-numbering
|
| 402 |
+
years.
|
| 403 |
+
|
| 404 |
+
* The second argument of ``DatabaseSchemaEditor._unique_sql()`` and
|
| 405 |
+
``_create_unique_sql()`` methods is now ``fields`` instead of ``columns``.
|
| 406 |
+
|
| 407 |
+
:mod:`django.contrib.gis`
|
| 408 |
+
-------------------------
|
| 409 |
+
|
| 410 |
+
* Support for PostGIS 2.3 is removed.
|
| 411 |
+
|
| 412 |
+
* Support for GDAL 2.0 and GEOS 3.5 is removed.
|
| 413 |
+
|
| 414 |
+
Dropped support for PostgreSQL 9.6
|
| 415 |
+
----------------------------------
|
| 416 |
+
|
| 417 |
+
Upstream support for PostgreSQL 9.6 ends in November 2021. Django 4.0 supports
|
| 418 |
+
PostgreSQL 10 and higher.
|
| 419 |
+
|
| 420 |
+
Also, the minimum supported version of ``psycopg2`` is increased from 2.5.4 to
|
| 421 |
+
2.8.4, as ``psycopg2`` 2.8.4 is the first release to support Python 3.8.
|
| 422 |
+
|
| 423 |
+
Dropped support for Oracle 12.2 and 18c
|
| 424 |
+
---------------------------------------
|
| 425 |
+
|
| 426 |
+
Upstream support for Oracle 12.2 ends in March 2022 and for Oracle 18c it ends
|
| 427 |
+
in June 2021. Django 3.2 will be supported until April 2024. Django 4.0
|
| 428 |
+
officially supports Oracle 19c.
|
| 429 |
+
|
| 430 |
+
.. _csrf-trusted-origins-changes-4.0:
|
| 431 |
+
|
| 432 |
+
``CSRF_TRUSTED_ORIGINS`` changes
|
| 433 |
+
--------------------------------
|
| 434 |
+
|
| 435 |
+
Format change
|
| 436 |
+
~~~~~~~~~~~~~
|
| 437 |
+
|
| 438 |
+
Values in the :setting:`CSRF_TRUSTED_ORIGINS` setting must include the scheme
|
| 439 |
+
(e.g. ``'http://'`` or ``'https://'``) instead of only the hostname.
|
| 440 |
+
|
| 441 |
+
Also, values that started with a dot, must now also include an asterisk before
|
| 442 |
+
the dot. For example, change ``'.example.com'`` to ``'https://*.example.com'``.
|
| 443 |
+
|
| 444 |
+
A system check detects any required changes.
|
| 445 |
+
|
| 446 |
+
Configuring it may now be required
|
| 447 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 448 |
+
|
| 449 |
+
As CSRF protection now consults the ``Origin`` header, you may need to set
|
| 450 |
+
:setting:`CSRF_TRUSTED_ORIGINS`, particularly if you allow requests from
|
| 451 |
+
subdomains by setting :setting:`CSRF_COOKIE_DOMAIN` (or
|
| 452 |
+
:setting:`SESSION_COOKIE_DOMAIN` if :setting:`CSRF_USE_SESSIONS` is enabled) to
|
| 453 |
+
a value starting with a dot.
|
| 454 |
+
|
| 455 |
+
``SecurityMiddleware`` no longer sets the ``X-XSS-Protection`` header
|
| 456 |
+
---------------------------------------------------------------------
|
| 457 |
+
|
| 458 |
+
The :class:`~django.middleware.security.SecurityMiddleware` no longer sets the
|
| 459 |
+
``X-XSS-Protection`` header if the ``SECURE_BROWSER_XSS_FILTER`` setting is
|
| 460 |
+
``True``. The setting is removed.
|
| 461 |
+
|
| 462 |
+
Most modern browsers don't honor the ``X-XSS-Protection`` HTTP header. You can
|
| 463 |
+
use Content-Security-Policy_ without allowing ``'unsafe-inline'`` scripts
|
| 464 |
+
instead.
|
| 465 |
+
|
| 466 |
+
If you want to support legacy browsers and set the header, use this line in a
|
| 467 |
+
custom middleware::
|
| 468 |
+
|
| 469 |
+
response.headers.setdefault("X-XSS-Protection", "1; mode=block")
|
| 470 |
+
|
| 471 |
+
.. _Content-Security-Policy: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
|
| 472 |
+
|
| 473 |
+
Migrations autodetector changes
|
| 474 |
+
-------------------------------
|
| 475 |
+
|
| 476 |
+
The migrations autodetector now uses model states instead of model classes.
|
| 477 |
+
Also, migration operations for ``ForeignKey`` and ``ManyToManyField`` fields no
|
| 478 |
+
longer specify attributes which were not passed to the fields during
|
| 479 |
+
initialization.
|
| 480 |
+
|
| 481 |
+
As a side-effect, running ``makemigrations`` might generate no-op
|
| 482 |
+
``AlterField`` operations for ``ManyToManyField`` and ``ForeignKey`` fields in
|
| 483 |
+
some cases.
|
| 484 |
+
|
| 485 |
+
``DeleteView`` changes
|
| 486 |
+
----------------------
|
| 487 |
+
|
| 488 |
+
:class:`~django.views.generic.edit.DeleteView` now uses
|
| 489 |
+
:class:`~django.views.generic.edit.FormMixin` to handle POST requests. As a
|
| 490 |
+
consequence, any custom deletion logic in ``delete()`` handlers should be
|
| 491 |
+
moved to ``form_valid()``, or a shared helper method, if required.
|
| 492 |
+
|
| 493 |
+
Table and column naming scheme changes on Oracle
|
| 494 |
+
------------------------------------------------
|
| 495 |
+
|
| 496 |
+
Django 4.0 inadvertently changed the table and column naming scheme on Oracle.
|
| 497 |
+
This causes errors for models and fields with names longer than 30 characters.
|
| 498 |
+
Unfortunately, renaming some Oracle tables and columns is required. Use the
|
| 499 |
+
upgrade script in :ticket:`33789 <33789#comment:15>` to generate ``RENAME``
|
| 500 |
+
statements to change naming scheme.
|
| 501 |
+
|
| 502 |
+
Miscellaneous
|
| 503 |
+
-------------
|
| 504 |
+
|
| 505 |
+
* Support for ``cx_Oracle`` < 7.0 is removed.
|
| 506 |
+
|
| 507 |
+
* To allow serving a Django site on a subpath without changing the value of
|
| 508 |
+
:setting:`STATIC_URL`, the leading slash is removed from that setting (now
|
| 509 |
+
``'static/'``) in the default :djadmin:`startproject` template.
|
| 510 |
+
|
| 511 |
+
* The :class:`~django.contrib.admin.AdminSite` method for the admin ``index``
|
| 512 |
+
view is no longer decorated with ``never_cache`` when accessed directly,
|
| 513 |
+
rather than via the recommended ``AdminSite.urls`` property, or
|
| 514 |
+
``AdminSite.get_urls()`` method.
|
| 515 |
+
|
| 516 |
+
* Unsupported operations on a sliced queryset now raise ``TypeError`` instead
|
| 517 |
+
of ``AssertionError``.
|
| 518 |
+
|
| 519 |
+
* The undocumented ``django.test.runner.reorder_suite()`` function is renamed
|
| 520 |
+
to ``reorder_tests()``. It now accepts an iterable of tests rather than a
|
| 521 |
+
test suite, and returns an iterator of tests.
|
| 522 |
+
|
| 523 |
+
* Calling ``FileSystemStorage.delete()`` with an empty ``name`` now raises
|
| 524 |
+
``ValueError`` instead of ``AssertionError``.
|
| 525 |
+
|
| 526 |
+
* Calling ``EmailMultiAlternatives.attach_alternative()`` or
|
| 527 |
+
``EmailMessage.attach()`` with an invalid ``content`` or ``mimetype``
|
| 528 |
+
arguments now raise ``ValueError`` instead of ``AssertionError``.
|
| 529 |
+
|
| 530 |
+
* :meth:`~django.test.SimpleTestCase.assertHTMLEqual` no longer considers a
|
| 531 |
+
non-boolean attribute without a value equal to an attribute with the same
|
| 532 |
+
name and value.
|
| 533 |
+
|
| 534 |
+
* Tests that fail to load, for example due to syntax errors, now always match
|
| 535 |
+
when using :option:`test --tag`.
|
| 536 |
+
|
| 537 |
+
* The undocumented ``django.contrib.admin.utils.lookup_needs_distinct()``
|
| 538 |
+
function is renamed to ``lookup_spawns_duplicates()``.
|
| 539 |
+
|
| 540 |
+
* The undocumented ``HttpRequest.get_raw_uri()`` method is removed. The
|
| 541 |
+
:meth:`.HttpRequest.build_absolute_uri` method may be a suitable alternative.
|
| 542 |
+
|
| 543 |
+
* The ``object`` argument of undocumented ``ModelAdmin.log_addition()``,
|
| 544 |
+
``log_change()``, and ``log_deletion()`` methods is renamed to ``obj``.
|
| 545 |
+
|
| 546 |
+
* :class:`~django.utils.feedgenerator.RssFeed`,
|
| 547 |
+
:class:`~django.utils.feedgenerator.Atom1Feed`, and their subclasses now emit
|
| 548 |
+
elements with no content as self-closing tags.
|
| 549 |
+
|
| 550 |
+
* ``NodeList.render()`` no longer casts the output of ``render()`` method for
|
| 551 |
+
individual nodes to a string. ``Node.render()`` should always return a string
|
| 552 |
+
as documented.
|
| 553 |
+
|
| 554 |
+
* The ``where_class`` property of ``django.db.models.sql.query.Query`` and the
|
| 555 |
+
``where_class`` argument to the private ``get_extra_restriction()`` method of
|
| 556 |
+
``ForeignObject`` and ``ForeignObjectRel`` are removed. If needed, initialize
|
| 557 |
+
``django.db.models.sql.where.WhereNode`` instead.
|
| 558 |
+
|
| 559 |
+
* The ``filter_clause`` argument of the undocumented ``Query.add_filter()``
|
| 560 |
+
method is replaced by two positional arguments ``filter_lhs`` and
|
| 561 |
+
``filter_rhs``.
|
| 562 |
+
|
| 563 |
+
* :class:`~django.middleware.csrf.CsrfViewMiddleware` now uses
|
| 564 |
+
``request.META['CSRF_COOKIE_NEEDS_UPDATE']`` in place of
|
| 565 |
+
``request.META['CSRF_COOKIE_USED']``, ``request.csrf_cookie_needs_reset``,
|
| 566 |
+
and ``response.csrf_cookie_set`` to track whether the CSRF cookie should be
|
| 567 |
+
sent. This is an undocumented, private API.
|
| 568 |
+
|
| 569 |
+
* The undocumented ``TRANSLATOR_COMMENT_MARK`` constant is moved from
|
| 570 |
+
``django.template.base`` to ``django.utils.translation.template``.
|
| 571 |
+
|
| 572 |
+
* The ``real_apps`` argument of the undocumented
|
| 573 |
+
``django.db.migrations.state.ProjectState.__init__()`` method must now be a
|
| 574 |
+
set if provided.
|
| 575 |
+
|
| 576 |
+
* :class:`~django.forms.RadioSelect` and
|
| 577 |
+
:class:`~django.forms.CheckboxSelectMultiple` widgets are now rendered in
|
| 578 |
+
``<div>`` tags so they are announced more concisely by screen readers. If you
|
| 579 |
+
need the previous behavior, :ref:`override the widget template
|
| 580 |
+
<overriding-built-in-widget-templates>` with the appropriate template from
|
| 581 |
+
Django 3.2.
|
| 582 |
+
|
| 583 |
+
* The :tfilter:`floatformat` template filter no longer depends on the
|
| 584 |
+
``USE_L10N`` setting and always returns localized output. Use the ``u``
|
| 585 |
+
suffix to disable localization.
|
| 586 |
+
|
| 587 |
+
* The default value of the ``USE_L10N`` setting is changed to ``True``. See the
|
| 588 |
+
:ref:`Localization section <use_l10n_deprecation>` above for more details.
|
| 589 |
+
|
| 590 |
+
* As part of the :ref:`move to zoneinfo <whats-new-4.0>`,
|
| 591 |
+
``django.utils.timezone.utc`` is changed to alias
|
| 592 |
+
:attr:`datetime.timezone.utc`.
|
| 593 |
+
|
| 594 |
+
* The minimum supported version of ``asgiref`` is increased from 3.3.2 to
|
| 595 |
+
3.4.1.
|
| 596 |
+
|
| 597 |
+
.. _deprecated-features-4.0:
|
| 598 |
+
|
| 599 |
+
Features deprecated in 4.0
|
| 600 |
+
==========================
|
| 601 |
+
|
| 602 |
+
Use of ``pytz`` time zones
|
| 603 |
+
--------------------------
|
| 604 |
+
|
| 605 |
+
As part of the :ref:`move to zoneinfo <whats-new-4.0>`, use of ``pytz`` time
|
| 606 |
+
zones is deprecated.
|
| 607 |
+
|
| 608 |
+
Accordingly, the ``is_dst`` arguments to the following are also deprecated:
|
| 609 |
+
|
| 610 |
+
* :meth:`django.db.models.query.QuerySet.datetimes`
|
| 611 |
+
* :func:`django.db.models.functions.Trunc`
|
| 612 |
+
* :func:`django.db.models.functions.TruncSecond`
|
| 613 |
+
* :func:`django.db.models.functions.TruncMinute`
|
| 614 |
+
* :func:`django.db.models.functions.TruncHour`
|
| 615 |
+
* :func:`django.db.models.functions.TruncDay`
|
| 616 |
+
* :func:`django.db.models.functions.TruncWeek`
|
| 617 |
+
* :func:`django.db.models.functions.TruncMonth`
|
| 618 |
+
* :func:`django.db.models.functions.TruncQuarter`
|
| 619 |
+
* :func:`django.db.models.functions.TruncYear`
|
| 620 |
+
* :func:`django.utils.timezone.make_aware`
|
| 621 |
+
|
| 622 |
+
Support for use of ``pytz`` will be removed in Django 5.0.
|
| 623 |
+
|
| 624 |
+
Time zone support
|
| 625 |
+
-----------------
|
| 626 |
+
|
| 627 |
+
In order to follow good practice, the default value of the :setting:`USE_TZ`
|
| 628 |
+
setting will change from ``False`` to ``True``, and time zone support will be
|
| 629 |
+
enabled by default, in Django 5.0.
|
| 630 |
+
|
| 631 |
+
Note that the default :file:`settings.py` file created by
|
| 632 |
+
:djadmin:`django-admin startproject <startproject>` includes
|
| 633 |
+
:setting:`USE_TZ = True <USE_TZ>` since Django 1.4.
|
| 634 |
+
|
| 635 |
+
You can set ``USE_TZ`` to ``False`` in your project settings before then to
|
| 636 |
+
opt-out.
|
| 637 |
+
|
| 638 |
+
.. _use_l10n_deprecation:
|
| 639 |
+
|
| 640 |
+
Localization
|
| 641 |
+
------------
|
| 642 |
+
|
| 643 |
+
In order to follow good practice, the default value of the ``USE_L10N`` setting
|
| 644 |
+
is changed from ``False`` to ``True``.
|
| 645 |
+
|
| 646 |
+
Moreover ``USE_L10N`` is deprecated as of this release. Starting with Django
|
| 647 |
+
5.0, by default, any date or number displayed by Django will be localized.
|
| 648 |
+
|
| 649 |
+
The :ttag:`{% localize %} <localize>` tag and the :tfilter:`localize`/
|
| 650 |
+
:tfilter:`unlocalize` filters will still be honored by Django.
|
| 651 |
+
|
| 652 |
+
Miscellaneous
|
| 653 |
+
-------------
|
| 654 |
+
|
| 655 |
+
* ``SERIALIZE`` test setting is deprecated as it can be inferred from the
|
| 656 |
+
:attr:`~django.test.TestCase.databases` with the
|
| 657 |
+
:ref:`serialized_rollback <test-case-serialized-rollback>` option enabled.
|
| 658 |
+
|
| 659 |
+
* The undocumented ``django.utils.baseconv`` module is deprecated.
|
| 660 |
+
|
| 661 |
+
* The undocumented ``django.utils.datetime_safe`` module is deprecated.
|
| 662 |
+
|
| 663 |
+
* The default sitemap protocol for sitemaps built outside the context of a
|
| 664 |
+
request will change from ``'http'`` to ``'https'`` in Django 5.0.
|
| 665 |
+
|
| 666 |
+
* The ``extra_tests`` argument for :meth:`.DiscoverRunner.build_suite` and
|
| 667 |
+
:meth:`.DiscoverRunner.run_tests` is deprecated.
|
| 668 |
+
|
| 669 |
+
* The :class:`~django.contrib.postgres.aggregates.ArrayAgg`,
|
| 670 |
+
:class:`~django.contrib.postgres.aggregates.JSONBAgg`, and
|
| 671 |
+
:class:`~django.contrib.postgres.aggregates.StringAgg` aggregates will return
|
| 672 |
+
``None`` when there are no rows instead of ``[]``, ``[]``, and ``''``
|
| 673 |
+
respectively in Django 5.0. If you need the previous behavior, explicitly set
|
| 674 |
+
``default`` to ``Value([])``, ``Value('[]')``, or ``Value('')``.
|
| 675 |
+
|
| 676 |
+
* The ``django.contrib.gis.admin.GeoModelAdmin`` and ``OSMGeoAdmin`` classes
|
| 677 |
+
are deprecated. Use :class:`~django.contrib.admin.ModelAdmin` and
|
| 678 |
+
:class:`~django.contrib.gis.admin.GISModelAdmin` instead.
|
| 679 |
+
|
| 680 |
+
* Since form rendering now uses the template engine, the undocumented
|
| 681 |
+
``BaseForm._html_output()`` helper method is deprecated.
|
| 682 |
+
|
| 683 |
+
* The ability to return a ``str`` from ``ErrorList`` and ``ErrorDict`` is
|
| 684 |
+
deprecated. It is expected these methods return a ``SafeString``.
|
| 685 |
+
|
| 686 |
+
Features removed in 4.0
|
| 687 |
+
=======================
|
| 688 |
+
|
| 689 |
+
These features have reached the end of their deprecation cycle and are removed
|
| 690 |
+
in Django 4.0.
|
| 691 |
+
|
| 692 |
+
See :ref:`deprecated-features-3.0` for details on these changes, including how
|
| 693 |
+
to remove usage of these features.
|
| 694 |
+
|
| 695 |
+
* ``django.utils.http.urlquote()``, ``urlquote_plus()``, ``urlunquote()``, and
|
| 696 |
+
``urlunquote_plus()`` are removed.
|
| 697 |
+
|
| 698 |
+
* ``django.utils.encoding.force_text()`` and ``smart_text()`` are removed.
|
| 699 |
+
|
| 700 |
+
* ``django.utils.translation.ugettext()``, ``ugettext_lazy()``,
|
| 701 |
+
``ugettext_noop()``, ``ungettext()``, and ``ungettext_lazy()`` are removed.
|
| 702 |
+
|
| 703 |
+
* ``django.views.i18n.set_language()`` doesn't set the user language in
|
| 704 |
+
``request.session`` (key ``_language``).
|
| 705 |
+
|
| 706 |
+
* ``alias=None`` is required in the signature of
|
| 707 |
+
``django.db.models.Expression.get_group_by_cols()`` subclasses.
|
| 708 |
+
|
| 709 |
+
* ``django.utils.text.unescape_entities()`` is removed.
|
| 710 |
+
|
| 711 |
+
* ``django.utils.http.is_safe_url()`` is removed.
|
| 712 |
+
|
| 713 |
+
See :ref:`deprecated-features-3.1` for details on these changes, including how
|
| 714 |
+
to remove usage of these features.
|
| 715 |
+
|
| 716 |
+
* The ``PASSWORD_RESET_TIMEOUT_DAYS`` setting is removed.
|
| 717 |
+
|
| 718 |
+
* The :lookup:`isnull` lookup no longer allows using non-boolean values as the
|
| 719 |
+
right-hand side.
|
| 720 |
+
|
| 721 |
+
* The ``django.db.models.query_utils.InvalidQuery`` exception class is removed.
|
| 722 |
+
|
| 723 |
+
* The ``django-admin.py`` entry point is removed.
|
| 724 |
+
|
| 725 |
+
* The ``HttpRequest.is_ajax()`` method is removed.
|
| 726 |
+
|
| 727 |
+
* Support for the pre-Django 3.1 encoding format of cookies values used by
|
| 728 |
+
``django.contrib.messages.storage.cookie.CookieStorage`` is removed.
|
| 729 |
+
|
| 730 |
+
* Support for the pre-Django 3.1 password reset tokens in the admin site (that
|
| 731 |
+
use the SHA-1 hashing algorithm) is removed.
|
| 732 |
+
|
| 733 |
+
* Support for the pre-Django 3.1 encoding format of sessions is removed.
|
| 734 |
+
|
| 735 |
+
* Support for the pre-Django 3.1 ``django.core.signing.Signer`` signatures
|
| 736 |
+
(encoded with the SHA-1 algorithm) is removed.
|
| 737 |
+
|
| 738 |
+
* Support for the pre-Django 3.1 ``django.core.signing.dumps()`` signatures
|
| 739 |
+
(encoded with the SHA-1 algorithm) in ``django.core.signing.loads()`` is
|
| 740 |
+
removed.
|
| 741 |
+
|
| 742 |
+
* Support for the pre-Django 3.1 user sessions (that use the SHA-1 algorithm)
|
| 743 |
+
is removed.
|
| 744 |
+
|
| 745 |
+
* The ``get_response`` argument for
|
| 746 |
+
``django.utils.deprecation.MiddlewareMixin.__init__()`` is required and
|
| 747 |
+
doesn't accept ``None``.
|
| 748 |
+
|
| 749 |
+
* The ``providing_args`` argument for ``django.dispatch.Signal`` is removed.
|
| 750 |
+
|
| 751 |
+
* The ``length`` argument for ``django.utils.crypto.get_random_string()`` is
|
| 752 |
+
required.
|
| 753 |
+
|
| 754 |
+
* The ``list`` message for ``ModelMultipleChoiceField`` is removed.
|
| 755 |
+
|
| 756 |
+
* Support for passing raw column aliases to ``QuerySet.order_by()`` is removed.
|
| 757 |
+
|
| 758 |
+
* The ``NullBooleanField`` model field is removed, except for support in
|
| 759 |
+
historical migrations.
|
| 760 |
+
|
| 761 |
+
* ``django.conf.urls.url()`` is removed.
|
| 762 |
+
|
| 763 |
+
* The ``django.contrib.postgres.fields.JSONField`` model field is removed,
|
| 764 |
+
except for support in historical migrations.
|
| 765 |
+
|
| 766 |
+
* ``django.contrib.postgres.fields.jsonb.KeyTransform`` and
|
| 767 |
+
``django.contrib.postgres.fields.jsonb.KeyTextTransform`` are removed.
|
| 768 |
+
|
| 769 |
+
* ``django.contrib.postgres.forms.JSONField`` is removed.
|
| 770 |
+
|
| 771 |
+
* The ``{% ifequal %}`` and ``{% ifnotequal %}`` template tags are removed.
|
| 772 |
+
|
| 773 |
+
* The ``DEFAULT_HASHING_ALGORITHM`` transitional setting is removed.
|
testbed/django__django/docs/releases/4.1.txt
ADDED
|
@@ -0,0 +1,753 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
========================
|
| 2 |
+
Django 4.1 release notes
|
| 3 |
+
========================
|
| 4 |
+
|
| 5 |
+
*August 3, 2022*
|
| 6 |
+
|
| 7 |
+
Welcome to Django 4.1!
|
| 8 |
+
|
| 9 |
+
These release notes cover the :ref:`new features <whats-new-4.1>`, as well as
|
| 10 |
+
some :ref:`backwards incompatible changes <backwards-incompatible-4.1>` you'll
|
| 11 |
+
want to be aware of when upgrading from Django 4.0 or earlier. We've
|
| 12 |
+
:ref:`begun the deprecation process for some features
|
| 13 |
+
<deprecated-features-4.1>`.
|
| 14 |
+
|
| 15 |
+
See the :doc:`/howto/upgrade-version` guide if you're updating an existing
|
| 16 |
+
project.
|
| 17 |
+
|
| 18 |
+
Python compatibility
|
| 19 |
+
====================
|
| 20 |
+
|
| 21 |
+
Django 4.1 supports Python 3.8, 3.9, 3.10, and 3.11 (as of 4.1.3). We
|
| 22 |
+
**highly recommend** and only officially support the latest release of each
|
| 23 |
+
series.
|
| 24 |
+
|
| 25 |
+
.. _whats-new-4.1:
|
| 26 |
+
|
| 27 |
+
What's new in Django 4.1
|
| 28 |
+
========================
|
| 29 |
+
|
| 30 |
+
Asynchronous handlers for class-based views
|
| 31 |
+
-------------------------------------------
|
| 32 |
+
|
| 33 |
+
View subclasses may now define async HTTP method handlers::
|
| 34 |
+
|
| 35 |
+
import asyncio
|
| 36 |
+
from django.http import HttpResponse
|
| 37 |
+
from django.views import View
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class AsyncView(View):
|
| 41 |
+
async def get(self, request, *args, **kwargs):
|
| 42 |
+
# Perform view logic using await.
|
| 43 |
+
await asyncio.sleep(1)
|
| 44 |
+
return HttpResponse("Hello async world!")
|
| 45 |
+
|
| 46 |
+
See :ref:`async-class-based-views` for more details.
|
| 47 |
+
|
| 48 |
+
Asynchronous ORM interface
|
| 49 |
+
--------------------------
|
| 50 |
+
|
| 51 |
+
``QuerySet`` now provides an asynchronous interface for all data access
|
| 52 |
+
operations. These are named as-per the existing synchronous operations but with
|
| 53 |
+
an ``a`` prefix, for example ``acreate()``, ``aget()``, and so on.
|
| 54 |
+
|
| 55 |
+
The new interface allows you to write asynchronous code without needing to wrap
|
| 56 |
+
ORM operations in ``sync_to_async()``::
|
| 57 |
+
|
| 58 |
+
async for author in Author.objects.filter(name__startswith="A"):
|
| 59 |
+
book = await author.books.afirst()
|
| 60 |
+
|
| 61 |
+
Note that, at this stage, the underlying database operations remain
|
| 62 |
+
synchronous, with contributions ongoing to push asynchronous support down into
|
| 63 |
+
the SQL compiler, and integrate asynchronous database drivers. The new
|
| 64 |
+
asynchronous queryset interface currently encapsulates the necessary
|
| 65 |
+
``sync_to_async()`` operations for you, and will allow your code to take
|
| 66 |
+
advantage of developments in the ORM's asynchronous support as it evolves.
|
| 67 |
+
|
| 68 |
+
See :ref:`async-queries` for details and limitations.
|
| 69 |
+
|
| 70 |
+
Validation of Constraints
|
| 71 |
+
-------------------------
|
| 72 |
+
|
| 73 |
+
:class:`Check <django.db.models.CheckConstraint>`,
|
| 74 |
+
:class:`unique <django.db.models.UniqueConstraint>`, and :class:`exclusion
|
| 75 |
+
<django.contrib.postgres.constraints.ExclusionConstraint>` constraints defined
|
| 76 |
+
in the :attr:`Meta.constraints <django.db.models.Options.constraints>` option
|
| 77 |
+
are now checked during :ref:`model validation <validating-objects>`.
|
| 78 |
+
|
| 79 |
+
Form rendering accessibility
|
| 80 |
+
----------------------------
|
| 81 |
+
|
| 82 |
+
In order to aid users with screen readers, and other assistive technology, new
|
| 83 |
+
``<div>`` based form templates are available from this release. These provide
|
| 84 |
+
more accessible navigation than the older templates, and are able to correctly
|
| 85 |
+
group related controls, such as radio-lists, into fieldsets.
|
| 86 |
+
|
| 87 |
+
The new templates are recommended, and will become the default form rendering
|
| 88 |
+
style when outputting a form, like ``{{ form }}`` in a template, from Django
|
| 89 |
+
5.0.
|
| 90 |
+
|
| 91 |
+
In order to ease adopting the new output style, the default form and formset
|
| 92 |
+
templates are now configurable at the project level via the
|
| 93 |
+
:setting:`FORM_RENDERER` setting.
|
| 94 |
+
|
| 95 |
+
See :ref:`the Forms section (below)<forms-4.1>` for full details.
|
| 96 |
+
|
| 97 |
+
.. _csrf-cookie-masked-usage:
|
| 98 |
+
|
| 99 |
+
``CSRF_COOKIE_MASKED`` setting
|
| 100 |
+
------------------------------
|
| 101 |
+
|
| 102 |
+
The new ``CSRF_COOKIE_MASKED`` transitional setting allows specifying whether
|
| 103 |
+
to mask the CSRF cookie.
|
| 104 |
+
|
| 105 |
+
:class:`~django.middleware.csrf.CsrfViewMiddleware` no longer masks the CSRF
|
| 106 |
+
cookie like it does the CSRF token in the DOM. If you are upgrading multiple
|
| 107 |
+
instances of the same project to Django 4.1, you should set
|
| 108 |
+
``CSRF_COOKIE_MASKED`` to ``True`` during the transition, in order to allow
|
| 109 |
+
compatibility with the older versions of Django. Once the transition to 4.1 is
|
| 110 |
+
complete you can stop overriding ``CSRF_COOKIE_MASKED``.
|
| 111 |
+
|
| 112 |
+
This setting is deprecated as of this release and will be removed in Django
|
| 113 |
+
5.0.
|
| 114 |
+
|
| 115 |
+
Minor features
|
| 116 |
+
--------------
|
| 117 |
+
|
| 118 |
+
:mod:`django.contrib.admin`
|
| 119 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 120 |
+
|
| 121 |
+
* The admin :ref:`dark mode CSS variables <admin-theming>` are now applied in a
|
| 122 |
+
separate stylesheet and template block.
|
| 123 |
+
|
| 124 |
+
* :ref:`modeladmin-list-filters` providing custom ``FieldListFilter``
|
| 125 |
+
subclasses can now control the query string value separator when filtering
|
| 126 |
+
for multiple values using the ``__in`` lookup.
|
| 127 |
+
|
| 128 |
+
* The admin :meth:`history view <django.contrib.admin.ModelAdmin.history_view>`
|
| 129 |
+
is now paginated.
|
| 130 |
+
|
| 131 |
+
* Related widget wrappers now have a link to object's change form.
|
| 132 |
+
|
| 133 |
+
* The :meth:`.AdminSite.get_app_list` method now allows changing the order of
|
| 134 |
+
apps and models on the admin index page.
|
| 135 |
+
|
| 136 |
+
:mod:`django.contrib.auth`
|
| 137 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 138 |
+
|
| 139 |
+
* The default iteration count for the PBKDF2 password hasher is increased from
|
| 140 |
+
320,000 to 390,000.
|
| 141 |
+
|
| 142 |
+
* The :meth:`.RemoteUserBackend.configure_user` method now allows synchronizing
|
| 143 |
+
user attributes with attributes in a remote system such as an LDAP directory.
|
| 144 |
+
|
| 145 |
+
:mod:`django.contrib.gis`
|
| 146 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 147 |
+
|
| 148 |
+
* The new :meth:`.GEOSGeometry.make_valid()` method allows converting invalid
|
| 149 |
+
geometries to valid ones.
|
| 150 |
+
|
| 151 |
+
* The new ``clone`` argument for :meth:`.GEOSGeometry.normalize` allows
|
| 152 |
+
creating a normalized clone of the geometry.
|
| 153 |
+
|
| 154 |
+
:mod:`django.contrib.postgres`
|
| 155 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 156 |
+
|
| 157 |
+
* The new :class:`BitXor() <django.contrib.postgres.aggregates.BitXor>`
|
| 158 |
+
aggregate function returns an ``int`` of the bitwise ``XOR`` of all non-null
|
| 159 |
+
input values.
|
| 160 |
+
|
| 161 |
+
* :class:`~django.contrib.postgres.indexes.SpGistIndex` now supports covering
|
| 162 |
+
indexes on PostgreSQL 14+.
|
| 163 |
+
|
| 164 |
+
* :class:`~django.contrib.postgres.constraints.ExclusionConstraint` now
|
| 165 |
+
supports covering exclusion constraints using SP-GiST indexes on PostgreSQL
|
| 166 |
+
14+.
|
| 167 |
+
|
| 168 |
+
* The new ``default_bounds`` attribute of :attr:`DateTimeRangeField
|
| 169 |
+
<django.contrib.postgres.fields.DateTimeRangeField.default_bounds>` and
|
| 170 |
+
:attr:`DecimalRangeField
|
| 171 |
+
<django.contrib.postgres.fields.DecimalRangeField.default_bounds>` allows
|
| 172 |
+
specifying bounds for list and tuple inputs.
|
| 173 |
+
|
| 174 |
+
* :class:`~django.contrib.postgres.constraints.ExclusionConstraint` now allows
|
| 175 |
+
specifying operator classes with the
|
| 176 |
+
:class:`OpClass() <django.contrib.postgres.indexes.OpClass>` expression.
|
| 177 |
+
|
| 178 |
+
:mod:`django.contrib.sitemaps`
|
| 179 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 180 |
+
|
| 181 |
+
* The default sitemap index template ``<sitemapindex>`` now includes the
|
| 182 |
+
``<lastmod>`` timestamp where available, through the new
|
| 183 |
+
:meth:`~django.contrib.sitemaps.Sitemap.get_latest_lastmod` method. Custom
|
| 184 |
+
sitemap index templates should be updated for the adjusted :ref:`context
|
| 185 |
+
variables <sitemap-index-context-variables>`.
|
| 186 |
+
|
| 187 |
+
:mod:`django.contrib.staticfiles`
|
| 188 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 189 |
+
|
| 190 |
+
* :class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` now
|
| 191 |
+
replaces paths to CSS source map references with their hashed counterparts.
|
| 192 |
+
|
| 193 |
+
Database backends
|
| 194 |
+
~~~~~~~~~~~~~~~~~
|
| 195 |
+
|
| 196 |
+
* Third-party database backends can now specify the minimum required version of
|
| 197 |
+
the database using the ``DatabaseFeatures.minimum_database_version``
|
| 198 |
+
attribute which is a tuple (e.g. ``(10, 0)`` means "10.0"). If a minimum
|
| 199 |
+
version is specified, backends must also implement
|
| 200 |
+
``DatabaseWrapper.get_database_version()``, which returns a tuple of the
|
| 201 |
+
current database version. The backend's
|
| 202 |
+
``DatabaseWrapper.init_connection_state()`` method must call ``super()`` in
|
| 203 |
+
order for the check to run.
|
| 204 |
+
|
| 205 |
+
.. _forms-4.1:
|
| 206 |
+
|
| 207 |
+
Forms
|
| 208 |
+
~~~~~
|
| 209 |
+
|
| 210 |
+
* The default template used to render forms when cast to a string, e.g. in
|
| 211 |
+
templates as ``{{ form }}``, is now configurable at the project-level by
|
| 212 |
+
setting :attr:`~django.forms.renderers.BaseRenderer.form_template_name` on
|
| 213 |
+
the class provided for :setting:`FORM_RENDERER`.
|
| 214 |
+
|
| 215 |
+
:attr:`.Form.template_name` is now a property deferring to the renderer, but
|
| 216 |
+
may be overridden with a string value to specify the template name per-form
|
| 217 |
+
class.
|
| 218 |
+
|
| 219 |
+
Similarly, the default template used to render formsets can be specified via
|
| 220 |
+
the matching
|
| 221 |
+
:attr:`~django.forms.renderers.BaseRenderer.formset_template_name` renderer
|
| 222 |
+
attribute.
|
| 223 |
+
|
| 224 |
+
* The new ``div.html`` form template, referencing
|
| 225 |
+
:attr:`.Form.template_name_div` attribute, and matching :meth:`.Form.as_div`
|
| 226 |
+
method, render forms using HTML ``<div>`` elements.
|
| 227 |
+
|
| 228 |
+
This new output style is recommended over the existing
|
| 229 |
+
:meth:`~.Form.as_table`, :meth:`~.Form.as_p` and :meth:`~.Form.as_ul` styles,
|
| 230 |
+
as the template implements ``<fieldset>`` and ``<legend>`` to group related
|
| 231 |
+
inputs and is easier for screen reader users to navigate.
|
| 232 |
+
|
| 233 |
+
The div-based output will become the default rendering style from Django 5.0.
|
| 234 |
+
|
| 235 |
+
* In order to smooth adoption of the new ``<div>`` output style, two
|
| 236 |
+
transitional form renderer classes are available:
|
| 237 |
+
``django.forms.renderers.DjangoDivFormRenderer`` and
|
| 238 |
+
``django.forms.renderers.Jinja2DivFormRenderer``, for the Django and Jinja2
|
| 239 |
+
template backends respectively.
|
| 240 |
+
|
| 241 |
+
You can apply one of these via the :setting:`FORM_RENDERER` setting. For
|
| 242 |
+
example::
|
| 243 |
+
|
| 244 |
+
FORM_RENDERER = "django.forms.renderers.DjangoDivFormRenderer"
|
| 245 |
+
|
| 246 |
+
Once the ``<div>`` output style is the default, from Django 5.0, these
|
| 247 |
+
transitional renderers will be deprecated, for removal in Django 6.0. The
|
| 248 |
+
``FORM_RENDERER`` declaration can be removed at that time.
|
| 249 |
+
|
| 250 |
+
* If the new ``<div>`` output style is not appropriate for your project, you should
|
| 251 |
+
define a renderer subclass specifying
|
| 252 |
+
:attr:`~django.forms.renderers.BaseRenderer.form_template_name` and
|
| 253 |
+
:attr:`~django.forms.renderers.BaseRenderer.formset_template_name` for your
|
| 254 |
+
required style, and set :setting:`FORM_RENDERER` accordingly.
|
| 255 |
+
|
| 256 |
+
For example, for the ``<p>`` output style used by :meth:`~.Form.as_p`, you
|
| 257 |
+
would define a form renderer setting ``form_template_name`` to
|
| 258 |
+
``"django/forms/p.html"`` and ``formset_template_name`` to
|
| 259 |
+
``"django/forms/formsets/p.html"``.
|
| 260 |
+
|
| 261 |
+
* The new :meth:`~django.forms.BoundField.legend_tag` allows rendering field
|
| 262 |
+
labels in ``<legend>`` tags via the new ``tag`` argument of
|
| 263 |
+
:meth:`~django.forms.BoundField.label_tag`.
|
| 264 |
+
|
| 265 |
+
* The new ``edit_only`` argument for :func:`.modelformset_factory` and
|
| 266 |
+
:func:`.inlineformset_factory` allows preventing new objects creation.
|
| 267 |
+
|
| 268 |
+
* The ``js`` and ``css`` class attributes of :doc:`Media </topics/forms/media>`
|
| 269 |
+
now allow using hashable objects, not only path strings, as long as those
|
| 270 |
+
objects implement the ``__html__()`` method (typically when decorated with
|
| 271 |
+
the :func:`~django.utils.html.html_safe` decorator).
|
| 272 |
+
|
| 273 |
+
* The new :attr:`.BoundField.use_fieldset` and :attr:`.Widget.use_fieldset`
|
| 274 |
+
attributes help to identify widgets where its inputs should be grouped in a
|
| 275 |
+
``<fieldset>`` with a ``<legend>``.
|
| 276 |
+
|
| 277 |
+
* The :ref:`formsets-error-messages` argument for
|
| 278 |
+
:class:`~django.forms.formsets.BaseFormSet` now allows customizing
|
| 279 |
+
error messages for invalid number of forms by passing ``'too_few_forms'``
|
| 280 |
+
and ``'too_many_forms'`` keys.
|
| 281 |
+
|
| 282 |
+
* :class:`~django.forms.IntegerField`, :class:`~django.forms.FloatField`, and
|
| 283 |
+
:class:`~django.forms.DecimalField` now optionally accept a ``step_size``
|
| 284 |
+
argument. This is used to set the ``step`` HTML attribute, and is validated
|
| 285 |
+
on form submission.
|
| 286 |
+
|
| 287 |
+
Internationalization
|
| 288 |
+
~~~~~~~~~~~~~~~~~~~~
|
| 289 |
+
|
| 290 |
+
* The :func:`~django.conf.urls.i18n.i18n_patterns` function now supports
|
| 291 |
+
languages with both scripts and regions.
|
| 292 |
+
|
| 293 |
+
Management Commands
|
| 294 |
+
~~~~~~~~~~~~~~~~~~~
|
| 295 |
+
|
| 296 |
+
* :option:`makemigrations --no-input` now logs default answers and reasons why
|
| 297 |
+
migrations cannot be created.
|
| 298 |
+
|
| 299 |
+
* The new :option:`makemigrations --scriptable` option diverts log output and
|
| 300 |
+
input prompts to ``stderr``, writing only paths of generated migration files
|
| 301 |
+
to ``stdout``.
|
| 302 |
+
|
| 303 |
+
* The new :option:`migrate --prune` option allows deleting nonexistent
|
| 304 |
+
migrations from the ``django_migrations`` table.
|
| 305 |
+
|
| 306 |
+
* Python files created by :djadmin:`startproject`, :djadmin:`startapp`,
|
| 307 |
+
:djadmin:`optimizemigration`, :djadmin:`makemigrations`, and
|
| 308 |
+
:djadmin:`squashmigrations` are now formatted using the ``black`` command if
|
| 309 |
+
it is present on your ``PATH``.
|
| 310 |
+
|
| 311 |
+
* The new :djadmin:`optimizemigration` command allows optimizing operations for
|
| 312 |
+
a migration.
|
| 313 |
+
|
| 314 |
+
Migrations
|
| 315 |
+
~~~~~~~~~~
|
| 316 |
+
|
| 317 |
+
* The new :class:`~django.db.migrations.operations.RenameIndex` operation
|
| 318 |
+
allows renaming indexes defined in the
|
| 319 |
+
:attr:`Meta.indexes <django.db.models.Options.indexes>` or
|
| 320 |
+
:attr:`~django.db.models.Options.index_together` options.
|
| 321 |
+
|
| 322 |
+
* The migrations autodetector now generates
|
| 323 |
+
:class:`~django.db.migrations.operations.RenameIndex` operations instead of
|
| 324 |
+
``RemoveIndex`` and ``AddIndex``, when renaming indexes defined in the
|
| 325 |
+
:attr:`Meta.indexes <django.db.models.Options.indexes>`.
|
| 326 |
+
|
| 327 |
+
* The migrations autodetector now generates
|
| 328 |
+
:class:`~django.db.migrations.operations.RenameIndex` operations instead of
|
| 329 |
+
``AlterIndexTogether`` and ``AddIndex``, when moving indexes defined in the
|
| 330 |
+
:attr:`Meta.index_together <django.db.models.Options.index_together>` to the
|
| 331 |
+
:attr:`Meta.indexes <django.db.models.Options.indexes>`.
|
| 332 |
+
|
| 333 |
+
Models
|
| 334 |
+
~~~~~~
|
| 335 |
+
|
| 336 |
+
* The ``order_by`` argument of the
|
| 337 |
+
:class:`~django.db.models.expressions.Window` expression now accepts string
|
| 338 |
+
references to fields and transforms.
|
| 339 |
+
|
| 340 |
+
* The new :setting:`CONN_HEALTH_CHECKS` setting allows enabling health checks
|
| 341 |
+
for :ref:`persistent database connections <persistent-database-connections>`
|
| 342 |
+
in order to reduce the number of failed requests, e.g. after database server
|
| 343 |
+
restart.
|
| 344 |
+
|
| 345 |
+
* :meth:`.QuerySet.bulk_create` now supports updating fields when a row
|
| 346 |
+
insertion fails uniqueness constraints. This is supported on MariaDB, MySQL,
|
| 347 |
+
PostgreSQL, and SQLite 3.24+.
|
| 348 |
+
|
| 349 |
+
* :meth:`.QuerySet.iterator` now supports prefetching related objects as long
|
| 350 |
+
as the ``chunk_size`` argument is provided. In older versions, no prefetching
|
| 351 |
+
was done.
|
| 352 |
+
|
| 353 |
+
* :class:`~django.db.models.Q` objects and querysets can now be combined using
|
| 354 |
+
``^`` as the exclusive or (``XOR``) operator. ``XOR`` is natively supported
|
| 355 |
+
on MariaDB and MySQL. For databases that do not support ``XOR``, the query
|
| 356 |
+
will be converted to an equivalent using ``AND``, ``OR``, and ``NOT``.
|
| 357 |
+
|
| 358 |
+
* The new :ref:`Field.non_db_attrs <custom-field-non_db_attrs>` attribute
|
| 359 |
+
allows customizing attributes of fields that don't affect a column
|
| 360 |
+
definition.
|
| 361 |
+
|
| 362 |
+
* On PostgreSQL, ``AutoField``, ``BigAutoField``, and ``SmallAutoField`` are
|
| 363 |
+
now created as identity columns rather than serial columns with sequences.
|
| 364 |
+
|
| 365 |
+
Requests and Responses
|
| 366 |
+
~~~~~~~~~~~~~~~~~~~~~~
|
| 367 |
+
|
| 368 |
+
* :meth:`.HttpResponse.set_cookie` now supports :class:`~datetime.timedelta`
|
| 369 |
+
objects for the ``max_age`` argument.
|
| 370 |
+
|
| 371 |
+
Security
|
| 372 |
+
~~~~~~~~
|
| 373 |
+
|
| 374 |
+
* The new :setting:`SECRET_KEY_FALLBACKS` setting allows providing a list of
|
| 375 |
+
values for secret key rotation.
|
| 376 |
+
|
| 377 |
+
* The :setting:`SECURE_PROXY_SSL_HEADER` setting now supports a comma-separated
|
| 378 |
+
list of protocols in the header value.
|
| 379 |
+
|
| 380 |
+
Signals
|
| 381 |
+
~~~~~~~
|
| 382 |
+
|
| 383 |
+
* The :data:`~django.db.models.signals.pre_delete` and
|
| 384 |
+
:data:`~django.db.models.signals.post_delete` signals now dispatch the
|
| 385 |
+
``origin`` of the deletion.
|
| 386 |
+
|
| 387 |
+
.. _templates-4.1:
|
| 388 |
+
|
| 389 |
+
Templates
|
| 390 |
+
~~~~~~~~~
|
| 391 |
+
|
| 392 |
+
* The HTML ``<script>`` element ``id`` attribute is no longer required when
|
| 393 |
+
wrapping the :tfilter:`json_script` template filter.
|
| 394 |
+
|
| 395 |
+
* The :class:`cached template loader <django.template.loaders.cached.Loader>`
|
| 396 |
+
is now enabled in development, when :setting:`DEBUG` is ``True``, and
|
| 397 |
+
:setting:`OPTIONS['loaders'] <TEMPLATES-OPTIONS>` isn't specified. You may
|
| 398 |
+
specify ``OPTIONS['loaders']`` to override this, if necessary.
|
| 399 |
+
|
| 400 |
+
Tests
|
| 401 |
+
~~~~~
|
| 402 |
+
|
| 403 |
+
* The :class:`.DiscoverRunner` now supports running tests in parallel on
|
| 404 |
+
macOS, Windows, and any other systems where the default
|
| 405 |
+
:mod:`multiprocessing` start method is ``spawn``.
|
| 406 |
+
|
| 407 |
+
* A nested atomic block marked as durable in :class:`django.test.TestCase` now
|
| 408 |
+
raises a ``RuntimeError``, the same as outside of tests.
|
| 409 |
+
|
| 410 |
+
* :meth:`.SimpleTestCase.assertFormError` and
|
| 411 |
+
:meth:`assertFormsetError() <django.test.SimpleTestCase.assertFormSetError>`
|
| 412 |
+
now support passing a form/formset object directly.
|
| 413 |
+
|
| 414 |
+
URLs
|
| 415 |
+
~~~~
|
| 416 |
+
|
| 417 |
+
* The new :attr:`.ResolverMatch.captured_kwargs` attribute stores the captured
|
| 418 |
+
keyword arguments, as parsed from the URL.
|
| 419 |
+
|
| 420 |
+
* The new :attr:`.ResolverMatch.extra_kwargs` attribute stores the additional
|
| 421 |
+
keyword arguments passed to the view function.
|
| 422 |
+
|
| 423 |
+
Utilities
|
| 424 |
+
~~~~~~~~~
|
| 425 |
+
|
| 426 |
+
* ``SimpleLazyObject`` now supports addition operations.
|
| 427 |
+
|
| 428 |
+
* :func:`~django.utils.safestring.mark_safe` now preserves lazy objects.
|
| 429 |
+
|
| 430 |
+
Validators
|
| 431 |
+
~~~~~~~~~~
|
| 432 |
+
|
| 433 |
+
* The new :class:`~django.core.validators.StepValueValidator` checks if a value
|
| 434 |
+
is an integral multiple of a given step size. This new validator is used for
|
| 435 |
+
the new ``step_size`` argument added to form fields representing numeric
|
| 436 |
+
values.
|
| 437 |
+
|
| 438 |
+
.. _backwards-incompatible-4.1:
|
| 439 |
+
|
| 440 |
+
Backwards incompatible changes in 4.1
|
| 441 |
+
=====================================
|
| 442 |
+
|
| 443 |
+
Database backend API
|
| 444 |
+
--------------------
|
| 445 |
+
|
| 446 |
+
This section describes changes that may be needed in third-party database
|
| 447 |
+
backends.
|
| 448 |
+
|
| 449 |
+
* ``BaseDatabaseFeatures.has_case_insensitive_like`` is changed from ``True``
|
| 450 |
+
to ``False`` to reflect the behavior of most databases.
|
| 451 |
+
|
| 452 |
+
* ``DatabaseIntrospection.get_key_columns()`` is removed. Use
|
| 453 |
+
``DatabaseIntrospection.get_relations()`` instead.
|
| 454 |
+
|
| 455 |
+
* ``DatabaseOperations.ignore_conflicts_suffix_sql()`` method is replaced by
|
| 456 |
+
``DatabaseOperations.on_conflict_suffix_sql()`` that accepts the ``fields``,
|
| 457 |
+
``on_conflict``, ``update_fields``, and ``unique_fields`` arguments.
|
| 458 |
+
|
| 459 |
+
* The ``ignore_conflicts`` argument of the
|
| 460 |
+
``DatabaseOperations.insert_statement()`` method is replaced by
|
| 461 |
+
``on_conflict`` that accepts ``django.db.models.constants.OnConflict``.
|
| 462 |
+
|
| 463 |
+
* ``DatabaseOperations._convert_field_to_tz()`` is replaced by
|
| 464 |
+
``DatabaseOperations._convert_sql_to_tz()`` that accepts the ``sql``,
|
| 465 |
+
``params``, and ``tzname`` arguments.
|
| 466 |
+
|
| 467 |
+
* Several date and time methods on ``DatabaseOperations`` now take ``sql`` and
|
| 468 |
+
``params`` arguments instead of ``field_name`` and return 2-tuple containing
|
| 469 |
+
some SQL and the parameters to be interpolated into that SQL. The changed
|
| 470 |
+
methods have these new signatures:
|
| 471 |
+
|
| 472 |
+
* ``DatabaseOperations.date_extract_sql(lookup_type, sql, params)``
|
| 473 |
+
* ``DatabaseOperations.datetime_extract_sql(lookup_type, sql, params, tzname)``
|
| 474 |
+
* ``DatabaseOperations.time_extract_sql(lookup_type, sql, params)``
|
| 475 |
+
* ``DatabaseOperations.date_trunc_sql(lookup_type, sql, params, tzname=None)``
|
| 476 |
+
* ``DatabaseOperations.datetime_trunc_sql(self, lookup_type, sql, params, tzname)``
|
| 477 |
+
* ``DatabaseOperations.time_trunc_sql(lookup_type, sql, params, tzname=None)``
|
| 478 |
+
* ``DatabaseOperations.datetime_cast_date_sql(sql, params, tzname)``
|
| 479 |
+
* ``DatabaseOperations.datetime_cast_time_sql(sql, params, tzname)``
|
| 480 |
+
|
| 481 |
+
:mod:`django.contrib.gis`
|
| 482 |
+
-------------------------
|
| 483 |
+
|
| 484 |
+
* Support for GDAL 2.1 is removed.
|
| 485 |
+
|
| 486 |
+
* Support for PostGIS 2.4 is removed.
|
| 487 |
+
|
| 488 |
+
Dropped support for PostgreSQL 10
|
| 489 |
+
---------------------------------
|
| 490 |
+
|
| 491 |
+
Upstream support for PostgreSQL 10 ends in November 2022. Django 4.1 supports
|
| 492 |
+
PostgreSQL 11 and higher.
|
| 493 |
+
|
| 494 |
+
Dropped support for MariaDB 10.2
|
| 495 |
+
--------------------------------
|
| 496 |
+
|
| 497 |
+
Upstream support for MariaDB 10.2 ends in May 2022. Django 4.1 supports MariaDB
|
| 498 |
+
10.3 and higher.
|
| 499 |
+
|
| 500 |
+
Admin changelist searches spanning multi-valued relationships changes
|
| 501 |
+
---------------------------------------------------------------------
|
| 502 |
+
|
| 503 |
+
Admin changelist searches using multiple search terms are now applied in a
|
| 504 |
+
single call to ``filter()``, rather than in sequential ``filter()`` calls.
|
| 505 |
+
|
| 506 |
+
For multi-valued relationships, this means that rows from the related model
|
| 507 |
+
must match all terms rather than any term. For example, if ``search_fields``
|
| 508 |
+
is set to ``['child__name', 'child__age']``, and a user searches for
|
| 509 |
+
``'Jamal 17'``, parent rows will be returned only if there is a relationship to
|
| 510 |
+
some 17-year-old child named Jamal, rather than also returning parents who
|
| 511 |
+
merely have a younger or older child named Jamal in addition to some other
|
| 512 |
+
17-year-old.
|
| 513 |
+
|
| 514 |
+
See the :ref:`spanning-multi-valued-relationships` topic for more discussion of
|
| 515 |
+
this difference. In Django 4.0 and earlier,
|
| 516 |
+
:meth:`~django.contrib.admin.ModelAdmin.get_search_results` followed the
|
| 517 |
+
second example query, but this undocumented behavior led to queries with
|
| 518 |
+
excessive joins.
|
| 519 |
+
|
| 520 |
+
Reverse foreign key changes for unsaved model instances
|
| 521 |
+
-------------------------------------------------------
|
| 522 |
+
|
| 523 |
+
In order to unify the behavior with many-to-many relations for unsaved model
|
| 524 |
+
instances, a reverse foreign key now raises ``ValueError`` when calling
|
| 525 |
+
:class:`related managers <django.db.models.fields.related.RelatedManager>` for
|
| 526 |
+
unsaved objects.
|
| 527 |
+
|
| 528 |
+
Miscellaneous
|
| 529 |
+
-------------
|
| 530 |
+
|
| 531 |
+
* Related managers for :class:`~django.db.models.ForeignKey`,
|
| 532 |
+
:class:`~django.db.models.ManyToManyField`, and
|
| 533 |
+
:class:`~django.contrib.contenttypes.fields.GenericRelation` are now cached
|
| 534 |
+
on the :class:`~django.db.models.Model` instance to which they belong. *This
|
| 535 |
+
change was reverted in Django 4.1.2.*
|
| 536 |
+
|
| 537 |
+
* The Django test runner now returns a non-zero error code for unexpected
|
| 538 |
+
successes from tests marked with :py:func:`unittest.expectedFailure`.
|
| 539 |
+
|
| 540 |
+
* :class:`~django.middleware.csrf.CsrfViewMiddleware` no longer masks the CSRF
|
| 541 |
+
cookie like it does the CSRF token in the DOM.
|
| 542 |
+
|
| 543 |
+
* :class:`~django.middleware.csrf.CsrfViewMiddleware` now uses
|
| 544 |
+
``request.META['CSRF_COOKIE']`` for storing the unmasked CSRF secret rather
|
| 545 |
+
than a masked version. This is an undocumented, private API.
|
| 546 |
+
|
| 547 |
+
* The :attr:`.ModelAdmin.actions` and
|
| 548 |
+
:attr:`~django.contrib.admin.ModelAdmin.inlines` attributes now default to an
|
| 549 |
+
empty tuple rather than an empty list to discourage unintended mutation.
|
| 550 |
+
|
| 551 |
+
* The ``type="text/css"`` attribute is no longer included in ``<link>`` tags
|
| 552 |
+
for CSS :doc:`form media </topics/forms/media>`.
|
| 553 |
+
|
| 554 |
+
* ``formset:added`` and ``formset:removed`` JavaScript events are now pure
|
| 555 |
+
JavaScript events and don't depend on jQuery. See
|
| 556 |
+
:ref:`admin-javascript-inline-form-events` for more details on the change.
|
| 557 |
+
|
| 558 |
+
* The ``exc_info`` argument of the undocumented
|
| 559 |
+
``django.utils.log.log_response()`` function is replaced by ``exception``.
|
| 560 |
+
|
| 561 |
+
* The ``size`` argument of the undocumented
|
| 562 |
+
``django.views.static.was_modified_since()`` function is removed.
|
| 563 |
+
|
| 564 |
+
* The admin log out UI now uses ``POST`` requests.
|
| 565 |
+
|
| 566 |
+
* The undocumented ``InlineAdminFormSet.non_form_errors`` property is replaced
|
| 567 |
+
by the ``non_form_errors()`` method. This is consistent with ``BaseFormSet``.
|
| 568 |
+
|
| 569 |
+
* As per :ref:`above<templates-4.1>`, the cached template loader is now
|
| 570 |
+
enabled in development. You may specify ``OPTIONS['loaders']`` to override
|
| 571 |
+
this, if necessary.
|
| 572 |
+
|
| 573 |
+
* The undocumented ``django.contrib.auth.views.SuccessURLAllowedHostsMixin``
|
| 574 |
+
mixin is replaced by ``RedirectURLMixin``.
|
| 575 |
+
|
| 576 |
+
* :class:`~django.db.models.BaseConstraint` subclasses must implement
|
| 577 |
+
:meth:`~django.db.models.BaseConstraint.validate` method to allow those
|
| 578 |
+
constraints to be used for validation.
|
| 579 |
+
|
| 580 |
+
* The undocumented ``URLResolver._is_callback()``,
|
| 581 |
+
``URLResolver._callback_strs``, and ``URLPattern.lookup_str()`` are
|
| 582 |
+
moved to ``django.contrib.admindocs.utils``.
|
| 583 |
+
|
| 584 |
+
* The :meth:`.Model.full_clean` method now converts an ``exclude`` value to a
|
| 585 |
+
``set``. It’s also preferable to pass an ``exclude`` value as a ``set`` to
|
| 586 |
+
the :meth:`.Model.clean_fields`, :meth:`.Model.full_clean`,
|
| 587 |
+
:meth:`.Model.validate_unique`, and :meth:`.Model.validate_constraints`
|
| 588 |
+
methods.
|
| 589 |
+
|
| 590 |
+
* The minimum supported version of ``asgiref`` is increased from 3.4.1 to
|
| 591 |
+
3.5.2.
|
| 592 |
+
|
| 593 |
+
* Combined expressions no longer use the error-prone behavior of guessing
|
| 594 |
+
``output_field`` when argument types match. As a consequence, resolving an
|
| 595 |
+
``output_field`` for database functions and combined expressions may now
|
| 596 |
+
crash with mixed types. You will need to explicitly set the ``output_field``
|
| 597 |
+
in such cases.
|
| 598 |
+
|
| 599 |
+
* The :djadmin:`makemessages` command no longer changes ``.po`` files when up
|
| 600 |
+
to date. In older versions, ``POT-Creation-Date`` was always updated.
|
| 601 |
+
|
| 602 |
+
.. _deprecated-features-4.1:
|
| 603 |
+
|
| 604 |
+
Features deprecated in 4.1
|
| 605 |
+
==========================
|
| 606 |
+
|
| 607 |
+
Log out via GET
|
| 608 |
+
---------------
|
| 609 |
+
|
| 610 |
+
Logging out via ``GET`` requests to the :py:class:`built-in logout view
|
| 611 |
+
<django.contrib.auth.views.LogoutView>` is deprecated. Use ``POST`` requests
|
| 612 |
+
instead.
|
| 613 |
+
|
| 614 |
+
If you want to retain the user experience of an HTML link, you can use a form
|
| 615 |
+
that is styled to appear as a link:
|
| 616 |
+
|
| 617 |
+
.. code-block:: html
|
| 618 |
+
|
| 619 |
+
<form id="logout-form" method="post" action="{% url 'admin:logout' %}">
|
| 620 |
+
{% csrf_token %}
|
| 621 |
+
<button type="submit">{% translate "Log out" %}</button>
|
| 622 |
+
</form>
|
| 623 |
+
|
| 624 |
+
.. code-block:: css
|
| 625 |
+
|
| 626 |
+
#logout-form {
|
| 627 |
+
display: inline;
|
| 628 |
+
}
|
| 629 |
+
#logout-form button {
|
| 630 |
+
background: none;
|
| 631 |
+
border: none;
|
| 632 |
+
cursor: pointer;
|
| 633 |
+
padding: 0;
|
| 634 |
+
text-decoration: underline;
|
| 635 |
+
}
|
| 636 |
+
|
| 637 |
+
Miscellaneous
|
| 638 |
+
-------------
|
| 639 |
+
|
| 640 |
+
* The context for sitemap index templates of a flat list of URLs is deprecated.
|
| 641 |
+
Custom sitemap index templates should be updated for the adjusted
|
| 642 |
+
:ref:`context variables <sitemap-index-context-variables>`, expecting a list
|
| 643 |
+
of objects with ``location`` and optional ``lastmod`` attributes.
|
| 644 |
+
|
| 645 |
+
* ``CSRF_COOKIE_MASKED`` transitional setting is deprecated.
|
| 646 |
+
|
| 647 |
+
* The ``name`` argument of :func:`django.utils.functional.cached_property` is
|
| 648 |
+
deprecated as it's unnecessary as of Python 3.6.
|
| 649 |
+
|
| 650 |
+
* The ``opclasses`` argument of
|
| 651 |
+
``django.contrib.postgres.constraints.ExclusionConstraint`` is deprecated in
|
| 652 |
+
favor of using :class:`OpClass() <django.contrib.postgres.indexes.OpClass>`
|
| 653 |
+
in :attr:`.ExclusionConstraint.expressions`. To use it, you need to add
|
| 654 |
+
``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`.
|
| 655 |
+
|
| 656 |
+
After making this change, :djadmin:`makemigrations` will generate a new
|
| 657 |
+
migration with two operations: ``RemoveConstraint`` and ``AddConstraint``.
|
| 658 |
+
Since this change has no effect on the database schema,
|
| 659 |
+
the :class:`~django.db.migrations.operations.SeparateDatabaseAndState`
|
| 660 |
+
operation can be used to only update the migration state without running any
|
| 661 |
+
SQL. Move the generated operations into the ``state_operations`` argument of
|
| 662 |
+
:class:`~django.db.migrations.operations.SeparateDatabaseAndState`. For
|
| 663 |
+
example::
|
| 664 |
+
|
| 665 |
+
class Migration(migrations.Migration):
|
| 666 |
+
...
|
| 667 |
+
|
| 668 |
+
operations = [
|
| 669 |
+
migrations.SeparateDatabaseAndState(
|
| 670 |
+
database_operations=[],
|
| 671 |
+
state_operations=[
|
| 672 |
+
migrations.RemoveConstraint(...),
|
| 673 |
+
migrations.AddConstraint(...),
|
| 674 |
+
],
|
| 675 |
+
),
|
| 676 |
+
]
|
| 677 |
+
|
| 678 |
+
* The undocumented ability to pass ``errors=None`` to
|
| 679 |
+
:meth:`.SimpleTestCase.assertFormError` and
|
| 680 |
+
:meth:`assertFormsetError() <django.test.SimpleTestCase.assertFormSetError>`
|
| 681 |
+
is deprecated. Use ``errors=[]`` instead.
|
| 682 |
+
|
| 683 |
+
* ``django.contrib.sessions.serializers.PickleSerializer`` is deprecated due to
|
| 684 |
+
the risk of remote code execution.
|
| 685 |
+
|
| 686 |
+
* The usage of ``QuerySet.iterator()`` on a queryset that prefetches related
|
| 687 |
+
objects without providing the ``chunk_size`` argument is deprecated. In older
|
| 688 |
+
versions, no prefetching was done. Providing a value for ``chunk_size``
|
| 689 |
+
signifies that the additional query per chunk needed to prefetch is desired.
|
| 690 |
+
|
| 691 |
+
* Passing unsaved model instances to related filters is deprecated. In Django
|
| 692 |
+
5.0, the exception will be raised.
|
| 693 |
+
|
| 694 |
+
* ``created=True`` is added to the signature of
|
| 695 |
+
:meth:`.RemoteUserBackend.configure_user`. Support for ``RemoteUserBackend``
|
| 696 |
+
subclasses that do not accept this argument is deprecated.
|
| 697 |
+
|
| 698 |
+
* The ``django.utils.timezone.utc`` alias to :attr:`datetime.timezone.utc` is
|
| 699 |
+
deprecated. Use :attr:`datetime.timezone.utc` directly.
|
| 700 |
+
|
| 701 |
+
* Passing a response object and a form/formset name to
|
| 702 |
+
``SimpleTestCase.assertFormError()`` and ``assertFormsetError()`` is
|
| 703 |
+
deprecated. Use::
|
| 704 |
+
|
| 705 |
+
assertFormError(response.context["form_name"], ...)
|
| 706 |
+
assertFormsetError(response.context["formset_name"], ...)
|
| 707 |
+
|
| 708 |
+
or pass the form/formset object directly instead.
|
| 709 |
+
|
| 710 |
+
* The undocumented ``django.contrib.gis.admin.OpenLayersWidget`` is deprecated.
|
| 711 |
+
|
| 712 |
+
* ``django.contrib.auth.hashers.CryptPasswordHasher`` is deprecated.
|
| 713 |
+
|
| 714 |
+
* The ability to pass ``nulls_first=False`` or ``nulls_last=False`` to
|
| 715 |
+
``Expression.asc()`` and ``Expression.desc()`` methods, and the ``OrderBy``
|
| 716 |
+
expression is deprecated. Use ``None`` instead.
|
| 717 |
+
|
| 718 |
+
* The ``"django/forms/default.html"`` and
|
| 719 |
+
``"django/forms/formsets/default.html"`` templates which are a proxy to the
|
| 720 |
+
table-based templates are deprecated. Use the specific template instead.
|
| 721 |
+
|
| 722 |
+
* The undocumented ``LogoutView.get_next_page()`` method is renamed to
|
| 723 |
+
``get_success_url()``.
|
| 724 |
+
|
| 725 |
+
Features removed in 4.1
|
| 726 |
+
=======================
|
| 727 |
+
|
| 728 |
+
These features have reached the end of their deprecation cycle and are removed
|
| 729 |
+
in Django 4.1.
|
| 730 |
+
|
| 731 |
+
See :ref:`deprecated-features-3.2` for details on these changes, including how
|
| 732 |
+
to remove usage of these features.
|
| 733 |
+
|
| 734 |
+
* Support for assigning objects which don't support creating deep copies with
|
| 735 |
+
``copy.deepcopy()`` to class attributes in ``TestCase.setUpTestData()`` is
|
| 736 |
+
removed.
|
| 737 |
+
|
| 738 |
+
* Support for using a boolean value in
|
| 739 |
+
:attr:`.BaseCommand.requires_system_checks` is removed.
|
| 740 |
+
|
| 741 |
+
* The ``whitelist`` argument and ``domain_whitelist`` attribute of
|
| 742 |
+
``django.core.validators.EmailValidator`` are removed.
|
| 743 |
+
|
| 744 |
+
* The ``default_app_config`` application configuration variable is removed.
|
| 745 |
+
|
| 746 |
+
* ``TransactionTestCase.assertQuerysetEqual()`` no longer calls ``repr()`` on a
|
| 747 |
+
queryset when compared to string values.
|
| 748 |
+
|
| 749 |
+
* The ``django.core.cache.backends.memcached.MemcachedCache`` backend is
|
| 750 |
+
removed.
|
| 751 |
+
|
| 752 |
+
* Support for the pre-Django 3.2 format of messages used by
|
| 753 |
+
``django.contrib.messages.storage.cookie.CookieStorage`` is removed.
|
testbed/django__django/docs/releases/4.2.4.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==========================
|
| 2 |
+
Django 4.2.4 release notes
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
*Expected August 1, 2023*
|
| 6 |
+
|
| 7 |
+
Django 4.2.4 fixes several bugs in 4.2.3.
|
| 8 |
+
|
| 9 |
+
Bugfixes
|
| 10 |
+
========
|
| 11 |
+
|
| 12 |
+
* ...
|
testbed/django__django/docs/releases/4.2.txt
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
========================
|
| 2 |
+
Django 4.2 release notes
|
| 3 |
+
========================
|
| 4 |
+
|
| 5 |
+
*April 3, 2023*
|
| 6 |
+
|
| 7 |
+
Welcome to Django 4.2!
|
| 8 |
+
|
| 9 |
+
These release notes cover the :ref:`new features <whats-new-4.2>`, as well as
|
| 10 |
+
some :ref:`backwards incompatible changes <backwards-incompatible-4.2>` you'll
|
| 11 |
+
want to be aware of when upgrading from Django 4.1 or earlier. We've
|
| 12 |
+
:ref:`begun the deprecation process for some features
|
| 13 |
+
<deprecated-features-4.2>`.
|
| 14 |
+
|
| 15 |
+
See the :doc:`/howto/upgrade-version` guide if you're updating an existing
|
| 16 |
+
project.
|
| 17 |
+
|
| 18 |
+
Django 4.2 is designated as a :term:`long-term support release
|
| 19 |
+
<Long-term support release>`. It will receive security updates for at least
|
| 20 |
+
three years after its release. Support for the previous LTS, Django 3.2, will
|
| 21 |
+
end in April 2024.
|
| 22 |
+
|
| 23 |
+
Python compatibility
|
| 24 |
+
====================
|
| 25 |
+
|
| 26 |
+
Django 4.2 supports Python 3.8, 3.9, 3.10, and 3.11. We **highly recommend**
|
| 27 |
+
and only officially support the latest release of each series.
|
| 28 |
+
|
| 29 |
+
.. _whats-new-4.2:
|
| 30 |
+
|
| 31 |
+
What's new in Django 4.2
|
| 32 |
+
========================
|
| 33 |
+
|
| 34 |
+
Psycopg 3 support
|
| 35 |
+
-----------------
|
| 36 |
+
|
| 37 |
+
Django now supports `psycopg`_ version 3.1.8 or higher. To update your code,
|
| 38 |
+
install the :pypi:`psycopg library <psycopg>`, you don't need to change the
|
| 39 |
+
:setting:`ENGINE <DATABASE-ENGINE>` as ``django.db.backends.postgresql``
|
| 40 |
+
supports both libraries.
|
| 41 |
+
|
| 42 |
+
Support for ``psycopg2`` is likely to be deprecated and removed at some point
|
| 43 |
+
in the future.
|
| 44 |
+
|
| 45 |
+
Be aware that ``psycopg`` 3 introduces some breaking changes over ``psycopg2``.
|
| 46 |
+
As a consequence, you may need to make some changes to account for
|
| 47 |
+
`differences from psycopg2`_.
|
| 48 |
+
|
| 49 |
+
.. _psycopg: https://www.psycopg.org/psycopg3/
|
| 50 |
+
.. _differences from psycopg2: https://www.psycopg.org/psycopg3/docs/basic/from_pg2.html
|
| 51 |
+
|
| 52 |
+
Comments on columns and tables
|
| 53 |
+
------------------------------
|
| 54 |
+
|
| 55 |
+
The new :attr:`Field.db_comment <django.db.models.Field.db_comment>` and
|
| 56 |
+
:attr:`Meta.db_table_comment <django.db.models.Options.db_table_comment>`
|
| 57 |
+
options allow creating comments on columns and tables, respectively. For
|
| 58 |
+
example::
|
| 59 |
+
|
| 60 |
+
from django.db import models
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class Question(models.Model):
|
| 64 |
+
text = models.TextField(db_comment="Poll question")
|
| 65 |
+
pub_date = models.DateTimeField(
|
| 66 |
+
db_comment="Date and time when the question was published",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
class Meta:
|
| 70 |
+
db_table_comment = "Poll questions"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class Answer(models.Model):
|
| 74 |
+
question = models.ForeignKey(
|
| 75 |
+
Question,
|
| 76 |
+
on_delete=models.CASCADE,
|
| 77 |
+
db_comment="Reference to a question",
|
| 78 |
+
)
|
| 79 |
+
answer = models.TextField(db_comment="Question answer")
|
| 80 |
+
|
| 81 |
+
class Meta:
|
| 82 |
+
db_table_comment = "Question answers"
|
| 83 |
+
|
| 84 |
+
Also, the new :class:`~django.db.migrations.operations.AlterModelTableComment`
|
| 85 |
+
operation allows changing table comments defined in the
|
| 86 |
+
:attr:`Meta.db_table_comment <django.db.models.Options.db_table_comment>`.
|
| 87 |
+
|
| 88 |
+
Mitigation for the BREACH attack
|
| 89 |
+
--------------------------------
|
| 90 |
+
|
| 91 |
+
:class:`~django.middleware.gzip.GZipMiddleware` now includes a mitigation for
|
| 92 |
+
the BREACH attack. It will add up to 100 random bytes to gzip responses to make
|
| 93 |
+
BREACH attacks harder. Read more about the mitigation technique in the `Heal
|
| 94 |
+
The Breach (HTB) paper`_.
|
| 95 |
+
|
| 96 |
+
.. _Heal The Breach (HTB) paper: https://ieeexplore.ieee.org/document/9754554
|
| 97 |
+
|
| 98 |
+
In-memory file storage
|
| 99 |
+
----------------------
|
| 100 |
+
|
| 101 |
+
The new :class:`django.core.files.storage.InMemoryStorage` class provides a
|
| 102 |
+
non-persistent storage useful for speeding up tests by avoiding disk access.
|
| 103 |
+
|
| 104 |
+
Custom file storages
|
| 105 |
+
--------------------
|
| 106 |
+
|
| 107 |
+
The new :setting:`STORAGES` setting allows configuring multiple custom file
|
| 108 |
+
storage backends. It also controls storage engines for managing
|
| 109 |
+
:doc:`files </topics/files>` (the ``"default"`` key) and :doc:`static files
|
| 110 |
+
</ref/contrib/staticfiles>` (the ``"staticfiles"`` key).
|
| 111 |
+
|
| 112 |
+
The old ``DEFAULT_FILE_STORAGE`` and ``STATICFILES_STORAGE`` settings are
|
| 113 |
+
deprecated as of this release.
|
| 114 |
+
|
| 115 |
+
Minor features
|
| 116 |
+
--------------
|
| 117 |
+
|
| 118 |
+
:mod:`django.contrib.admin`
|
| 119 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 120 |
+
|
| 121 |
+
* The light or dark color theme of the admin can now be toggled in the UI, as
|
| 122 |
+
well as being set to follow the system setting.
|
| 123 |
+
|
| 124 |
+
* The admin's font stack now prefers system UI fonts and no longer requires
|
| 125 |
+
downloading fonts. Additionally, CSS variables are available to more easily
|
| 126 |
+
override the default font families.
|
| 127 |
+
|
| 128 |
+
* The :source:`admin/delete_confirmation.html
|
| 129 |
+
<django/contrib/admin/templates/admin/delete_confirmation.html>` template now
|
| 130 |
+
has some additional blocks and scripting hooks to ease customization.
|
| 131 |
+
|
| 132 |
+
* The chosen options of
|
| 133 |
+
:attr:`~django.contrib.admin.ModelAdmin.filter_horizontal` and
|
| 134 |
+
:attr:`~django.contrib.admin.ModelAdmin.filter_vertical` widgets are now
|
| 135 |
+
filterable.
|
| 136 |
+
|
| 137 |
+
* The ``admin/base.html`` template now has a new block ``nav-breadcrumbs``
|
| 138 |
+
which contains the navigation landmark and the ``breadcrumbs`` block.
|
| 139 |
+
|
| 140 |
+
* :attr:`.ModelAdmin.list_editable` now uses atomic transactions when making
|
| 141 |
+
edits.
|
| 142 |
+
|
| 143 |
+
* jQuery is upgraded from version 3.6.0 to 3.6.4.
|
| 144 |
+
|
| 145 |
+
:mod:`django.contrib.auth`
|
| 146 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 147 |
+
|
| 148 |
+
* The default iteration count for the PBKDF2 password hasher is increased from
|
| 149 |
+
390,000 to 600,000.
|
| 150 |
+
|
| 151 |
+
* :class:`~django.contrib.auth.forms.UserCreationForm` now saves many-to-many
|
| 152 |
+
form fields for a custom user model.
|
| 153 |
+
|
| 154 |
+
* The new :class:`~django.contrib.auth.forms.BaseUserCreationForm` is now the
|
| 155 |
+
recommended base class for customizing the user creation form.
|
| 156 |
+
|
| 157 |
+
:mod:`django.contrib.gis`
|
| 158 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 159 |
+
|
| 160 |
+
* The :doc:`GeoJSON serializer </ref/contrib/gis/serializers>` now outputs the
|
| 161 |
+
``id`` key for serialized features, which defaults to the primary key of
|
| 162 |
+
objects.
|
| 163 |
+
|
| 164 |
+
* The :class:`~django.contrib.gis.gdal.GDALRaster` class now supports
|
| 165 |
+
:class:`pathlib.Path`.
|
| 166 |
+
|
| 167 |
+
* The :class:`~django.contrib.gis.geoip2.GeoIP2` class now supports ``.mmdb``
|
| 168 |
+
files downloaded from DB-IP.
|
| 169 |
+
|
| 170 |
+
* The OpenLayers template widget no longer includes inline CSS (which also
|
| 171 |
+
removes the former ``map_css`` block) to better comply with a strict Content
|
| 172 |
+
Security Policy.
|
| 173 |
+
|
| 174 |
+
* :class:`~django.contrib.gis.forms.widgets.OpenLayersWidget` is now based on
|
| 175 |
+
OpenLayers 7.2.2 (previously 4.6.5).
|
| 176 |
+
|
| 177 |
+
* The new :lookup:`isempty` lookup and
|
| 178 |
+
:class:`IsEmpty() <django.contrib.gis.db.models.functions.IsEmpty>`
|
| 179 |
+
expression allow filtering empty geometries on PostGIS.
|
| 180 |
+
|
| 181 |
+
* The new :class:`FromWKB() <django.contrib.gis.db.models.functions.FromWKB>`
|
| 182 |
+
and :class:`FromWKT() <django.contrib.gis.db.models.functions.FromWKT>`
|
| 183 |
+
functions allow creating geometries from Well-known binary (WKB) and
|
| 184 |
+
Well-known text (WKT) representations.
|
| 185 |
+
|
| 186 |
+
:mod:`django.contrib.postgres`
|
| 187 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 188 |
+
|
| 189 |
+
* The new :lookup:`trigram_strict_word_similar` lookup, and the
|
| 190 |
+
:class:`TrigramStrictWordSimilarity()
|
| 191 |
+
<django.contrib.postgres.search.TrigramStrictWordSimilarity>` and
|
| 192 |
+
:class:`TrigramStrictWordDistance()
|
| 193 |
+
<django.contrib.postgres.search.TrigramStrictWordDistance>` expressions allow
|
| 194 |
+
using trigram strict word similarity.
|
| 195 |
+
|
| 196 |
+
* The :lookup:`arrayfield.overlap` lookup now supports ``QuerySet.values()``
|
| 197 |
+
and ``values_list()`` as a right-hand side.
|
| 198 |
+
|
| 199 |
+
:mod:`django.contrib.sitemaps`
|
| 200 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 201 |
+
|
| 202 |
+
* The new :meth:`.Sitemap.get_languages_for_item` method allows customizing the
|
| 203 |
+
list of languages for which the item is displayed.
|
| 204 |
+
|
| 205 |
+
:mod:`django.contrib.staticfiles`
|
| 206 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 207 |
+
|
| 208 |
+
* :class:`~django.contrib.staticfiles.storage.ManifestStaticFilesStorage` now
|
| 209 |
+
has experimental support for replacing paths to JavaScript modules in
|
| 210 |
+
``import`` and ``export`` statements with their hashed counterparts. If you
|
| 211 |
+
want to try it, subclass ``ManifestStaticFilesStorage`` and set the
|
| 212 |
+
``support_js_module_import_aggregation`` attribute to ``True``.
|
| 213 |
+
|
| 214 |
+
* The new :attr:`.ManifestStaticFilesStorage.manifest_hash` attribute provides
|
| 215 |
+
a hash over all files in the manifest and changes whenever one of the files
|
| 216 |
+
changes.
|
| 217 |
+
|
| 218 |
+
Database backends
|
| 219 |
+
~~~~~~~~~~~~~~~~~
|
| 220 |
+
|
| 221 |
+
* The new ``"assume_role"`` option is now supported in :setting:`OPTIONS` on
|
| 222 |
+
PostgreSQL to allow specifying the :ref:`session role <database-role>`.
|
| 223 |
+
|
| 224 |
+
* The new ``"server_side_binding"`` option is now supported in
|
| 225 |
+
:setting:`OPTIONS` on PostgreSQL with ``psycopg`` 3.1.8+ to allow using
|
| 226 |
+
:ref:`server-side binding cursors <database-server-side-parameters-binding>`.
|
| 227 |
+
|
| 228 |
+
Error Reporting
|
| 229 |
+
~~~~~~~~~~~~~~~
|
| 230 |
+
|
| 231 |
+
* The debug page now shows :pep:`exception notes <678>` and
|
| 232 |
+
:pep:`fine-grained error locations <657>` on Python 3.11+.
|
| 233 |
+
|
| 234 |
+
* Session cookies are now treated as credentials and therefore hidden and
|
| 235 |
+
replaced with stars (``**********``) in error reports.
|
| 236 |
+
|
| 237 |
+
Forms
|
| 238 |
+
~~~~~
|
| 239 |
+
|
| 240 |
+
* :class:`~django.forms.ModelForm` now accepts the new ``Meta`` option
|
| 241 |
+
``formfield_callback`` to customize form fields.
|
| 242 |
+
|
| 243 |
+
* :func:`~django.forms.models.modelform_factory` now respects the
|
| 244 |
+
``formfield_callback`` attribute of the ``form``’s ``Meta``.
|
| 245 |
+
|
| 246 |
+
Internationalization
|
| 247 |
+
~~~~~~~~~~~~~~~~~~~~
|
| 248 |
+
|
| 249 |
+
* Added support and translations for the Central Kurdish (Sorani) language.
|
| 250 |
+
|
| 251 |
+
Logging
|
| 252 |
+
~~~~~~~
|
| 253 |
+
|
| 254 |
+
* The :ref:`django-db-logger` logger now logs transaction management queries
|
| 255 |
+
(``BEGIN``, ``COMMIT``, and ``ROLLBACK``) at the ``DEBUG`` level.
|
| 256 |
+
|
| 257 |
+
Management Commands
|
| 258 |
+
~~~~~~~~~~~~~~~~~~~
|
| 259 |
+
|
| 260 |
+
* :djadmin:`makemessages` command now supports locales with private sub-tags
|
| 261 |
+
such as ``nl_NL-x-informal``.
|
| 262 |
+
|
| 263 |
+
* The new :option:`makemigrations --update` option merges model changes into
|
| 264 |
+
the latest migration and optimizes the resulting operations.
|
| 265 |
+
|
| 266 |
+
Migrations
|
| 267 |
+
~~~~~~~~~~
|
| 268 |
+
|
| 269 |
+
* Migrations now support serialization of ``enum.Flag`` objects.
|
| 270 |
+
|
| 271 |
+
Models
|
| 272 |
+
~~~~~~
|
| 273 |
+
|
| 274 |
+
* ``QuerySet`` now extensively supports filtering against
|
| 275 |
+
:ref:`window-functions` with the exception of disjunctive filter lookups
|
| 276 |
+
against window functions when performing aggregation.
|
| 277 |
+
|
| 278 |
+
* :meth:`~.QuerySet.prefetch_related` now supports
|
| 279 |
+
:class:`~django.db.models.Prefetch` objects with sliced querysets.
|
| 280 |
+
|
| 281 |
+
* :ref:`Registering lookups <lookup-registration-api>` on
|
| 282 |
+
:class:`~django.db.models.Field` instances is now supported.
|
| 283 |
+
|
| 284 |
+
* The new ``robust`` argument for :func:`~django.db.transaction.on_commit`
|
| 285 |
+
allows performing actions that can fail after a database transaction is
|
| 286 |
+
successfully committed.
|
| 287 |
+
|
| 288 |
+
* The new :class:`KT() <django.db.models.fields.json.KT>` expression represents
|
| 289 |
+
the text value of a key, index, or path transform of
|
| 290 |
+
:class:`~django.db.models.JSONField`.
|
| 291 |
+
|
| 292 |
+
* :class:`~django.db.models.functions.Now` now supports microsecond precision
|
| 293 |
+
on MySQL and millisecond precision on SQLite.
|
| 294 |
+
|
| 295 |
+
* :class:`F() <django.db.models.F>` expressions that output ``BooleanField``
|
| 296 |
+
can now be negated using ``~F()`` (inversion operator).
|
| 297 |
+
|
| 298 |
+
* ``Model`` now provides asynchronous versions of some methods that use the
|
| 299 |
+
database, using an ``a`` prefix: :meth:`~.Model.adelete`,
|
| 300 |
+
:meth:`~.Model.arefresh_from_db`, and :meth:`~.Model.asave`.
|
| 301 |
+
|
| 302 |
+
* Related managers now provide asynchronous versions of methods that change a
|
| 303 |
+
set of related objects, using an ``a`` prefix: :meth:`~.RelatedManager.aadd`,
|
| 304 |
+
:meth:`~.RelatedManager.aclear`, :meth:`~.RelatedManager.aremove`, and
|
| 305 |
+
:meth:`~.RelatedManager.aset`.
|
| 306 |
+
|
| 307 |
+
* :attr:`CharField.max_length <django.db.models.CharField.max_length>` is no
|
| 308 |
+
longer required to be set on PostgreSQL, which supports unlimited ``VARCHAR``
|
| 309 |
+
columns.
|
| 310 |
+
|
| 311 |
+
Requests and Responses
|
| 312 |
+
~~~~~~~~~~~~~~~~~~~~~~
|
| 313 |
+
|
| 314 |
+
* :class:`~django.http.StreamingHttpResponse` now supports async iterators
|
| 315 |
+
when Django is served via ASGI.
|
| 316 |
+
|
| 317 |
+
Tests
|
| 318 |
+
~~~~~
|
| 319 |
+
|
| 320 |
+
* The :option:`test --debug-sql` option now formats SQL queries with
|
| 321 |
+
``sqlparse``.
|
| 322 |
+
|
| 323 |
+
* The :class:`~django.test.RequestFactory`,
|
| 324 |
+
:class:`~django.test.AsyncRequestFactory`, :class:`~django.test.Client`, and
|
| 325 |
+
:class:`~django.test.AsyncClient` classes now support the ``headers``
|
| 326 |
+
parameter, which accepts a dictionary of header names and values. This allows
|
| 327 |
+
a more natural syntax for declaring headers.
|
| 328 |
+
|
| 329 |
+
.. code-block:: python
|
| 330 |
+
|
| 331 |
+
# Before:
|
| 332 |
+
self.client.get("/home/", HTTP_ACCEPT_LANGUAGE="fr")
|
| 333 |
+
await self.async_client.get("/home/", ACCEPT_LANGUAGE="fr")
|
| 334 |
+
|
| 335 |
+
# After:
|
| 336 |
+
self.client.get("/home/", headers={"accept-language": "fr"})
|
| 337 |
+
await self.async_client.get("/home/", headers={"accept-language": "fr"})
|
| 338 |
+
|
| 339 |
+
Utilities
|
| 340 |
+
~~~~~~~~~
|
| 341 |
+
|
| 342 |
+
* The new ``encoder`` parameter for :meth:`django.utils.html.json_script`
|
| 343 |
+
function allows customizing a JSON encoder class.
|
| 344 |
+
|
| 345 |
+
* The private internal vendored copy of ``urllib.parse.urlsplit()`` now strips
|
| 346 |
+
``'\r'``, ``'\n'``, and ``'\t'`` (see :cve:`2022-0391` and :bpo:`43882`).
|
| 347 |
+
This is to protect projects that may be incorrectly using the internal
|
| 348 |
+
``url_has_allowed_host_and_scheme()`` function, instead of using one of the
|
| 349 |
+
documented functions for handling URL redirects. The Django functions were
|
| 350 |
+
not affected.
|
| 351 |
+
|
| 352 |
+
* The new :func:`django.utils.http.content_disposition_header` function returns
|
| 353 |
+
a ``Content-Disposition`` HTTP header value as specified by :rfc:`6266`.
|
| 354 |
+
|
| 355 |
+
Validators
|
| 356 |
+
~~~~~~~~~~
|
| 357 |
+
|
| 358 |
+
* The list of common passwords used by ``CommonPasswordValidator`` is updated
|
| 359 |
+
to the most recent version.
|
| 360 |
+
|
| 361 |
+
.. _backwards-incompatible-4.2:
|
| 362 |
+
|
| 363 |
+
Backwards incompatible changes in 4.2
|
| 364 |
+
=====================================
|
| 365 |
+
|
| 366 |
+
Database backend API
|
| 367 |
+
--------------------
|
| 368 |
+
|
| 369 |
+
This section describes changes that may be needed in third-party database
|
| 370 |
+
backends.
|
| 371 |
+
|
| 372 |
+
* ``DatabaseFeatures.allows_group_by_pk`` is removed as it only remained to
|
| 373 |
+
accommodate a MySQL extension that has been supplanted by proper functional
|
| 374 |
+
dependency detection in MySQL 5.7.15. Note that
|
| 375 |
+
``DatabaseFeatures.allows_group_by_selected_pks`` is still supported and
|
| 376 |
+
should be enabled if your backend supports functional dependency detection in
|
| 377 |
+
``GROUP BY`` clauses as specified by the ``SQL:1999`` standard.
|
| 378 |
+
|
| 379 |
+
* :djadmin:`inspectdb` now uses ``display_size`` from
|
| 380 |
+
``DatabaseIntrospection.get_table_description()`` rather than
|
| 381 |
+
``internal_size`` for ``CharField``.
|
| 382 |
+
|
| 383 |
+
Dropped support for MariaDB 10.3
|
| 384 |
+
--------------------------------
|
| 385 |
+
|
| 386 |
+
Upstream support for MariaDB 10.3 ends in May 2023. Django 4.2 supports MariaDB
|
| 387 |
+
10.4 and higher.
|
| 388 |
+
|
| 389 |
+
Dropped support for MySQL 5.7
|
| 390 |
+
-----------------------------
|
| 391 |
+
|
| 392 |
+
Upstream support for MySQL 5.7 ends in October 2023. Django 4.2 supports MySQL
|
| 393 |
+
8 and higher.
|
| 394 |
+
|
| 395 |
+
Dropped support for PostgreSQL 11
|
| 396 |
+
---------------------------------
|
| 397 |
+
|
| 398 |
+
Upstream support for PostgreSQL 11 ends in November 2023. Django 4.2 supports
|
| 399 |
+
PostgreSQL 12 and higher.
|
| 400 |
+
|
| 401 |
+
Setting ``update_fields`` in ``Model.save()`` may now be required
|
| 402 |
+
-----------------------------------------------------------------
|
| 403 |
+
|
| 404 |
+
In order to avoid updating unnecessary columns,
|
| 405 |
+
:meth:`.QuerySet.update_or_create` now passes ``update_fields`` to the
|
| 406 |
+
:meth:`Model.save() <django.db.models.Model.save>` calls. As a consequence, any
|
| 407 |
+
fields modified in the custom ``save()`` methods should be added to the
|
| 408 |
+
``update_fields`` keyword argument before calling ``super()``. See
|
| 409 |
+
:ref:`overriding-model-methods` for more details.
|
| 410 |
+
|
| 411 |
+
Miscellaneous
|
| 412 |
+
-------------
|
| 413 |
+
|
| 414 |
+
* The undocumented ``django.http.multipartparser.parse_header()`` function is
|
| 415 |
+
removed. Use ``django.utils.http.parse_header_parameters()`` instead.
|
| 416 |
+
|
| 417 |
+
* :ttag:`{% blocktranslate asvar … %}<blocktranslate>` result is now marked as
|
| 418 |
+
safe for (HTML) output purposes.
|
| 419 |
+
|
| 420 |
+
* The ``autofocus`` HTML attribute in the admin search box is removed as it can
|
| 421 |
+
be confusing for screen readers.
|
| 422 |
+
|
| 423 |
+
* The :option:`makemigrations --check` option no longer creates missing
|
| 424 |
+
migration files.
|
| 425 |
+
|
| 426 |
+
* The ``alias`` argument for :meth:`.Expression.get_group_by_cols` is removed.
|
| 427 |
+
|
| 428 |
+
* The minimum supported version of ``sqlparse`` is increased from 0.2.2 to
|
| 429 |
+
0.3.1.
|
| 430 |
+
|
| 431 |
+
* The undocumented ``negated`` parameter of the
|
| 432 |
+
:class:`~django.db.models.Exists` expression is removed.
|
| 433 |
+
|
| 434 |
+
* The ``is_summary`` argument of the undocumented ``Query.add_annotation()``
|
| 435 |
+
method is removed.
|
| 436 |
+
|
| 437 |
+
* The minimum supported version of SQLite is increased from 3.9.0 to 3.21.0.
|
| 438 |
+
|
| 439 |
+
* The minimum supported version of ``asgiref`` is increased from 3.5.2 to
|
| 440 |
+
3.6.0.
|
| 441 |
+
|
| 442 |
+
* :class:`~django.contrib.auth.forms.UserCreationForm` now rejects usernames
|
| 443 |
+
that differ only in case. If you need the previous behavior, use
|
| 444 |
+
:class:`~django.contrib.auth.forms.BaseUserCreationForm` instead.
|
| 445 |
+
|
| 446 |
+
* The minimum supported version of ``mysqlclient`` is increased from 1.4.0 to
|
| 447 |
+
1.4.3.
|
| 448 |
+
|
| 449 |
+
* The minimum supported version of ``argon2-cffi`` is increased from 19.1.0 to
|
| 450 |
+
19.2.0.
|
| 451 |
+
|
| 452 |
+
* The minimum supported version of ``Pillow`` is increased from 6.2.0 to 6.2.1.
|
| 453 |
+
|
| 454 |
+
* The minimum supported version of ``jinja2`` is increased from 2.9.2 to
|
| 455 |
+
2.11.0.
|
| 456 |
+
|
| 457 |
+
* The minimum supported version of :pypi:`redis-py <redis>` is increased from
|
| 458 |
+
3.0.0 to 3.4.0.
|
| 459 |
+
|
| 460 |
+
* Manually instantiated ``WSGIRequest`` objects must be provided a file-like
|
| 461 |
+
object for ``wsgi.input``. Previously, Django was more lax than the expected
|
| 462 |
+
behavior as specified by the WSGI specification.
|
| 463 |
+
|
| 464 |
+
* Support for ``PROJ`` < 5 is removed.
|
| 465 |
+
|
| 466 |
+
* :class:`~django.core.mail.backends.smtp.EmailBackend` now verifies a
|
| 467 |
+
:py:attr:`hostname <ssl.SSLContext.check_hostname>` and
|
| 468 |
+
:py:attr:`certificates <ssl.SSLContext.verify_mode>`. If you need the
|
| 469 |
+
previous behavior that is less restrictive and not recommended, subclass
|
| 470 |
+
``EmailBackend`` and override the ``ssl_context`` property.
|
| 471 |
+
|
| 472 |
+
.. _deprecated-features-4.2:
|
| 473 |
+
|
| 474 |
+
Features deprecated in 4.2
|
| 475 |
+
==========================
|
| 476 |
+
|
| 477 |
+
``index_together`` option is deprecated in favor of ``indexes``
|
| 478 |
+
---------------------------------------------------------------
|
| 479 |
+
|
| 480 |
+
The :attr:`Meta.index_together <django.db.models.Options.index_together>`
|
| 481 |
+
option is deprecated in favor of the :attr:`~django.db.models.Options.indexes`
|
| 482 |
+
option.
|
| 483 |
+
|
| 484 |
+
Migrating existing ``index_together`` should be handled as a migration. For
|
| 485 |
+
example::
|
| 486 |
+
|
| 487 |
+
class Author(models.Model):
|
| 488 |
+
rank = models.IntegerField()
|
| 489 |
+
name = models.CharField(max_length=30)
|
| 490 |
+
|
| 491 |
+
class Meta:
|
| 492 |
+
index_together = [["rank", "name"]]
|
| 493 |
+
|
| 494 |
+
Should become::
|
| 495 |
+
|
| 496 |
+
class Author(models.Model):
|
| 497 |
+
rank = models.IntegerField()
|
| 498 |
+
name = models.CharField(max_length=30)
|
| 499 |
+
|
| 500 |
+
class Meta:
|
| 501 |
+
indexes = [models.Index(fields=["rank", "name"])]
|
| 502 |
+
|
| 503 |
+
Running the :djadmin:`makemigrations` command will generate a migration
|
| 504 |
+
containing a :class:`~django.db.migrations.operations.RenameIndex` operation
|
| 505 |
+
which will rename the existing index. Next, consider squashing migrations to
|
| 506 |
+
remove ``index_together`` from historical migrations.
|
| 507 |
+
|
| 508 |
+
The ``AlterIndexTogether`` migration operation is now officially supported only
|
| 509 |
+
for pre-Django 4.2 migration files. For backward compatibility reasons, it's
|
| 510 |
+
still part of the public API, and there's no plan to deprecate or remove it,
|
| 511 |
+
but it should not be used for new migrations. Use
|
| 512 |
+
:class:`~django.db.migrations.operations.AddIndex` and
|
| 513 |
+
:class:`~django.db.migrations.operations.RemoveIndex` operations instead.
|
| 514 |
+
|
| 515 |
+
Passing encoded JSON string literals to ``JSONField`` is deprecated
|
| 516 |
+
-------------------------------------------------------------------
|
| 517 |
+
|
| 518 |
+
``JSONField`` and its associated lookups and aggregates used to allow passing
|
| 519 |
+
JSON encoded string literals which caused ambiguity on whether string literals
|
| 520 |
+
were already encoded from database backend's perspective.
|
| 521 |
+
|
| 522 |
+
During the deprecation period string literals will be attempted to be JSON
|
| 523 |
+
decoded and a warning will be emitted on success that points at passing
|
| 524 |
+
non-encoded forms instead.
|
| 525 |
+
|
| 526 |
+
Code that use to pass JSON encoded string literals::
|
| 527 |
+
|
| 528 |
+
Document.objects.bulk_create(
|
| 529 |
+
Document(data=Value("null")),
|
| 530 |
+
Document(data=Value("[]")),
|
| 531 |
+
Document(data=Value('"foo-bar"')),
|
| 532 |
+
)
|
| 533 |
+
Document.objects.annotate(
|
| 534 |
+
JSONBAgg("field", default=Value("[]")),
|
| 535 |
+
)
|
| 536 |
+
|
| 537 |
+
Should become::
|
| 538 |
+
|
| 539 |
+
Document.objects.bulk_create(
|
| 540 |
+
Document(data=Value(None, JSONField())),
|
| 541 |
+
Document(data=[]),
|
| 542 |
+
Document(data="foo-bar"),
|
| 543 |
+
)
|
| 544 |
+
Document.objects.annotate(
|
| 545 |
+
JSONBAgg("field", default=[]),
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
From Django 5.1+ string literals will be implicitly interpreted as JSON string
|
| 549 |
+
literals.
|
| 550 |
+
|
| 551 |
+
Miscellaneous
|
| 552 |
+
-------------
|
| 553 |
+
|
| 554 |
+
* The ``BaseUserManager.make_random_password()`` method is deprecated. See
|
| 555 |
+
`recipes and best practices
|
| 556 |
+
<https://docs.python.org/3/library/secrets.html#recipes-and-best-practices>`_
|
| 557 |
+
for using Python's :py:mod:`secrets` module to generate passwords.
|
| 558 |
+
|
| 559 |
+
* The ``length_is`` template filter is deprecated in favor of :tfilter:`length`
|
| 560 |
+
and the ``==`` operator within an :ttag:`{% if %}<if>` tag. For example
|
| 561 |
+
|
| 562 |
+
.. code-block:: html+django
|
| 563 |
+
|
| 564 |
+
{% if value|length == 4 %}…{% endif %}
|
| 565 |
+
{% if value|length == 4 %}True{% else %}False{% endif %}
|
| 566 |
+
|
| 567 |
+
instead of:
|
| 568 |
+
|
| 569 |
+
.. code-block:: html+django
|
| 570 |
+
|
| 571 |
+
{% if value|length_is:4 %}…{% endif %}
|
| 572 |
+
{{ value|length_is:4 }}
|
| 573 |
+
|
| 574 |
+
* ``django.contrib.auth.hashers.SHA1PasswordHasher``,
|
| 575 |
+
``django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher``, and
|
| 576 |
+
``django.contrib.auth.hashers.UnsaltedMD5PasswordHasher`` are deprecated.
|
| 577 |
+
|
| 578 |
+
* ``django.contrib.postgres.fields.CICharField`` is deprecated in favor of
|
| 579 |
+
``CharField(db_collation="…")`` with a case-insensitive non-deterministic
|
| 580 |
+
collation.
|
| 581 |
+
|
| 582 |
+
* ``django.contrib.postgres.fields.CIEmailField`` is deprecated in favor of
|
| 583 |
+
``EmailField(db_collation="…")`` with a case-insensitive non-deterministic
|
| 584 |
+
collation.
|
| 585 |
+
|
| 586 |
+
* ``django.contrib.postgres.fields.CITextField`` is deprecated in favor of
|
| 587 |
+
``TextField(db_collation="…")`` with a case-insensitive non-deterministic
|
| 588 |
+
collation.
|
| 589 |
+
|
| 590 |
+
* ``django.contrib.postgres.fields.CIText`` mixin is deprecated.
|
| 591 |
+
|
| 592 |
+
* The ``map_height`` and ``map_width`` attributes of ``BaseGeometryWidget`` are
|
| 593 |
+
deprecated, use CSS to size map widgets instead.
|
| 594 |
+
|
| 595 |
+
* ``SimpleTestCase.assertFormsetError()`` is deprecated in favor of
|
| 596 |
+
``assertFormSetError()``.
|
| 597 |
+
|
| 598 |
+
* ``TransactionTestCase.assertQuerysetEqual()`` is deprecated in favor of
|
| 599 |
+
``assertQuerySetEqual()``.
|
| 600 |
+
|
| 601 |
+
* Passing positional arguments to ``Signer`` and ``TimestampSigner`` is
|
| 602 |
+
deprecated in favor of keyword-only arguments.
|
| 603 |
+
|
| 604 |
+
* The ``DEFAULT_FILE_STORAGE`` setting is deprecated in favor of
|
| 605 |
+
``STORAGES["default"]``.
|
| 606 |
+
|
| 607 |
+
* The ``STATICFILES_STORAGE`` setting is deprecated in favor of
|
| 608 |
+
``STORAGES["staticfiles"]``.
|
| 609 |
+
|
| 610 |
+
* The ``django.core.files.storage.get_storage_class()`` function is deprecated.
|
testbed/django__django/docs/releases/5.0.txt
ADDED
|
@@ -0,0 +1,646 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
============================================
|
| 2 |
+
Django 5.0 release notes - UNDER DEVELOPMENT
|
| 3 |
+
============================================
|
| 4 |
+
|
| 5 |
+
*Expected December 2023*
|
| 6 |
+
|
| 7 |
+
Welcome to Django 5.0!
|
| 8 |
+
|
| 9 |
+
These release notes cover the :ref:`new features <whats-new-5.0>`, as well as
|
| 10 |
+
some :ref:`backwards incompatible changes <backwards-incompatible-5.0>` you'll
|
| 11 |
+
want to be aware of when upgrading from Django 4.2 or earlier. We've
|
| 12 |
+
:ref:`begun the deprecation process for some features
|
| 13 |
+
<deprecated-features-5.0>`.
|
| 14 |
+
|
| 15 |
+
See the :doc:`/howto/upgrade-version` guide if you're updating an existing
|
| 16 |
+
project.
|
| 17 |
+
|
| 18 |
+
Python compatibility
|
| 19 |
+
====================
|
| 20 |
+
|
| 21 |
+
Django 5.0 supports Python 3.10, 3.11, and 3.12. We **highly recommend** and
|
| 22 |
+
only officially support the latest release of each series.
|
| 23 |
+
|
| 24 |
+
The Django 4.2.x series is the last to support Python 3.8 and 3.9.
|
| 25 |
+
|
| 26 |
+
Third-party library support for older version of Django
|
| 27 |
+
=======================================================
|
| 28 |
+
|
| 29 |
+
Following the release of Django 5.0, we suggest that third-party app authors
|
| 30 |
+
drop support for all versions of Django prior to 4.2. At that time, you should
|
| 31 |
+
be able to run your package's tests using ``python -Wd`` so that deprecation
|
| 32 |
+
warnings appear. After making the deprecation warning fixes, your app should be
|
| 33 |
+
compatible with Django 5.0.
|
| 34 |
+
|
| 35 |
+
.. _whats-new-5.0:
|
| 36 |
+
|
| 37 |
+
What's new in Django 5.0
|
| 38 |
+
========================
|
| 39 |
+
|
| 40 |
+
Facet filters in the admin
|
| 41 |
+
--------------------------
|
| 42 |
+
|
| 43 |
+
Facet counts are now shown for applied filters in the admin changelist when
|
| 44 |
+
toggled on via the UI. This behavior can be changed via the new
|
| 45 |
+
:attr:`.ModelAdmin.show_facets` attribute. For more information see
|
| 46 |
+
:ref:`facet-filters`.
|
| 47 |
+
|
| 48 |
+
Simplified templates for form field rendering
|
| 49 |
+
---------------------------------------------
|
| 50 |
+
|
| 51 |
+
Django 5.0 introduces the concept of a field group, and field group templates.
|
| 52 |
+
This simplifies rendering of the related elements of a Django form field such
|
| 53 |
+
as its label, widget, help text, and errors.
|
| 54 |
+
|
| 55 |
+
For example, the template below:
|
| 56 |
+
|
| 57 |
+
.. code-block:: html+django
|
| 58 |
+
|
| 59 |
+
<form>
|
| 60 |
+
...
|
| 61 |
+
<div>
|
| 62 |
+
{{ form.name.label_tag }}
|
| 63 |
+
{% if form.name.help_text %}
|
| 64 |
+
<div class="helptext" id="{{ form.name.id_for_label }}_helptext">
|
| 65 |
+
{{ form.name.help_text|safe }}
|
| 66 |
+
</div>
|
| 67 |
+
{% endif %}
|
| 68 |
+
{{ form.name.errors }}
|
| 69 |
+
{{ form.name }}
|
| 70 |
+
<div class="row">
|
| 71 |
+
<div class="col">
|
| 72 |
+
{{ form.email.label_tag }}
|
| 73 |
+
{% if form.email.help_text %}
|
| 74 |
+
<div class="helptext" id="{{ form.email.id_for_label }}_helptext">
|
| 75 |
+
{{ form.email.help_text|safe }}
|
| 76 |
+
</div>
|
| 77 |
+
{% endif %}
|
| 78 |
+
{{ form.email.errors }}
|
| 79 |
+
{{ form.email }}
|
| 80 |
+
</div>
|
| 81 |
+
<div class="col">
|
| 82 |
+
{{ form.password.label_tag }}
|
| 83 |
+
{% if form.password.help_text %}
|
| 84 |
+
<div class="helptext" id="{{ form.password.id_for_label }}_helptext">
|
| 85 |
+
{{ form.password.help_text|safe }}
|
| 86 |
+
</div>
|
| 87 |
+
{% endif %}
|
| 88 |
+
{{ form.password.errors }}
|
| 89 |
+
{{ form.password }}
|
| 90 |
+
</div>
|
| 91 |
+
</div>
|
| 92 |
+
</div>
|
| 93 |
+
...
|
| 94 |
+
</form>
|
| 95 |
+
|
| 96 |
+
Can now be simplified to:
|
| 97 |
+
|
| 98 |
+
.. code-block:: html+django
|
| 99 |
+
|
| 100 |
+
<form>
|
| 101 |
+
...
|
| 102 |
+
<div>
|
| 103 |
+
{{ form.name.as_field_group }}
|
| 104 |
+
<div class="row">
|
| 105 |
+
<div class="col">{{ form.email.as_field_group }}</div>
|
| 106 |
+
<div class="col">{{ form.password.as_field_group }}</div>
|
| 107 |
+
</div>
|
| 108 |
+
</div>
|
| 109 |
+
...
|
| 110 |
+
</form>
|
| 111 |
+
|
| 112 |
+
:meth:`~django.forms.BoundField.as_field_group` renders fields with the
|
| 113 |
+
``"django/forms/field.html"`` template by default and can be customized on a
|
| 114 |
+
per-project, per-field, or per-request basis. See
|
| 115 |
+
:ref:`reusable-field-group-templates`.
|
| 116 |
+
|
| 117 |
+
Database-computed default values
|
| 118 |
+
--------------------------------
|
| 119 |
+
|
| 120 |
+
The new :attr:`Field.db_default <django.db.models.Field.db_default>` parameter
|
| 121 |
+
sets a database-computed default value. For example::
|
| 122 |
+
|
| 123 |
+
from django.db import models
|
| 124 |
+
from django.db.models.functions import Now, Pi
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class MyModel(models.Model):
|
| 128 |
+
age = models.IntegerField(db_default=18)
|
| 129 |
+
created = models.DateTimeField(db_default=Now())
|
| 130 |
+
circumference = models.FloatField(db_default=2 * Pi())
|
| 131 |
+
|
| 132 |
+
Minor features
|
| 133 |
+
--------------
|
| 134 |
+
|
| 135 |
+
:mod:`django.contrib.admin`
|
| 136 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 137 |
+
|
| 138 |
+
* The new :meth:`.AdminSite.get_log_entries` method allows customizing the
|
| 139 |
+
queryset for the site's listed log entries.
|
| 140 |
+
|
| 141 |
+
* The ``django.contrib.admin.AllValuesFieldListFilter``,
|
| 142 |
+
``ChoicesFieldListFilter``, ``RelatedFieldListFilter``, and
|
| 143 |
+
``RelatedOnlyFieldListFilter`` admin filters now handle multi-valued query
|
| 144 |
+
parameters.
|
| 145 |
+
|
| 146 |
+
* ``XRegExp`` is upgraded from version 3.2.0 to 5.1.1.
|
| 147 |
+
|
| 148 |
+
* The new :meth:`.AdminSite.get_model_admin` method returns an admin class for
|
| 149 |
+
the given model class.
|
| 150 |
+
|
| 151 |
+
:mod:`django.contrib.admindocs`
|
| 152 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 153 |
+
|
| 154 |
+
* ...
|
| 155 |
+
|
| 156 |
+
:mod:`django.contrib.auth`
|
| 157 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 158 |
+
|
| 159 |
+
* The default iteration count for the PBKDF2 password hasher is increased from
|
| 160 |
+
600,000 to 720,000.
|
| 161 |
+
|
| 162 |
+
* The new asynchronous functions are now provided, using an
|
| 163 |
+
``a`` prefix: :func:`django.contrib.auth.aauthenticate`,
|
| 164 |
+
:func:`~.django.contrib.auth.aget_user`,
|
| 165 |
+
:func:`~.django.contrib.auth.alogin`, :func:`~.django.contrib.auth.alogout`,
|
| 166 |
+
and :func:`~.django.contrib.auth.aupdate_session_auth_hash`.
|
| 167 |
+
|
| 168 |
+
* ``AuthenticationMiddleware`` now adds an :meth:`.HttpRequest.auser`
|
| 169 |
+
asynchronous method that returns the currently logged-in user.
|
| 170 |
+
|
| 171 |
+
* The new :func:`django.contrib.auth.hashers.acheck_password` asynchronous
|
| 172 |
+
function and :meth:`.AbstractBaseUser.acheck_password` method allow
|
| 173 |
+
asynchronous checking of user passwords.
|
| 174 |
+
|
| 175 |
+
:mod:`django.contrib.contenttypes`
|
| 176 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 177 |
+
|
| 178 |
+
* ...
|
| 179 |
+
|
| 180 |
+
:mod:`django.contrib.gis`
|
| 181 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 182 |
+
|
| 183 |
+
* The new
|
| 184 |
+
:class:`ClosestPoint() <django.contrib.gis.db.models.functions.ClosestPoint>`
|
| 185 |
+
function returns a 2-dimensional point on the geometry that is closest to
|
| 186 |
+
another geometry.
|
| 187 |
+
|
| 188 |
+
* :ref:`GIS aggregates <gis-aggregation-functions>` now support the ``filter``
|
| 189 |
+
argument.
|
| 190 |
+
|
| 191 |
+
* Added support for GDAL 3.7.
|
| 192 |
+
|
| 193 |
+
:mod:`django.contrib.messages`
|
| 194 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 195 |
+
|
| 196 |
+
* ...
|
| 197 |
+
|
| 198 |
+
:mod:`django.contrib.postgres`
|
| 199 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 200 |
+
|
| 201 |
+
* The new :attr:`~.ExclusionConstraint.violation_error_code` attribute of
|
| 202 |
+
:class:`~django.contrib.postgres.constraints.ExclusionConstraint` allows
|
| 203 |
+
customizing the ``code`` of ``ValidationError`` raised during
|
| 204 |
+
:ref:`model validation <validating-objects>`.
|
| 205 |
+
|
| 206 |
+
:mod:`django.contrib.redirects`
|
| 207 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 208 |
+
|
| 209 |
+
* ...
|
| 210 |
+
|
| 211 |
+
:mod:`django.contrib.sessions`
|
| 212 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 213 |
+
|
| 214 |
+
* ...
|
| 215 |
+
|
| 216 |
+
:mod:`django.contrib.sitemaps`
|
| 217 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 218 |
+
|
| 219 |
+
* ...
|
| 220 |
+
|
| 221 |
+
:mod:`django.contrib.sites`
|
| 222 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 223 |
+
|
| 224 |
+
* ...
|
| 225 |
+
|
| 226 |
+
:mod:`django.contrib.staticfiles`
|
| 227 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 228 |
+
|
| 229 |
+
* ...
|
| 230 |
+
|
| 231 |
+
:mod:`django.contrib.syndication`
|
| 232 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 233 |
+
|
| 234 |
+
* ...
|
| 235 |
+
|
| 236 |
+
Asynchronous views
|
| 237 |
+
~~~~~~~~~~~~~~~~~~
|
| 238 |
+
|
| 239 |
+
* Under ASGI, ``http.disconnect`` events are now handled. This allows views to
|
| 240 |
+
perform any necessary cleanup if a client disconnects before the response is
|
| 241 |
+
generated. See :ref:`async-handling-disconnect` for more details.
|
| 242 |
+
|
| 243 |
+
Cache
|
| 244 |
+
~~~~~
|
| 245 |
+
|
| 246 |
+
* ...
|
| 247 |
+
|
| 248 |
+
CSRF
|
| 249 |
+
~~~~
|
| 250 |
+
|
| 251 |
+
* ...
|
| 252 |
+
|
| 253 |
+
Decorators
|
| 254 |
+
~~~~~~~~~~
|
| 255 |
+
|
| 256 |
+
* The following decorators now support wrapping asynchronous view functions:
|
| 257 |
+
|
| 258 |
+
* :func:`~django.views.decorators.cache.cache_control`
|
| 259 |
+
* :func:`~django.views.decorators.cache.never_cache`
|
| 260 |
+
* :func:`~django.views.decorators.common.no_append_slash`
|
| 261 |
+
* :func:`~django.views.decorators.csrf.csrf_exempt`
|
| 262 |
+
* :func:`~django.views.decorators.debug.sensitive_variables`
|
| 263 |
+
* :func:`~django.views.decorators.debug.sensitive_post_parameters`
|
| 264 |
+
* :func:`~django.views.decorators.http.condition`
|
| 265 |
+
* :func:`~django.views.decorators.http.etag`
|
| 266 |
+
* :func:`~django.views.decorators.http.last_modified`
|
| 267 |
+
* :func:`~django.views.decorators.http.require_http_methods`
|
| 268 |
+
* :func:`~django.views.decorators.http.require_GET`
|
| 269 |
+
* :func:`~django.views.decorators.http.require_POST`
|
| 270 |
+
* :func:`~django.views.decorators.http.require_safe`
|
| 271 |
+
* :func:`~django.views.decorators.vary.vary_on_cookie`
|
| 272 |
+
* :func:`~django.views.decorators.vary.vary_on_headers`
|
| 273 |
+
* ``xframe_options_deny()``
|
| 274 |
+
* ``xframe_options_sameorigin()``
|
| 275 |
+
* ``xframe_options_exempt()``
|
| 276 |
+
|
| 277 |
+
Email
|
| 278 |
+
~~~~~
|
| 279 |
+
|
| 280 |
+
* ...
|
| 281 |
+
|
| 282 |
+
Error Reporting
|
| 283 |
+
~~~~~~~~~~~~~~~
|
| 284 |
+
|
| 285 |
+
* :func:`~django.views.decorators.debug.sensitive_variables` and
|
| 286 |
+
:func:`~django.views.decorators.debug.sensitive_post_parameters` can now be
|
| 287 |
+
used with asynchronous functions.
|
| 288 |
+
|
| 289 |
+
File Storage
|
| 290 |
+
~~~~~~~~~~~~
|
| 291 |
+
|
| 292 |
+
* ...
|
| 293 |
+
|
| 294 |
+
File Uploads
|
| 295 |
+
~~~~~~~~~~~~
|
| 296 |
+
|
| 297 |
+
* ...
|
| 298 |
+
|
| 299 |
+
Forms
|
| 300 |
+
~~~~~
|
| 301 |
+
|
| 302 |
+
* :attr:`.ChoiceField.choices` now accepts
|
| 303 |
+
:ref:`Choices classes <field-choices-enum-types>` directly instead of
|
| 304 |
+
requiring expansion with the ``choices`` attribute.
|
| 305 |
+
|
| 306 |
+
* The new ``assume_scheme`` argument for :class:`~django.forms.URLField` allows
|
| 307 |
+
specifying a default URL scheme.
|
| 308 |
+
|
| 309 |
+
* In order to improve accessibility and enable screen readers to associate form
|
| 310 |
+
fields with their help text, the form field now includes the
|
| 311 |
+
``aria-describedby`` HTML attribute.
|
| 312 |
+
|
| 313 |
+
Generic Views
|
| 314 |
+
~~~~~~~~~~~~~
|
| 315 |
+
|
| 316 |
+
* ...
|
| 317 |
+
|
| 318 |
+
Internationalization
|
| 319 |
+
~~~~~~~~~~~~~~~~~~~~
|
| 320 |
+
|
| 321 |
+
* ...
|
| 322 |
+
|
| 323 |
+
Logging
|
| 324 |
+
~~~~~~~
|
| 325 |
+
|
| 326 |
+
* ...
|
| 327 |
+
|
| 328 |
+
Management Commands
|
| 329 |
+
~~~~~~~~~~~~~~~~~~~
|
| 330 |
+
|
| 331 |
+
* ...
|
| 332 |
+
|
| 333 |
+
Migrations
|
| 334 |
+
~~~~~~~~~~
|
| 335 |
+
|
| 336 |
+
* ...
|
| 337 |
+
|
| 338 |
+
Models
|
| 339 |
+
~~~~~~
|
| 340 |
+
|
| 341 |
+
* The new ``create_defaults`` argument of :meth:`.QuerySet.update_or_create`
|
| 342 |
+
and :meth:`.QuerySet.aupdate_or_create` methods allows specifying a different
|
| 343 |
+
field values for the create operation.
|
| 344 |
+
|
| 345 |
+
* The new ``violation_error_code`` attribute of
|
| 346 |
+
:class:`~django.db.models.BaseConstraint`,
|
| 347 |
+
:class:`~django.db.models.CheckConstraint`, and
|
| 348 |
+
:class:`~django.db.models.UniqueConstraint` allows customizing the ``code``
|
| 349 |
+
of ``ValidationError`` raised during
|
| 350 |
+
:ref:`model validation <validating-objects>`.
|
| 351 |
+
|
| 352 |
+
* :attr:`.Field.choices` now accepts
|
| 353 |
+
:ref:`Choices classes <field-choices-enum-types>` directly instead of
|
| 354 |
+
requiring expansion with the ``choices`` attribute.
|
| 355 |
+
|
| 356 |
+
* The :ref:`force_insert <ref-models-force-insert>` argument of
|
| 357 |
+
:meth:`.Model.save` now allows specifying a tuple of parent classes that must
|
| 358 |
+
be forced to be inserted.
|
| 359 |
+
|
| 360 |
+
* :meth:`.QuerySet.bulk_create` and :meth:`.QuerySet.abulk_create` methods now
|
| 361 |
+
set the primary key on each model instance when the ``update_conflicts``
|
| 362 |
+
parameter is enabled (if the database supports it).
|
| 363 |
+
|
| 364 |
+
Pagination
|
| 365 |
+
~~~~~~~~~~
|
| 366 |
+
|
| 367 |
+
* The new :attr:`django.core.paginator.Paginator.error_messages` argument
|
| 368 |
+
allows customizing the error messages raised by :meth:`.Paginator.page`.
|
| 369 |
+
|
| 370 |
+
Requests and Responses
|
| 371 |
+
~~~~~~~~~~~~~~~~~~~~~~
|
| 372 |
+
|
| 373 |
+
* ...
|
| 374 |
+
|
| 375 |
+
Security
|
| 376 |
+
~~~~~~~~
|
| 377 |
+
|
| 378 |
+
* ...
|
| 379 |
+
|
| 380 |
+
Serialization
|
| 381 |
+
~~~~~~~~~~~~~
|
| 382 |
+
|
| 383 |
+
* ...
|
| 384 |
+
|
| 385 |
+
Signals
|
| 386 |
+
~~~~~~~
|
| 387 |
+
|
| 388 |
+
* The new :meth:`.Signal.asend` and :meth:`.Signal.asend_robust` methods allow
|
| 389 |
+
asynchronous signal dispatch. Signal receivers may be synchronous or
|
| 390 |
+
asynchronous, and will be automatically adapted to the correct calling style.
|
| 391 |
+
|
| 392 |
+
Templates
|
| 393 |
+
~~~~~~~~~
|
| 394 |
+
|
| 395 |
+
* The new :tfilter:`escapeseq` template filter applies :tfilter:`escape` to
|
| 396 |
+
each element of a sequence.
|
| 397 |
+
|
| 398 |
+
Tests
|
| 399 |
+
~~~~~
|
| 400 |
+
|
| 401 |
+
* :class:`~django.test.Client` and :class:`~django.test.AsyncClient` now
|
| 402 |
+
provide asynchronous methods, using an ``a`` prefix:
|
| 403 |
+
:meth:`~django.test.Client.asession`, :meth:`~django.test.Client.alogin`,
|
| 404 |
+
:meth:`~django.test.Client.aforce_login`, and
|
| 405 |
+
:meth:`~django.test.Client.alogout`.
|
| 406 |
+
|
| 407 |
+
URLs
|
| 408 |
+
~~~~
|
| 409 |
+
|
| 410 |
+
* ...
|
| 411 |
+
|
| 412 |
+
Utilities
|
| 413 |
+
~~~~~~~~~
|
| 414 |
+
|
| 415 |
+
* ...
|
| 416 |
+
|
| 417 |
+
Validators
|
| 418 |
+
~~~~~~~~~~
|
| 419 |
+
|
| 420 |
+
* The new ``offset`` argument of
|
| 421 |
+
:class:`~django.core.validators.StepValueValidator` allows specifying an
|
| 422 |
+
offset for valid values.
|
| 423 |
+
|
| 424 |
+
.. _backwards-incompatible-5.0:
|
| 425 |
+
|
| 426 |
+
Backwards incompatible changes in 5.0
|
| 427 |
+
=====================================
|
| 428 |
+
|
| 429 |
+
Database backend API
|
| 430 |
+
--------------------
|
| 431 |
+
|
| 432 |
+
This section describes changes that may be needed in third-party database
|
| 433 |
+
backends.
|
| 434 |
+
|
| 435 |
+
* ``DatabaseFeatures.supports_expression_defaults`` should be set to ``False``
|
| 436 |
+
if the database doesn't support using database functions as defaults.
|
| 437 |
+
|
| 438 |
+
* ``DatabaseFeatures.supports_default_keyword_in_insert`` should be set to
|
| 439 |
+
``False`` if the database doesn't support the ``DEFAULT`` keyword in
|
| 440 |
+
``INSERT`` queries.
|
| 441 |
+
|
| 442 |
+
* ``DatabaseFeatures.supports_default_keyword_in_bulk insert`` should be set to
|
| 443 |
+
``False`` if the database doesn't support the ``DEFAULT`` keyword in bulk
|
| 444 |
+
``INSERT`` queries.
|
| 445 |
+
|
| 446 |
+
:mod:`django.contrib.gis`
|
| 447 |
+
-------------------------
|
| 448 |
+
|
| 449 |
+
* Support for GDAL 2.2 and 2.3 is removed.
|
| 450 |
+
|
| 451 |
+
* Support for GEOS 3.6 and 3.7 is removed.
|
| 452 |
+
|
| 453 |
+
:mod:`django.contrib.sitemaps`
|
| 454 |
+
------------------------------
|
| 455 |
+
|
| 456 |
+
* The ``django.contrib.sitemaps.ping_google()`` function and the
|
| 457 |
+
``ping_google`` management command are removed as the Google
|
| 458 |
+
Sitemaps ping endpoint is deprecated and will be removed in January 2024.
|
| 459 |
+
|
| 460 |
+
* The ``django.contrib.sitemaps.SitemapNotFound`` exception class is removed.
|
| 461 |
+
|
| 462 |
+
Using ``create_defaults__exact`` may now be required with ``QuerySet.update_or_create()``
|
| 463 |
+
-----------------------------------------------------------------------------------------
|
| 464 |
+
|
| 465 |
+
:meth:`.QuerySet.update_or_create` now supports the parameter
|
| 466 |
+
``create_defaults``. As a consequence, any models that have a field named
|
| 467 |
+
``create_defaults`` that are used with an ``update_or_create()`` should specify
|
| 468 |
+
the field in the lookup with ``create_defaults__exact``.
|
| 469 |
+
|
| 470 |
+
Miscellaneous
|
| 471 |
+
-------------
|
| 472 |
+
|
| 473 |
+
* The ``instance`` argument of the undocumented
|
| 474 |
+
``BaseModelFormSet.save_existing()`` method is renamed to ``obj``.
|
| 475 |
+
|
| 476 |
+
* The undocumented ``django.contrib.admin.helpers.checkbox`` is removed.
|
| 477 |
+
|
| 478 |
+
* Integer fields are now validated as 64-bit integers on SQLite to match the
|
| 479 |
+
behavior of ``sqlite3``.
|
| 480 |
+
|
| 481 |
+
* The undocumented ``Query.annotation_select_mask`` attribute is changed from a
|
| 482 |
+
set of strings to an ordered list of strings.
|
| 483 |
+
|
| 484 |
+
* ``ImageField.update_dimension_fields()`` is no longer called on the
|
| 485 |
+
``post_init`` signal if ``width_field`` and ``height_field`` are not set.
|
| 486 |
+
|
| 487 |
+
* :class:`~django.db.models.functions.Now` database function now uses
|
| 488 |
+
``LOCALTIMESTAMP`` instead of ``CURRENT_TIMESTAMP`` on Oracle.
|
| 489 |
+
|
| 490 |
+
* :attr:`.AdminSite.site_header` is now rendered in a ``<div>`` tag instead of
|
| 491 |
+
``<h1>``. Screen reader users rely on heading elements for navigation within
|
| 492 |
+
a page. Having two ``<h1>`` elements was confusing and the site header wasn't
|
| 493 |
+
helpful as it is repeated on all pages.
|
| 494 |
+
|
| 495 |
+
* On databases without native support for the SQL ``XOR`` operator, ``^`` as
|
| 496 |
+
the exclusive or (``XOR``) operator now returns rows that are matched by an
|
| 497 |
+
odd number of operands rather than exactly one operand. This is consistent
|
| 498 |
+
with the behavior of MySQL, MariaDB, and Python.
|
| 499 |
+
|
| 500 |
+
* The minimum supported version of ``asgiref`` is increased from 3.6.0 to
|
| 501 |
+
3.7.0.
|
| 502 |
+
|
| 503 |
+
* The minimum supported version of ``selenium`` is increased from 3.8.0 to
|
| 504 |
+
4.8.0.
|
| 505 |
+
|
| 506 |
+
* The ``AlreadyRegistered`` and ``NotRegistered`` exceptions are moved from
|
| 507 |
+
``django.contrib.admin.sites`` to ``django.contrib.admin.exceptions``.
|
| 508 |
+
|
| 509 |
+
.. _deprecated-features-5.0:
|
| 510 |
+
|
| 511 |
+
Features deprecated in 5.0
|
| 512 |
+
==========================
|
| 513 |
+
|
| 514 |
+
Miscellaneous
|
| 515 |
+
-------------
|
| 516 |
+
|
| 517 |
+
* The ``DjangoDivFormRenderer`` and ``Jinja2DivFormRenderer`` transitional form
|
| 518 |
+
renderers are deprecated.
|
| 519 |
+
|
| 520 |
+
* Passing positional arguments ``name`` and ``violation_error_message`` to
|
| 521 |
+
:class:`~django.db.models.BaseConstraint` is deprecated in favor of
|
| 522 |
+
keyword-only arguments.
|
| 523 |
+
|
| 524 |
+
* ``request`` is added to the signature of :meth:`.ModelAdmin.lookup_allowed`.
|
| 525 |
+
Support for ``ModelAdmin`` subclasses that do not accept this argument is
|
| 526 |
+
deprecated.
|
| 527 |
+
|
| 528 |
+
* The ``get_joining_columns()`` method of ``ForeignObject`` and
|
| 529 |
+
``ForeignObjectRel`` is deprecated. Starting with Django 6.0,
|
| 530 |
+
``django.db.models.sql.datastructures.Join`` will no longer fallback to
|
| 531 |
+
``get_joining_columns()``. Subclasses should implement
|
| 532 |
+
``get_joining_fields()`` instead.
|
| 533 |
+
|
| 534 |
+
* The ``ForeignObject.get_reverse_joining_columns()`` method is deprecated.
|
| 535 |
+
|
| 536 |
+
* The default scheme for ``forms.URLField`` will change from ``"http"`` to
|
| 537 |
+
``"https"`` in Django 6.0.
|
| 538 |
+
|
| 539 |
+
* Support for calling ``format_html()`` without passing args or kwargs will be
|
| 540 |
+
removed.
|
| 541 |
+
|
| 542 |
+
Features removed in 5.0
|
| 543 |
+
=======================
|
| 544 |
+
|
| 545 |
+
These features have reached the end of their deprecation cycle and are removed
|
| 546 |
+
in Django 5.0.
|
| 547 |
+
|
| 548 |
+
See :ref:`deprecated-features-4.0` for details on these changes, including how
|
| 549 |
+
to remove usage of these features.
|
| 550 |
+
|
| 551 |
+
* The ``SERIALIZE`` test setting is removed.
|
| 552 |
+
|
| 553 |
+
* The undocumented ``django.utils.baseconv`` module is removed.
|
| 554 |
+
|
| 555 |
+
* The undocumented ``django.utils.datetime_safe`` module is removed.
|
| 556 |
+
|
| 557 |
+
* The default value of the ``USE_TZ`` setting is changed from ``False`` to
|
| 558 |
+
``True``.
|
| 559 |
+
|
| 560 |
+
* The default sitemap protocol for sitemaps built outside the context of a
|
| 561 |
+
request is changed from ``'http'`` to ``'https'``.
|
| 562 |
+
|
| 563 |
+
* The ``extra_tests`` argument for ``DiscoverRunner.build_suite()`` and
|
| 564 |
+
``DiscoverRunner.run_tests()`` is removed.
|
| 565 |
+
|
| 566 |
+
* The ``django.contrib.postgres.aggregates.ArrayAgg``, ``JSONBAgg``, and
|
| 567 |
+
``StringAgg`` aggregates no longer return ``[]``, ``[]``, and ``''``,
|
| 568 |
+
respectively, when there are no rows.
|
| 569 |
+
|
| 570 |
+
* The ``USE_L10N`` setting is removed.
|
| 571 |
+
|
| 572 |
+
* The ``USE_DEPRECATED_PYTZ`` transitional setting is removed.
|
| 573 |
+
|
| 574 |
+
* Support for ``pytz`` timezones is removed.
|
| 575 |
+
|
| 576 |
+
* The ``is_dst`` argument is removed from:
|
| 577 |
+
|
| 578 |
+
* ``QuerySet.datetimes()``
|
| 579 |
+
* ``django.utils.timezone.make_aware()``
|
| 580 |
+
* ``django.db.models.functions.Trunc()``
|
| 581 |
+
* ``django.db.models.functions.TruncSecond()``
|
| 582 |
+
* ``django.db.models.functions.TruncMinute()``
|
| 583 |
+
* ``django.db.models.functions.TruncHour()``
|
| 584 |
+
* ``django.db.models.functions.TruncDay()``
|
| 585 |
+
* ``django.db.models.functions.TruncWeek()``
|
| 586 |
+
* ``django.db.models.functions.TruncMonth()``
|
| 587 |
+
* ``django.db.models.functions.TruncQuarter()``
|
| 588 |
+
* ``django.db.models.functions.TruncYear()``
|
| 589 |
+
|
| 590 |
+
* The ``django.contrib.gis.admin.GeoModelAdmin`` and ``OSMGeoAdmin`` classes
|
| 591 |
+
are removed.
|
| 592 |
+
|
| 593 |
+
* The undocumented ``BaseForm._html_output()`` method is removed.
|
| 594 |
+
|
| 595 |
+
* The ability to return a ``str``, rather than a ``SafeString``, when rendering
|
| 596 |
+
an ``ErrorDict`` and ``ErrorList`` is removed.
|
| 597 |
+
|
| 598 |
+
See :ref:`deprecated-features-4.1` for details on these changes, including how
|
| 599 |
+
to remove usage of these features.
|
| 600 |
+
|
| 601 |
+
* The ``SitemapIndexItem.__str__()`` method is removed.
|
| 602 |
+
|
| 603 |
+
* The ``CSRF_COOKIE_MASKED`` transitional setting is removed.
|
| 604 |
+
|
| 605 |
+
* The ``name`` argument of ``django.utils.functional.cached_property()`` is
|
| 606 |
+
removed.
|
| 607 |
+
|
| 608 |
+
* The ``opclasses`` argument of
|
| 609 |
+
``django.contrib.postgres.constraints.ExclusionConstraint`` is removed.
|
| 610 |
+
|
| 611 |
+
* The undocumented ability to pass ``errors=None`` to
|
| 612 |
+
``SimpleTestCase.assertFormError()`` and ``assertFormsetError()`` is removed.
|
| 613 |
+
|
| 614 |
+
* ``django.contrib.sessions.serializers.PickleSerializer`` is removed.
|
| 615 |
+
|
| 616 |
+
* The usage of ``QuerySet.iterator()`` on a queryset that prefetches related
|
| 617 |
+
objects without providing the ``chunk_size`` argument is no longer allowed.
|
| 618 |
+
|
| 619 |
+
* Passing unsaved model instances to related filters is no longer allowed.
|
| 620 |
+
|
| 621 |
+
* ``created=True`` is required in the signature of
|
| 622 |
+
``RemoteUserBackend.configure_user()`` subclasses.
|
| 623 |
+
|
| 624 |
+
* Support for logging out via ``GET`` requests in the
|
| 625 |
+
``django.contrib.auth.views.LogoutView`` and
|
| 626 |
+
``django.contrib.auth.views.logout_then_login()`` is removed.
|
| 627 |
+
|
| 628 |
+
* The ``django.utils.timezone.utc`` alias to ``datetime.timezone.utc`` is
|
| 629 |
+
removed.
|
| 630 |
+
|
| 631 |
+
* Passing a response object and a form/formset name to
|
| 632 |
+
``SimpleTestCase.assertFormError()`` and ``assertFormSetError()`` is no
|
| 633 |
+
longer allowed.
|
| 634 |
+
|
| 635 |
+
* The ``django.contrib.gis.admin.OpenLayersWidget`` is removed.
|
| 636 |
+
|
| 637 |
+
+ The ``django.contrib.auth.hashers.CryptPasswordHasher`` is removed.
|
| 638 |
+
|
| 639 |
+
* The ``"django/forms/default.html"`` and
|
| 640 |
+
``"django/forms/formsets/default.html"`` templates are removed.
|
| 641 |
+
|
| 642 |
+
* The default form and formset rendering style is changed to the div-based.
|
| 643 |
+
|
| 644 |
+
* Passing ``nulls_first=False`` or ``nulls_last=False`` to ``Expression.asc()``
|
| 645 |
+
and ``Expression.desc()`` methods, and the ``OrderBy`` expression is no
|
| 646 |
+
longer allowed.
|
testbed/django__django/docs/releases/index.txt
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
=============
|
| 2 |
+
Release notes
|
| 3 |
+
=============
|
| 4 |
+
|
| 5 |
+
Release notes for the official Django releases. Each release note will tell you
|
| 6 |
+
what's new in each version, and will also describe any backwards-incompatible
|
| 7 |
+
changes made in that version.
|
| 8 |
+
|
| 9 |
+
For those :doc:`upgrading to a new version of Django</howto/upgrade-version>`,
|
| 10 |
+
you will need to check all the backwards-incompatible changes and
|
| 11 |
+
:doc:`deprecated features</internals/deprecation>` for each 'final' release
|
| 12 |
+
from the one after your current Django version, up to and including the new
|
| 13 |
+
version.
|
| 14 |
+
|
| 15 |
+
Final releases
|
| 16 |
+
==============
|
| 17 |
+
|
| 18 |
+
Below are release notes through Django |version| and its patch releases. Newer
|
| 19 |
+
versions of the documentation contain the release notes for any later releases.
|
| 20 |
+
|
| 21 |
+
.. _development_release_notes:
|
| 22 |
+
|
| 23 |
+
5.0 release
|
| 24 |
+
-----------
|
| 25 |
+
.. toctree::
|
| 26 |
+
:maxdepth: 1
|
| 27 |
+
|
| 28 |
+
5.0
|
| 29 |
+
|
| 30 |
+
4.2 release
|
| 31 |
+
-----------
|
| 32 |
+
|
| 33 |
+
.. toctree::
|
| 34 |
+
:maxdepth: 1
|
| 35 |
+
|
| 36 |
+
4.2.4
|
| 37 |
+
4.2.3
|
| 38 |
+
4.2.2
|
| 39 |
+
4.2.1
|
| 40 |
+
4.2
|
| 41 |
+
|
| 42 |
+
4.1 release
|
| 43 |
+
-----------
|
| 44 |
+
.. toctree::
|
| 45 |
+
:maxdepth: 1
|
| 46 |
+
|
| 47 |
+
4.1.10
|
| 48 |
+
4.1.9
|
| 49 |
+
4.1.8
|
| 50 |
+
4.1.7
|
| 51 |
+
4.1.6
|
| 52 |
+
4.1.5
|
| 53 |
+
4.1.4
|
| 54 |
+
4.1.3
|
| 55 |
+
4.1.2
|
| 56 |
+
4.1.1
|
| 57 |
+
4.1
|
| 58 |
+
|
| 59 |
+
4.0 release
|
| 60 |
+
-----------
|
| 61 |
+
.. toctree::
|
| 62 |
+
:maxdepth: 1
|
| 63 |
+
|
| 64 |
+
4.0.10
|
| 65 |
+
4.0.9
|
| 66 |
+
4.0.8
|
| 67 |
+
4.0.7
|
| 68 |
+
4.0.6
|
| 69 |
+
4.0.5
|
| 70 |
+
4.0.4
|
| 71 |
+
4.0.3
|
| 72 |
+
4.0.2
|
| 73 |
+
4.0.1
|
| 74 |
+
4.0
|
| 75 |
+
|
| 76 |
+
3.2 release
|
| 77 |
+
-----------
|
| 78 |
+
.. toctree::
|
| 79 |
+
:maxdepth: 1
|
| 80 |
+
|
| 81 |
+
3.2.20
|
| 82 |
+
3.2.19
|
| 83 |
+
3.2.18
|
| 84 |
+
3.2.17
|
| 85 |
+
3.2.16
|
| 86 |
+
3.2.15
|
| 87 |
+
3.2.14
|
| 88 |
+
3.2.13
|
| 89 |
+
3.2.12
|
| 90 |
+
3.2.11
|
| 91 |
+
3.2.10
|
| 92 |
+
3.2.9
|
| 93 |
+
3.2.8
|
| 94 |
+
3.2.7
|
| 95 |
+
3.2.6
|
| 96 |
+
3.2.5
|
| 97 |
+
3.2.4
|
| 98 |
+
3.2.3
|
| 99 |
+
3.2.2
|
| 100 |
+
3.2.1
|
| 101 |
+
3.2
|
| 102 |
+
|
| 103 |
+
3.1 release
|
| 104 |
+
-----------
|
| 105 |
+
.. toctree::
|
| 106 |
+
:maxdepth: 1
|
| 107 |
+
|
| 108 |
+
3.1.14
|
| 109 |
+
3.1.13
|
| 110 |
+
3.1.12
|
| 111 |
+
3.1.11
|
| 112 |
+
3.1.10
|
| 113 |
+
3.1.9
|
| 114 |
+
3.1.8
|
| 115 |
+
3.1.7
|
| 116 |
+
3.1.6
|
| 117 |
+
3.1.5
|
| 118 |
+
3.1.4
|
| 119 |
+
3.1.3
|
| 120 |
+
3.1.2
|
| 121 |
+
3.1.1
|
| 122 |
+
3.1
|
| 123 |
+
|
| 124 |
+
3.0 release
|
| 125 |
+
-----------
|
| 126 |
+
.. toctree::
|
| 127 |
+
:maxdepth: 1
|
| 128 |
+
|
| 129 |
+
3.0.14
|
| 130 |
+
3.0.13
|
| 131 |
+
3.0.12
|
| 132 |
+
3.0.11
|
| 133 |
+
3.0.10
|
| 134 |
+
3.0.9
|
| 135 |
+
3.0.8
|
| 136 |
+
3.0.7
|
| 137 |
+
3.0.6
|
| 138 |
+
3.0.5
|
| 139 |
+
3.0.4
|
| 140 |
+
3.0.3
|
| 141 |
+
3.0.2
|
| 142 |
+
3.0.1
|
| 143 |
+
3.0
|
| 144 |
+
|
| 145 |
+
2.2 release
|
| 146 |
+
-----------
|
| 147 |
+
.. toctree::
|
| 148 |
+
:maxdepth: 1
|
| 149 |
+
|
| 150 |
+
2.2.28
|
| 151 |
+
2.2.27
|
| 152 |
+
2.2.26
|
| 153 |
+
2.2.25
|
| 154 |
+
2.2.24
|
| 155 |
+
2.2.23
|
| 156 |
+
2.2.22
|
| 157 |
+
2.2.21
|
| 158 |
+
2.2.20
|
| 159 |
+
2.2.19
|
| 160 |
+
2.2.18
|
| 161 |
+
2.2.17
|
| 162 |
+
2.2.16
|
| 163 |
+
2.2.15
|
| 164 |
+
2.2.14
|
| 165 |
+
2.2.13
|
| 166 |
+
2.2.12
|
| 167 |
+
2.2.11
|
| 168 |
+
2.2.10
|
| 169 |
+
2.2.9
|
| 170 |
+
2.2.8
|
| 171 |
+
2.2.7
|
| 172 |
+
2.2.6
|
| 173 |
+
2.2.5
|
| 174 |
+
2.2.4
|
| 175 |
+
2.2.3
|
| 176 |
+
2.2.2
|
| 177 |
+
2.2.1
|
| 178 |
+
2.2
|
| 179 |
+
|
| 180 |
+
2.1 release
|
| 181 |
+
-----------
|
| 182 |
+
.. toctree::
|
| 183 |
+
:maxdepth: 1
|
| 184 |
+
|
| 185 |
+
2.1.15
|
| 186 |
+
2.1.14
|
| 187 |
+
2.1.13
|
| 188 |
+
2.1.12
|
| 189 |
+
2.1.11
|
| 190 |
+
2.1.10
|
| 191 |
+
2.1.9
|
| 192 |
+
2.1.8
|
| 193 |
+
2.1.7
|
| 194 |
+
2.1.6
|
| 195 |
+
2.1.5
|
| 196 |
+
2.1.4
|
| 197 |
+
2.1.3
|
| 198 |
+
2.1.2
|
| 199 |
+
2.1.1
|
| 200 |
+
2.1
|
| 201 |
+
|
| 202 |
+
2.0 release
|
| 203 |
+
-----------
|
| 204 |
+
.. toctree::
|
| 205 |
+
:maxdepth: 1
|
| 206 |
+
|
| 207 |
+
2.0.13
|
| 208 |
+
2.0.12
|
| 209 |
+
2.0.11
|
| 210 |
+
2.0.10
|
| 211 |
+
2.0.9
|
| 212 |
+
2.0.8
|
| 213 |
+
2.0.7
|
| 214 |
+
2.0.6
|
| 215 |
+
2.0.5
|
| 216 |
+
2.0.4
|
| 217 |
+
2.0.3
|
| 218 |
+
2.0.2
|
| 219 |
+
2.0.1
|
| 220 |
+
2.0
|
| 221 |
+
|
| 222 |
+
1.11 release
|
| 223 |
+
------------
|
| 224 |
+
.. toctree::
|
| 225 |
+
:maxdepth: 1
|
| 226 |
+
|
| 227 |
+
1.11.29
|
| 228 |
+
1.11.28
|
| 229 |
+
1.11.27
|
| 230 |
+
1.11.26
|
| 231 |
+
1.11.25
|
| 232 |
+
1.11.24
|
| 233 |
+
1.11.23
|
| 234 |
+
1.11.22
|
| 235 |
+
1.11.21
|
| 236 |
+
1.11.20
|
| 237 |
+
1.11.19
|
| 238 |
+
1.11.18
|
| 239 |
+
1.11.17
|
| 240 |
+
1.11.16
|
| 241 |
+
1.11.15
|
| 242 |
+
1.11.14
|
| 243 |
+
1.11.13
|
| 244 |
+
1.11.12
|
| 245 |
+
1.11.11
|
| 246 |
+
1.11.10
|
| 247 |
+
1.11.9
|
| 248 |
+
1.11.8
|
| 249 |
+
1.11.7
|
| 250 |
+
1.11.6
|
| 251 |
+
1.11.5
|
| 252 |
+
1.11.4
|
| 253 |
+
1.11.3
|
| 254 |
+
1.11.2
|
| 255 |
+
1.11.1
|
| 256 |
+
1.11
|
| 257 |
+
|
| 258 |
+
1.10 release
|
| 259 |
+
------------
|
| 260 |
+
.. toctree::
|
| 261 |
+
:maxdepth: 1
|
| 262 |
+
|
| 263 |
+
1.10.8
|
| 264 |
+
1.10.7
|
| 265 |
+
1.10.6
|
| 266 |
+
1.10.5
|
| 267 |
+
1.10.4
|
| 268 |
+
1.10.3
|
| 269 |
+
1.10.2
|
| 270 |
+
1.10.1
|
| 271 |
+
1.10
|
| 272 |
+
|
| 273 |
+
1.9 release
|
| 274 |
+
-----------
|
| 275 |
+
.. toctree::
|
| 276 |
+
:maxdepth: 1
|
| 277 |
+
|
| 278 |
+
1.9.13
|
| 279 |
+
1.9.12
|
| 280 |
+
1.9.11
|
| 281 |
+
1.9.10
|
| 282 |
+
1.9.9
|
| 283 |
+
1.9.8
|
| 284 |
+
1.9.7
|
| 285 |
+
1.9.6
|
| 286 |
+
1.9.5
|
| 287 |
+
1.9.4
|
| 288 |
+
1.9.3
|
| 289 |
+
1.9.2
|
| 290 |
+
1.9.1
|
| 291 |
+
1.9
|
| 292 |
+
|
| 293 |
+
1.8 release
|
| 294 |
+
-----------
|
| 295 |
+
.. toctree::
|
| 296 |
+
:maxdepth: 1
|
| 297 |
+
|
| 298 |
+
1.8.19
|
| 299 |
+
1.8.18
|
| 300 |
+
1.8.17
|
| 301 |
+
1.8.16
|
| 302 |
+
1.8.15
|
| 303 |
+
1.8.14
|
| 304 |
+
1.8.13
|
| 305 |
+
1.8.12
|
| 306 |
+
1.8.11
|
| 307 |
+
1.8.10
|
| 308 |
+
1.8.9
|
| 309 |
+
1.8.8
|
| 310 |
+
1.8.7
|
| 311 |
+
1.8.6
|
| 312 |
+
1.8.5
|
| 313 |
+
1.8.4
|
| 314 |
+
1.8.3
|
| 315 |
+
1.8.2
|
| 316 |
+
1.8.1
|
| 317 |
+
1.8
|
| 318 |
+
|
| 319 |
+
1.7 release
|
| 320 |
+
-----------
|
| 321 |
+
.. toctree::
|
| 322 |
+
:maxdepth: 1
|
| 323 |
+
|
| 324 |
+
1.7.11
|
| 325 |
+
1.7.10
|
| 326 |
+
1.7.9
|
| 327 |
+
1.7.8
|
| 328 |
+
1.7.7
|
| 329 |
+
1.7.6
|
| 330 |
+
1.7.5
|
| 331 |
+
1.7.4
|
| 332 |
+
1.7.3
|
| 333 |
+
1.7.2
|
| 334 |
+
1.7.1
|
| 335 |
+
1.7
|
| 336 |
+
|
| 337 |
+
1.6 release
|
| 338 |
+
-----------
|
| 339 |
+
.. toctree::
|
| 340 |
+
:maxdepth: 1
|
| 341 |
+
|
| 342 |
+
1.6.11
|
| 343 |
+
1.6.10
|
| 344 |
+
1.6.9
|
| 345 |
+
1.6.8
|
| 346 |
+
1.6.7
|
| 347 |
+
1.6.6
|
| 348 |
+
1.6.5
|
| 349 |
+
1.6.4
|
| 350 |
+
1.6.3
|
| 351 |
+
1.6.2
|
| 352 |
+
1.6.1
|
| 353 |
+
1.6
|
| 354 |
+
|
| 355 |
+
1.5 release
|
| 356 |
+
-----------
|
| 357 |
+
.. toctree::
|
| 358 |
+
:maxdepth: 1
|
| 359 |
+
|
| 360 |
+
1.5.12
|
| 361 |
+
1.5.11
|
| 362 |
+
1.5.10
|
| 363 |
+
1.5.9
|
| 364 |
+
1.5.8
|
| 365 |
+
1.5.7
|
| 366 |
+
1.5.6
|
| 367 |
+
1.5.5
|
| 368 |
+
1.5.4
|
| 369 |
+
1.5.3
|
| 370 |
+
1.5.2
|
| 371 |
+
1.5.1
|
| 372 |
+
1.5
|
| 373 |
+
|
| 374 |
+
1.4 release
|
| 375 |
+
-----------
|
| 376 |
+
.. toctree::
|
| 377 |
+
:maxdepth: 1
|
| 378 |
+
|
| 379 |
+
1.4.22
|
| 380 |
+
1.4.21
|
| 381 |
+
1.4.20
|
| 382 |
+
1.4.19
|
| 383 |
+
1.4.18
|
| 384 |
+
1.4.17
|
| 385 |
+
1.4.16
|
| 386 |
+
1.4.15
|
| 387 |
+
1.4.14
|
| 388 |
+
1.4.13
|
| 389 |
+
1.4.12
|
| 390 |
+
1.4.11
|
| 391 |
+
1.4.10
|
| 392 |
+
1.4.9
|
| 393 |
+
1.4.8
|
| 394 |
+
1.4.7
|
| 395 |
+
1.4.6
|
| 396 |
+
1.4.5
|
| 397 |
+
1.4.4
|
| 398 |
+
1.4.3
|
| 399 |
+
1.4.2
|
| 400 |
+
1.4.1
|
| 401 |
+
1.4
|
| 402 |
+
|
| 403 |
+
1.3 release
|
| 404 |
+
-----------
|
| 405 |
+
.. toctree::
|
| 406 |
+
:maxdepth: 1
|
| 407 |
+
|
| 408 |
+
1.3.7
|
| 409 |
+
1.3.6
|
| 410 |
+
1.3.5
|
| 411 |
+
1.3.4
|
| 412 |
+
1.3.3
|
| 413 |
+
1.3.2
|
| 414 |
+
1.3.1
|
| 415 |
+
1.3
|
| 416 |
+
|
| 417 |
+
1.2 release
|
| 418 |
+
-----------
|
| 419 |
+
.. toctree::
|
| 420 |
+
:maxdepth: 1
|
| 421 |
+
|
| 422 |
+
1.2.7
|
| 423 |
+
1.2.6
|
| 424 |
+
1.2.5
|
| 425 |
+
1.2.4
|
| 426 |
+
1.2.3
|
| 427 |
+
1.2.2
|
| 428 |
+
1.2.1
|
| 429 |
+
1.2
|
| 430 |
+
|
| 431 |
+
1.1 release
|
| 432 |
+
-----------
|
| 433 |
+
.. toctree::
|
| 434 |
+
:maxdepth: 1
|
| 435 |
+
|
| 436 |
+
1.1.4
|
| 437 |
+
1.1.3
|
| 438 |
+
1.1.2
|
| 439 |
+
1.1
|
| 440 |
+
|
| 441 |
+
1.0 release
|
| 442 |
+
-----------
|
| 443 |
+
.. toctree::
|
| 444 |
+
:maxdepth: 1
|
| 445 |
+
|
| 446 |
+
1.0.2
|
| 447 |
+
1.0.1
|
| 448 |
+
1.0
|
| 449 |
+
|
| 450 |
+
Pre-1.0 releases
|
| 451 |
+
----------------
|
| 452 |
+
.. toctree::
|
| 453 |
+
:maxdepth: 1
|
| 454 |
+
|
| 455 |
+
0.96
|
| 456 |
+
0.95
|
| 457 |
+
|
| 458 |
+
Security releases
|
| 459 |
+
=================
|
| 460 |
+
|
| 461 |
+
Whenever a security issue is disclosed via :doc:`Django's security
|
| 462 |
+
policies </internals/security>`, appropriate release notes are now
|
| 463 |
+
added to all affected release series.
|
| 464 |
+
|
| 465 |
+
Additionally, :doc:`an archive of disclosed security issues
|
| 466 |
+
</releases/security>` is maintained.
|
| 467 |
+
|
| 468 |
+
.. toctree::
|
| 469 |
+
:hidden:
|
| 470 |
+
|
| 471 |
+
security
|
testbed/django__django/docs/releases/security.txt
ADDED
|
@@ -0,0 +1,1422 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
+
==========================
|
| 2 |
+
Archive of security issues
|
| 3 |
+
==========================
|
| 4 |
+
|
| 5 |
+
Django's development team is strongly committed to responsible
|
| 6 |
+
reporting and disclosure of security-related issues, as outlined in
|
| 7 |
+
:doc:`Django's security policies </internals/security>`.
|
| 8 |
+
|
| 9 |
+
As part of that commitment, we maintain the following historical list
|
| 10 |
+
of issues which have been fixed and disclosed. For each issue, the
|
| 11 |
+
list below includes the date, a brief description, the `CVE identifier
|
| 12 |
+
<https://en.wikipedia.org/wiki/Common_Vulnerabilities_and_Exposures>`_
|
| 13 |
+
if applicable, a list of affected versions, a link to the full
|
| 14 |
+
disclosure and links to the appropriate patch(es).
|
| 15 |
+
|
| 16 |
+
Some important caveats apply to this information:
|
| 17 |
+
|
| 18 |
+
* Lists of affected versions include only those versions of Django
|
| 19 |
+
which had stable, security-supported releases at the time of
|
| 20 |
+
disclosure. This means older versions (whose security support had
|
| 21 |
+
expired) and versions which were in pre-release (alpha/beta/RC)
|
| 22 |
+
states at the time of disclosure may have been affected, but are not
|
| 23 |
+
listed.
|
| 24 |
+
|
| 25 |
+
* The Django project has on occasion issued security advisories,
|
| 26 |
+
pointing out potential security problems which can arise from
|
| 27 |
+
improper configuration or from other issues outside of Django
|
| 28 |
+
itself. Some of these advisories have received CVEs; when that is
|
| 29 |
+
the case, they are listed here, but as they have no accompanying
|
| 30 |
+
patches or releases, only the description, disclosure and CVE will
|
| 31 |
+
be listed.
|
| 32 |
+
|
| 33 |
+
Issues under Django's security process
|
| 34 |
+
======================================
|
| 35 |
+
|
| 36 |
+
All security issues have been handled under versions of Django's security
|
| 37 |
+
process. These are listed below.
|
| 38 |
+
|
| 39 |
+
July 3, 2023 - :cve:`2023-36053`
|
| 40 |
+
--------------------------------
|
| 41 |
+
|
| 42 |
+
Potential regular expression denial of service vulnerability in
|
| 43 |
+
``EmailValidator``/``URLValidator``. `Full description
|
| 44 |
+
<https://www.djangoproject.com/weblog/2023/jul/03/security-releases/>`__
|
| 45 |
+
|
| 46 |
+
* Django 4.2 :commit:`(patch) <b7c5feb35a31799de6e582ad6a5a91a9de74e0f9>`
|
| 47 |
+
* Django 4.1 :commit:`(patch) <beb3f3d55940d9aa7198bf9d424ab74e873aec3d>`
|
| 48 |
+
* Django 3.2 :commit:`(patch) <454f2fb93437f98917283336201b4048293f7582>`
|
| 49 |
+
|
| 50 |
+
May 3, 2023 - :cve:`2023-31047`
|
| 51 |
+
-------------------------------
|
| 52 |
+
|
| 53 |
+
Potential bypass of validation when uploading multiple files using one form
|
| 54 |
+
field. `Full description
|
| 55 |
+
<https://www.djangoproject.com/weblog/2023/may/03/security-releases/>`__
|
| 56 |
+
|
| 57 |
+
* Django 4.2 :commit:`(patch) <21b1b1fc03e5f9e9f8c977ee6e35618dd3b353dd>`
|
| 58 |
+
* Django 4.1 :commit:`(patch) <e7c3a2ccc3a562328600be05068ed9149e12ce64>`
|
| 59 |
+
* Django 3.2 :commit:`(patch) <eed53d0011622e70b936e203005f0e6f4ac48965>`
|
| 60 |
+
|
| 61 |
+
February 14, 2023 - :cve:`2023-24580`
|
| 62 |
+
-------------------------------------
|
| 63 |
+
|
| 64 |
+
Potential denial-of-service vulnerability in file uploads. `Full description
|
| 65 |
+
<https://www.djangoproject.com/weblog/2023/feb/14/security-releases/>`__
|
| 66 |
+
|
| 67 |
+
* Django 4.1 :commit:`(patch) <628b33a854a9c68ec8a0c51f382f304a0044ec92>`
|
| 68 |
+
* Django 4.0 :commit:`(patch) <83f1ea83e4553e211c1c5a0dfc197b66d4e50432>`
|
| 69 |
+
* Django 3.2 :commit:`(patch) <a665ed5179f5bbd3db95ce67286d0192eff041d8>`
|
| 70 |
+
|
| 71 |
+
February 1, 2023 - :cve:`2023-23969`
|
| 72 |
+
------------------------------------
|
| 73 |
+
|
| 74 |
+
Potential denial-of-service via ``Accept-Language`` headers. `Full description
|
| 75 |
+
<https://www.djangoproject.com/weblog/2023/feb/01/security-releases/>`__
|
| 76 |
+
|
| 77 |
+
* Django 4.1 :commit:`(patch) <9d7bd5a56b1ce0576e8e07a8001373576d277942>`
|
| 78 |
+
* Django 4.0 :commit:`(patch) <4452642f193533e288a52c02efb5bbc766a68f95>`
|
| 79 |
+
* Django 3.2 :commit:`(patch) <c7e0151fdf33e1b11d488b6f67b94fdf3a30614a>`
|
| 80 |
+
|
| 81 |
+
October 4, 2022 - :cve:`2022-41323`
|
| 82 |
+
-----------------------------------
|
| 83 |
+
|
| 84 |
+
Potential denial-of-service vulnerability in internationalized URLs. `Full
|
| 85 |
+
description
|
| 86 |
+
<https://www.djangoproject.com/weblog/2022/oct/04/security-releases/>`__
|
| 87 |
+
|
| 88 |
+
* Django 4.1 :commit:`(patch) <9d656ea51d9ea7105c0c0785783ac29d426a7d25>`
|
| 89 |
+
* Django 4.0 :commit:`(patch) <23f0093125ac2e553da6c1b2f9988eb6a3dd2ea1>`
|
| 90 |
+
* Django 3.2 :commit:`(patch) <5b6b257fa7ec37ff27965358800c67e2dd11c924>`
|
| 91 |
+
|
| 92 |
+
August 3, 2022 - :cve:`2022-36359`
|
| 93 |
+
----------------------------------
|
| 94 |
+
|
| 95 |
+
Potential reflected file download vulnerability in FileResponse. `Full
|
| 96 |
+
description
|
| 97 |
+
<https://www.djangoproject.com/weblog/2022/aug/03/security-releases/>`__
|
| 98 |
+
|
| 99 |
+
* Django 4.0 :commit:`(patch) <b7d9529cbe0af4adabb6ea5d01ed8dcce3668fb3>`
|
| 100 |
+
* Django 3.2 :commit:`(patch) <b3e4494d759202a3b6bf247fd34455bf13be5b80>`
|
| 101 |
+
|
| 102 |
+
July 4, 2022 - :cve:`2022-34265`
|
| 103 |
+
--------------------------------
|
| 104 |
+
|
| 105 |
+
Potential SQL injection via ``Trunc(kind)`` and ``Extract(lookup_name)``
|
| 106 |
+
arguments. `Full description
|
| 107 |
+
<https://www.djangoproject.com/weblog/2022/jul/04/security-releases/>`__
|
| 108 |
+
|
| 109 |
+
* Django 4.0 :commit:`(patch) <0dc9c016fadb71a067e5a42be30164e3f96c0492>`
|
| 110 |
+
* Django 3.2 :commit:`(patch) <a9010fe5555e6086a9d9ae50069579400ef0685e>`
|
| 111 |
+
|
| 112 |
+
April 11, 2022 - :cve:`2022-28346`
|
| 113 |
+
----------------------------------
|
| 114 |
+
|
| 115 |
+
Potential SQL injection in ``QuerySet.annotate()``, ``aggregate()``, and
|
| 116 |
+
``extra()``. `Full description
|
| 117 |
+
<https://www.djangoproject.com/weblog/2022/apr/11/security-releases/>`__
|
| 118 |
+
|
| 119 |
+
* Django 4.0 :commit:`(patch) <800828887a0509ad1162d6d407e94d8de7eafc60>`
|
| 120 |
+
* Django 3.2 :commit:`(patch) <2044dac5c6968441be6f534c4139bcf48c5c7e48>`
|
| 121 |
+
* Django 2.2 :commit:`(patch) <2c09e68ec911919360d5f8502cefc312f9e03c5d>`
|
| 122 |
+
|
| 123 |
+
April 11, 2022 - :cve:`2022-28347`
|
| 124 |
+
----------------------------------
|
| 125 |
+
|
| 126 |
+
Potential SQL injection via ``QuerySet.explain(**options)`` on PostgreSQL.
|
| 127 |
+
`Full description
|
| 128 |
+
<https://www.djangoproject.com/weblog/2022/apr/11/security-releases/>`__
|
| 129 |
+
|
| 130 |
+
* Django 4.0 :commit:`(patch) <00b0fc50e1738c7174c495464a5ef069408a4402>`
|
| 131 |
+
* Django 3.2 :commit:`(patch) <9e19accb6e0a00ba77d5a95a91675bf18877c72d>`
|
| 132 |
+
* Django 2.2 :commit:`(patch) <29a6c98b4c13af82064f993f0acc6e8fafa4d3f5>`
|
| 133 |
+
|
| 134 |
+
February 1, 2022 - :cve:`2022-22818`
|
| 135 |
+
------------------------------------
|
| 136 |
+
|
| 137 |
+
Possible XSS via ``{% debug %}`` template tag. `Full description
|
| 138 |
+
<https://www.djangoproject.com/weblog/2022/feb/01/security-releases/>`__
|
| 139 |
+
|
| 140 |
+
Versions affected
|
| 141 |
+
~~~~~~~~~~~~~~~~~
|
| 142 |
+
|
| 143 |
+
* Django 4.0 :commit:`(patch) <01422046065d2b51f8f613409cad2c81b39487e5>`
|
| 144 |
+
* Django 3.2 :commit:`(patch) <1a1e8278c46418bde24c86a65443b0674bae65e2>`
|
| 145 |
+
* Django 2.2 :commit:`(patch) <c27a7eb9f40b64990398978152e62b6ff839c2e6>`
|
| 146 |
+
|
| 147 |
+
February 1, 2022 - :cve:`2022-23833`
|
| 148 |
+
------------------------------------
|
| 149 |
+
|
| 150 |
+
Denial-of-service possibility in file uploads. `Full description
|
| 151 |
+
<https://www.djangoproject.com/weblog/2022/feb/01/security-releases/>`__
|
| 152 |
+
|
| 153 |
+
Versions affected
|
| 154 |
+
~~~~~~~~~~~~~~~~~
|
| 155 |
+
|
| 156 |
+
* Django 4.0 :commit:`(patch) <f9c7d48fdd6f198a6494a9202f90242f176e4fc9>`
|
| 157 |
+
* Django 3.2 :commit:`(patch) <d16133568ef9c9b42cb7a08bdf9ff3feec2e5468>`
|
| 158 |
+
* Django 2.2 :commit:`(patch) <c477b761804984c932704554ad35f78a2e230c6a>`
|
| 159 |
+
|
| 160 |
+
January 4, 2022 - :cve:`2021-45452`
|
| 161 |
+
------------------------------------
|
| 162 |
+
|
| 163 |
+
Potential directory-traversal via ``Storage.save()``. `Full description
|
| 164 |
+
<https://www.djangoproject.com/weblog/2022/jan/04/security-releases/>`__
|
| 165 |
+
|
| 166 |
+
Versions affected
|
| 167 |
+
~~~~~~~~~~~~~~~~~
|
| 168 |
+
|
| 169 |
+
* Django 4.0 :commit:`(patch) <e1592e0f26302e79856cc7f2218ae848ae19b0f6>`
|
| 170 |
+
* Django 3.2 :commit:`(patch) <8d2f7cff76200cbd2337b2cf1707e383eb1fb54b>`
|
| 171 |
+
* Django 2.2 :commit:`(patch) <4cb35b384ceef52123fc66411a73c36a706825e1>`
|
| 172 |
+
|
| 173 |
+
January 4, 2022 - :cve:`2021-45116`
|
| 174 |
+
------------------------------------
|
| 175 |
+
|
| 176 |
+
Potential information disclosure in ``dictsort`` template filter. `Full
|
| 177 |
+
description
|
| 178 |
+
<https://www.djangoproject.com/weblog/2022/jan/04/security-releases/>`__
|
| 179 |
+
|
| 180 |
+
Versions affected
|
| 181 |
+
~~~~~~~~~~~~~~~~~
|
| 182 |
+
|
| 183 |
+
* Django 4.0 :commit:`(patch) <2a8ec7f546d6d5806e221ec948c5146b55bd7489>`
|
| 184 |
+
* Django 3.2 :commit:`(patch) <c7fe895bca06daf12cc1670b56eaf72a1ef27a16>`
|
| 185 |
+
* Django 2.2 :commit:`(patch) <c9f648ccfac5ab90fb2829a66da4f77e68c7f93a>`
|
| 186 |
+
|
| 187 |
+
January 4, 2022 - :cve:`2021-45115`
|
| 188 |
+
------------------------------------
|
| 189 |
+
|
| 190 |
+
Denial-of-service possibility in ``UserAttributeSimilarityValidator``. `Full
|
| 191 |
+
description
|
| 192 |
+
<https://www.djangoproject.com/weblog/2022/jan/04/security-releases/>`__
|
| 193 |
+
|
| 194 |
+
Versions affected
|
| 195 |
+
~~~~~~~~~~~~~~~~~
|
| 196 |
+
|
| 197 |
+
* Django 4.0 :commit:`(patch) <df79ef03ac867c93caaa6be56bc69e66abfeef8f>`
|
| 198 |
+
* Django 3.2 :commit:`(patch) <a8b32fe13bcaed1c0b772fdc53de84abc224fb20>`
|
| 199 |
+
* Django 2.2 :commit:`(patch) <2135637fdd5ce994de110affef9e67dffdf77277>`
|
| 200 |
+
|
| 201 |
+
December 7, 2021 - :cve:`2021-44420`
|
| 202 |
+
------------------------------------
|
| 203 |
+
|
| 204 |
+
Potential bypass of an upstream access control based on URL paths. `Full
|
| 205 |
+
description
|
| 206 |
+
<https://www.djangoproject.com/weblog/2021/dec/07/security-releases/>`__
|
| 207 |
+
|
| 208 |
+
Versions affected
|
| 209 |
+
~~~~~~~~~~~~~~~~~
|
| 210 |
+
|
| 211 |
+
* Django 3.2 :commit:`(patch) <333c65603032c377e682cdbd7388657a5463a05a>`
|
| 212 |
+
* Django 3.1 :commit:`(patch) <22bd17488159601bf0741b70ae7932bffea8eced>`
|
| 213 |
+
* Django 2.2 :commit:`(patch) <7cf7d74e8a754446eeb85cacf2fef1247e0cb6d7>`
|
| 214 |
+
|
| 215 |
+
July 1, 2021 - :cve:`2021-35042`
|
| 216 |
+
--------------------------------
|
| 217 |
+
|
| 218 |
+
Potential SQL injection via unsanitized ``QuerySet.order_by()`` input. `Full
|
| 219 |
+
description
|
| 220 |
+
<https://www.djangoproject.com/weblog/2021/jul/01/security-releases/>`__
|
| 221 |
+
|
| 222 |
+
Versions affected
|
| 223 |
+
~~~~~~~~~~~~~~~~~
|
| 224 |
+
|
| 225 |
+
* Django 3.2 :commit:`(patch) <a34a5f724c5d5adb2109374ba3989ebb7b11f81f>`
|
| 226 |
+
* Django 3.1 :commit:`(patch) <0bd57a879a0d54920bb9038a732645fb917040e9>`
|
| 227 |
+
|
| 228 |
+
June 2, 2021 - :cve:`2021-33203`
|
| 229 |
+
--------------------------------
|
| 230 |
+
|
| 231 |
+
Potential directory traversal via ``admindocs``. `Full description
|
| 232 |
+
<https://www.djangoproject.com/weblog/2021/jun/02/security-releases/>`__
|
| 233 |
+
|
| 234 |
+
Versions affected
|
| 235 |
+
~~~~~~~~~~~~~~~~~
|
| 236 |
+
|
| 237 |
+
* Django 3.2 :commit:`(patch) <dfaba12cda060b8b292ae1d271b44bf810b1c5b9>`
|
| 238 |
+
* Django 3.1 :commit:`(patch) <20c67a0693c4ede2b09af02574823485e82e4c8f>`
|
| 239 |
+
* Django 2.2 :commit:`(patch) <053cc9534d174dc89daba36724ed2dcb36755b90>`
|
| 240 |
+
|
| 241 |
+
June 2, 2021 - :cve:`2021-33571`
|
| 242 |
+
--------------------------------
|
| 243 |
+
|
| 244 |
+
Possible indeterminate SSRF, RFI, and LFI attacks since validators accepted
|
| 245 |
+
leading zeros in IPv4 addresses. `Full description
|
| 246 |
+
<https://www.djangoproject.com/weblog/2021/jun/02/security-releases/>`__
|
| 247 |
+
|
| 248 |
+
Versions affected
|
| 249 |
+
~~~~~~~~~~~~~~~~~
|
| 250 |
+
|
| 251 |
+
* Django 3.2 :commit:`(patch) <9f75e2e562fa0c0482f3dde6fc7399a9070b4a3d>`
|
| 252 |
+
* Django 3.1 :commit:`(patch) <203d4ab9ebcd72fc4d6eb7398e66ed9e474e118e>`
|
| 253 |
+
* Django 2.2 :commit:`(patch) <f27c38ab5d90f68c9dd60cabef248a570c0be8fc>`
|
| 254 |
+
|
| 255 |
+
May 6, 2021 - :cve:`2021-32052`
|
| 256 |
+
-------------------------------
|
| 257 |
+
|
| 258 |
+
Header injection possibility since ``URLValidator`` accepted newlines in input
|
| 259 |
+
on Python 3.9.5+. `Full description
|
| 260 |
+
<https://www.djangoproject.com/weblog/2021/may/06/security-releases/>`__
|
| 261 |
+
|
| 262 |
+
Versions affected
|
| 263 |
+
~~~~~~~~~~~~~~~~~
|
| 264 |
+
|
| 265 |
+
* Django 3.2 :commit:`(patch) <2d2c1d0c97832860fbd6597977e2aae17dd7e5b2>`
|
| 266 |
+
* Django 3.1 :commit:`(patch) <afb23f5929944a407e4990edef1c7806a94c9879>`
|
| 267 |
+
* Django 2.2 :commit:`(patch) <d9594c4ea57b6309d93879805302cec9ae9f23ff>`
|
| 268 |
+
|
| 269 |
+
May 4, 2021 - :cve:`2021-31542`
|
| 270 |
+
-------------------------------
|
| 271 |
+
|
| 272 |
+
Potential directory-traversal via uploaded files. `Full description
|
| 273 |
+
<https://www.djangoproject.com/weblog/2021/may/04/security-releases/>`__
|
| 274 |
+
|
| 275 |
+
Versions affected
|
| 276 |
+
~~~~~~~~~~~~~~~~~
|
| 277 |
+
|
| 278 |
+
* Django 3.2 :commit:`(patch) <c98f446c188596d4ba6de71d1b77b4a6c5c2a007>`
|
| 279 |
+
* Django 3.1 :commit:`(patch) <25d84d64122c15050a0ee739e859f22ddab5ac48>`
|
| 280 |
+
* Django 2.2 :commit:`(patch) <04ac1624bdc2fa737188401757cf95ced122d26d>`
|
| 281 |
+
|
| 282 |
+
April 6, 2021 - :cve:`2021-28658`
|
| 283 |
+
---------------------------------
|
| 284 |
+
|
| 285 |
+
Potential directory-traversal via uploaded files. `Full description
|
| 286 |
+
<https://www.djangoproject.com/weblog/2021/apr/06/security-releases/>`__
|
| 287 |
+
|
| 288 |
+
Versions affected
|
| 289 |
+
~~~~~~~~~~~~~~~~~
|
| 290 |
+
|
| 291 |
+
* Django 3.2 :commit:`(patch) <2820fd1be5dfccbf1216c3845fad8580502473e1>`
|
| 292 |
+
* Django 3.1 :commit:`(patch) <cca0d98118cccf9ae0c6dcf2d6c57fc50469fbf0>`
|
| 293 |
+
* Django 3.0 :commit:`(patch) <e7fba62248f604c76da4f23dcf1db4a57b0808ea>`
|
| 294 |
+
* Django 2.2 :commit:`(patch) <4036d62bda0e9e9f6172943794b744a454ca49c2>`
|
| 295 |
+
|
| 296 |
+
February 19, 2021 - :cve:`2021-23336`
|
| 297 |
+
-------------------------------------
|
| 298 |
+
|
| 299 |
+
Web cache poisoning via ``django.utils.http.limited_parse_qsl()``. `Full
|
| 300 |
+
description
|
| 301 |
+
<https://www.djangoproject.com/weblog/2021/feb/19/security-releases/>`__
|
| 302 |
+
|
| 303 |
+
Versions affected
|
| 304 |
+
~~~~~~~~~~~~~~~~~
|
| 305 |
+
|
| 306 |
+
* Django 3.2 :commit:`(patch) <be8237c7cce24b06aabde0b97afce98ddabbe3b6>`
|
| 307 |
+
* Django 3.1 :commit:`(patch) <8f6d431b08cbb418d9144b976e7b972546607851>`
|
| 308 |
+
* Django 3.0 :commit:`(patch) <326a926beef869d3341bc9ef737887f0449b6b71>`
|
| 309 |
+
* Django 2.2 :commit:`(patch) <fd6b6afd5959b638c62dbf4839ccff97e7f7dfda>`
|
| 310 |
+
|
| 311 |
+
February 1, 2021 - :cve:`2021-3281`
|
| 312 |
+
-----------------------------------
|
| 313 |
+
|
| 314 |
+
Potential directory-traversal via ``archive.extract()``. `Full description
|
| 315 |
+
<https://www.djangoproject.com/weblog/2021/feb/01/security-releases/>`__
|
| 316 |
+
|
| 317 |
+
Versions affected
|
| 318 |
+
~~~~~~~~~~~~~~~~~
|
| 319 |
+
|
| 320 |
+
* Django 3.1 :commit:`(patch) <02e6592835b4559909aa3aaaf67988fef435f624>`
|
| 321 |
+
* Django 3.0 :commit:`(patch) <52e409ed17287e9aabda847b6afe58be2fa9f86a>`
|
| 322 |
+
* Django 2.2 :commit:`(patch) <21e7622dec1f8612c85c2fc37fe8efbfd3311e37>`
|
| 323 |
+
|
| 324 |
+
September 1, 2020 - :cve:`2020-24584`
|
| 325 |
+
-------------------------------------
|
| 326 |
+
|
| 327 |
+
Permission escalation in intermediate-level directories of the file system
|
| 328 |
+
cache on Python 3.7+. `Full description
|
| 329 |
+
<https://www.djangoproject.com/weblog/2020/sep/01/security-releases/>`__
|
| 330 |
+
|
| 331 |
+
Versions affected
|
| 332 |
+
~~~~~~~~~~~~~~~~~
|
| 333 |
+
|
| 334 |
+
* Django 3.1 :commit:`(patch) <2b099caa5923afa8cfb5f1e8c0d56b6e0e81915b>`
|
| 335 |
+
* Django 3.0 :commit:`(patch) <cdb367c92a0ba72ddc0cbd13ff42b0e6df709554>`
|
| 336 |
+
* Django 2.2 :commit:`(patch) <a3aebfdc8153dc230686b6d2454ccd32ed4c9e6f>`
|
| 337 |
+
|
| 338 |
+
September 1, 2020 - :cve:`2020-24583`
|
| 339 |
+
-------------------------------------
|
| 340 |
+
|
| 341 |
+
Incorrect permissions on intermediate-level directories on Python 3.7+. `Full
|
| 342 |
+
description
|
| 343 |
+
<https://www.djangoproject.com/weblog/2020/sep/01/security-releases/>`__
|
| 344 |
+
|
| 345 |
+
Versions affected
|
| 346 |
+
~~~~~~~~~~~~~~~~~
|
| 347 |
+
|
| 348 |
+
* Django 3.1 :commit:`(patch) <934430d22aa5d90c2ba33495ff69a6a1d997d584>`
|
| 349 |
+
* Django 3.0 :commit:`(patch) <08892bffd275c79ee1f8f67639eb170aaaf1181e>`
|
| 350 |
+
* Django 2.2 :commit:`(patch) <375657a71c889c588f723469bd868bd1d40c369f>`
|
| 351 |
+
|
| 352 |
+
June 3, 2020 - :cve:`2020-13596`
|
| 353 |
+
--------------------------------
|
| 354 |
+
|
| 355 |
+
Possible XSS via admin ``ForeignKeyRawIdWidget``. `Full description
|
| 356 |
+
<https://www.djangoproject.com/weblog/2020/jun/03/security-releases/>`__
|
| 357 |
+
|
| 358 |
+
Versions affected
|
| 359 |
+
~~~~~~~~~~~~~~~~~
|
| 360 |
+
|
| 361 |
+
* Django 3.0 :commit:`(patch) <1f2dd37f6fcefdd10ed44cb233b2e62b520afb38>`
|
| 362 |
+
* Django 2.2 :commit:`(patch) <6d61860b22875f358fac83d903dc629897934815>`
|
| 363 |
+
|
| 364 |
+
June 3, 2020 - :cve:`2020-13254`
|
| 365 |
+
--------------------------------
|
| 366 |
+
|
| 367 |
+
Potential data leakage via malformed memcached keys. `Full description
|
| 368 |
+
<https://www.djangoproject.com/weblog/2020/jun/03/security-releases/>`__
|
| 369 |
+
|
| 370 |
+
Versions affected
|
| 371 |
+
~~~~~~~~~~~~~~~~~
|
| 372 |
+
|
| 373 |
+
* Django 3.0 :commit:`(patch) <84b2da5552e100ae3294f564f6c862fef8d0e693>`
|
| 374 |
+
* Django 2.2 :commit:`(patch) <07e59caa02831c4569bbebb9eb773bdd9cb4b206>`
|
| 375 |
+
|
| 376 |
+
March 4, 2020 - :cve:`2020-9402`
|
| 377 |
+
--------------------------------
|
| 378 |
+
|
| 379 |
+
Potential SQL injection via ``tolerance`` parameter in GIS functions and
|
| 380 |
+
aggregates on Oracle. `Full description
|
| 381 |
+
<https://www.djangoproject.com/weblog/2020/mar/04/security-releases/>`__
|
| 382 |
+
|
| 383 |
+
Versions affected
|
| 384 |
+
~~~~~~~~~~~~~~~~~
|
| 385 |
+
|
| 386 |
+
* Django 3.0 :commit:`(patch) <26a5cf834526e291db00385dd33d319b8271fc4c>`
|
| 387 |
+
* Django 2.2 :commit:`(patch) <fe886a3b58a93cfbe8864b485f93cb6d426cd1f2>`
|
| 388 |
+
* Django 1.11 :commit:`(patch) <02d97f3c9a88adc890047996e5606180bd1c6166>`
|
| 389 |
+
|
| 390 |
+
February 3, 2020 - :cve:`2020-7471`
|
| 391 |
+
-----------------------------------
|
| 392 |
+
|
| 393 |
+
Potential SQL injection via ``StringAgg(delimiter)``. `Full description
|
| 394 |
+
<https://www.djangoproject.com/weblog/2020/feb/03/security-releases/>`__
|
| 395 |
+
|
| 396 |
+
Versions affected
|
| 397 |
+
~~~~~~~~~~~~~~~~~
|
| 398 |
+
|
| 399 |
+
* Django 3.0 :commit:`(patch) <505826b469b16ab36693360da9e11fd13213421b>`
|
| 400 |
+
* Django 2.2 :commit:`(patch) <c67a368c16e4680b324b4f385398d638db4d8147>`
|
| 401 |
+
* Django 1.11 :commit:`(patch) <001b0634cd309e372edb6d7d95d083d02b8e37bd>`
|
| 402 |
+
|
| 403 |
+
December 18, 2019 - :cve:`2019-19844`
|
| 404 |
+
-------------------------------------
|
| 405 |
+
|
| 406 |
+
Potential account hijack via password reset form. `Full description
|
| 407 |
+
<https://www.djangoproject.com/weblog/2019/dec/18/security-releases/>`__
|
| 408 |
+
|
| 409 |
+
Versions affected
|
| 410 |
+
~~~~~~~~~~~~~~~~~
|
| 411 |
+
|
| 412 |
+
* Django 3.0 :commit:`(patch) <302a4ff1e8b1c798aab97673909c7a3dfda42c26>`
|
| 413 |
+
* Django 2.2 :commit:`(patch) <4d334bea06cac63dc1272abcec545b85136cca0e>`
|
| 414 |
+
* Django 1.11 :commit:`(patch) <f4cff43bf921fcea6a29b726eb66767f67753fa2>`
|
| 415 |
+
|
| 416 |
+
December 2, 2019 - :cve:`2019-19118`
|
| 417 |
+
------------------------------------
|
| 418 |
+
|
| 419 |
+
Privilege escalation in the Django admin. `Full description
|
| 420 |
+
<https://www.djangoproject.com/weblog/2019/dec/02/security-releases/>`__
|
| 421 |
+
|
| 422 |
+
Versions affected
|
| 423 |
+
~~~~~~~~~~~~~~~~~
|
| 424 |
+
|
| 425 |
+
* Django 3.0 :commit:`(patch) <092cd66cf3c3e175acce698d6ca2012068d878fa>`
|
| 426 |
+
* Django 2.2 :commit:`(patch) <36f580a17f0b3cb087deadf3b65eea024f479c21>`
|
| 427 |
+
* Django 2.1 :commit:`(patch) <103ebe2b5ff1b2614b85a52c239f471904d26244>`
|
| 428 |
+
|
| 429 |
+
August 1, 2019 - :cve:`2019-14235`
|
| 430 |
+
----------------------------------
|
| 431 |
+
|
| 432 |
+
Potential memory exhaustion in ``django.utils.encoding.uri_to_iri()``. `Full
|
| 433 |
+
description
|
| 434 |
+
<https://www.djangoproject.com/weblog/2019/aug/01/security-releases/>`__
|
| 435 |
+
|
| 436 |
+
Versions affected
|
| 437 |
+
~~~~~~~~~~~~~~~~~
|
| 438 |
+
|
| 439 |
+
* Django 2.2 :commit:`(patch) <cf694e6852b0da7799f8b53f1fb2f7d20cf17534>`
|
| 440 |
+
* Django 2.1 :commit:`(patch) <5d50a2e5fa36ad23ab532fc54cf4073de84b3306>`
|
| 441 |
+
* Django 1.11 :commit:`(patch) <869b34e9b3be3a4cfcb3a145f218ffd3f5e3fd79>`
|
| 442 |
+
|
| 443 |
+
August 1, 2019 - :cve:`2019-14234`
|
| 444 |
+
----------------------------------
|
| 445 |
+
|
| 446 |
+
SQL injection possibility in key and index lookups for
|
| 447 |
+
``JSONField``/``HStoreField``. `Full description
|
| 448 |
+
<https://www.djangoproject.com/weblog/2019/aug/01/security-releases/>`__
|
| 449 |
+
|
| 450 |
+
Versions affected
|
| 451 |
+
~~~~~~~~~~~~~~~~~
|
| 452 |
+
|
| 453 |
+
* Django 2.2 :commit:`(patch) <4f5b58f5cd3c57fee9972ab074f8dc6895d8f387>`
|
| 454 |
+
* Django 2.1 :commit:`(patch) <f74b3ae3628c26e1b4f8db3d13a91d52a833a975>`
|
| 455 |
+
* Django 1.11 :commit:`(patch) <ed682a24fca774818542757651bfba576c3fc3ef>`
|
| 456 |
+
|
| 457 |
+
August 1, 2019 - :cve:`2019-14233`
|
| 458 |
+
----------------------------------
|
| 459 |
+
|
| 460 |
+
Denial-of-service possibility in ``strip_tags()``. `Full description
|
| 461 |
+
<https://www.djangoproject.com/weblog/2019/aug/01/security-releases/>`__
|
| 462 |
+
|
| 463 |
+
Versions affected
|
| 464 |
+
~~~~~~~~~~~~~~~~~
|
| 465 |
+
|
| 466 |
+
* Django 2.2 :commit:`(patch) <e34f3c0e9ee5fc9022428fe91640638bafd4cda7>`
|
| 467 |
+
* Django 2.1 :commit:`(patch) <5ff8e791148bd451180124d76a55cb2b2b9556eb>`
|
| 468 |
+
* Django 1.11 :commit:`(patch) <52479acce792ad80bb0f915f20b835f919993c72>`
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
August 1, 2019 - :cve:`2019-14232`
|
| 472 |
+
----------------------------------
|
| 473 |
+
|
| 474 |
+
Denial-of-service possibility in ``django.utils.text.Truncator``. `Full
|
| 475 |
+
description <https://www.djangoproject.com/weblog/2019/aug/01/security-releases/>`__
|
| 476 |
+
|
| 477 |
+
Versions affected
|
| 478 |
+
~~~~~~~~~~~~~~~~~
|
| 479 |
+
|
| 480 |
+
* Django 2.2 :commit:`(patch) <c3289717c6f21a8cf23daff1c78c0c014b94041f>`
|
| 481 |
+
* Django 2.1 :commit:`(patch) <c23723a1551340cc7d3126f04fcfd178fa224193>`
|
| 482 |
+
* Django 1.11 :commit:`(patch) <42a66e969023c00536256469f0e8b8a099ef109d>`
|
| 483 |
+
|
| 484 |
+
July 1, 2019 - :cve:`2019-12781`
|
| 485 |
+
--------------------------------
|
| 486 |
+
|
| 487 |
+
Incorrect HTTP detection with reverse-proxy connecting via HTTPS. `Full
|
| 488 |
+
description <https://www.djangoproject.com/weblog/2019/jul/01/security-releases/>`__
|
| 489 |
+
|
| 490 |
+
Versions affected
|
| 491 |
+
~~~~~~~~~~~~~~~~~
|
| 492 |
+
|
| 493 |
+
* Django 2.2 :commit:`(patch) <77706a3e4766da5d5fb75c4db22a0a59a28e6cd6>`
|
| 494 |
+
* Django 2.1 :commit:`(patch) <1e40f427bb8d0fb37cc9f830096a97c36c97af6f>`
|
| 495 |
+
* Django 1.11 :commit:`(patch) <32124fc41e75074141b05f10fc55a4f01ff7f050>`
|
| 496 |
+
|
| 497 |
+
June 3, 2019 - :cve:`2019-12308`
|
| 498 |
+
--------------------------------
|
| 499 |
+
|
| 500 |
+
XSS via "Current URL" link generated by ``AdminURLFieldWidget``. `Full
|
| 501 |
+
description <https://www.djangoproject.com/weblog/2019/jun/03/security-releases/>`__
|
| 502 |
+
|
| 503 |
+
Versions affected
|
| 504 |
+
~~~~~~~~~~~~~~~~~
|
| 505 |
+
|
| 506 |
+
* Django 2.2 :commit:`(patch) <afddabf8428ddc89a332f7a78d0d21eaf2b5a673>`
|
| 507 |
+
* Django 2.1 :commit:`(patch) <09186a13d975de6d049f8b3e05484f66b01ece62>`
|
| 508 |
+
* Django 1.11 :commit:`(patch) <c238701859a52d584f349cce15d56c8e8137c52b>`
|
| 509 |
+
|
| 510 |
+
June 3, 2019 - :cve:`2019-11358`
|
| 511 |
+
--------------------------------
|
| 512 |
+
|
| 513 |
+
Prototype pollution in bundled jQuery. `Full description
|
| 514 |
+
<https://www.djangoproject.com/weblog/2019/jun/03/security-releases/>`__
|
| 515 |
+
|
| 516 |
+
Versions affected
|
| 517 |
+
~~~~~~~~~~~~~~~~~
|
| 518 |
+
|
| 519 |
+
* Django 2.2 :commit:`(patch) <baaf187a4e354bf3976c51e2c83a0d2f8ee6e6ad>`
|
| 520 |
+
* Django 2.1 :commit:`(patch) <95649bc08547a878cebfa1d019edec8cb1b80829>`
|
| 521 |
+
|
| 522 |
+
February 11, 2019 - :cve:`2019-6975`
|
| 523 |
+
------------------------------------
|
| 524 |
+
|
| 525 |
+
Memory exhaustion in ``django.utils.numberformat.format()``. `Full description
|
| 526 |
+
<https://www.djangoproject.com/weblog/2019/feb/11/security-releases/>`__
|
| 527 |
+
|
| 528 |
+
Versions affected
|
| 529 |
+
~~~~~~~~~~~~~~~~~
|
| 530 |
+
|
| 531 |
+
* Django 2.1 :commit:`(patch) <40cd19055773705301c3428ed5e08a036d2091f3>`
|
| 532 |
+
* Django 2.0 :commit:`(patch <1f42f82566c9d2d73aff1c42790d6b1b243f7676>` and
|
| 533 |
+
:commit:`correction) <392e040647403fc8007708d52ce01d915b014849>`
|
| 534 |
+
* Django 1.11 :commit:`(patch) <0bbb560183fabf0533289700845dafa94951f227>`
|
| 535 |
+
|
| 536 |
+
January 4, 2019 - :cve:`2019-3498`
|
| 537 |
+
----------------------------------
|
| 538 |
+
|
| 539 |
+
Content spoofing possibility in the default 404 page. `Full description
|
| 540 |
+
<https://www.djangoproject.com/weblog/2019/jan/04/security-releases/>`__
|
| 541 |
+
|
| 542 |
+
Versions affected
|
| 543 |
+
~~~~~~~~~~~~~~~~~
|
| 544 |
+
|
| 545 |
+
* Django 2.1 :commit:`(patch) <64d2396e83aedba3fcc84ca40f23fbd22f0b9b5b>`
|
| 546 |
+
* Django 2.0 :commit:`(patch) <9f4ed7c94c62e21644ef5115e393ac426b886f2e>`
|
| 547 |
+
* Django 1.11 :commit:`(patch) <1cd00fcf52d089ef0fe03beabd05d59df8ea052a>`
|
| 548 |
+
|
| 549 |
+
October 1, 2018 - :cve:`2018-16984`
|
| 550 |
+
-----------------------------------
|
| 551 |
+
|
| 552 |
+
Password hash disclosure to "view only" admin users. `Full description
|
| 553 |
+
<https://www.djangoproject.com/weblog/2018/oct/01/security-release/>`__
|
| 554 |
+
|
| 555 |
+
Versions affected
|
| 556 |
+
~~~~~~~~~~~~~~~~~
|
| 557 |
+
|
| 558 |
+
* Django 2.1 :commit:`(patch) <c4bd5b597e0aa2432e4c867b86650f18af117851>`
|
| 559 |
+
|
| 560 |
+
August 1, 2018 - :cve:`2018-14574`
|
| 561 |
+
----------------------------------
|
| 562 |
+
|
| 563 |
+
Open redirect possibility in ``CommonMiddleware``. `Full description
|
| 564 |
+
<https://www.djangoproject.com/weblog/2018/aug/01/security-releases/>`__
|
| 565 |
+
|
| 566 |
+
Versions affected
|
| 567 |
+
~~~~~~~~~~~~~~~~~
|
| 568 |
+
|
| 569 |
+
* Django 2.1 :commit:`(patch) <c4e5ff7fdb5fce447675e90291fd33fddd052b3c>`
|
| 570 |
+
* Django 2.0 :commit:`(patch) <6fffc3c6d420e44f4029d5643f38d00a39b08525>`
|
| 571 |
+
* Django 1.11 :commit:`(patch) <d6eaee092709aad477a9894598496c6deec532ff>`
|
| 572 |
+
|
| 573 |
+
March 6, 2018 - :cve:`2018-7537`
|
| 574 |
+
--------------------------------
|
| 575 |
+
|
| 576 |
+
Denial-of-service possibility in ``truncatechars_html`` and
|
| 577 |
+
``truncatewords_html`` template filters. `Full description
|
| 578 |
+
<https://www.djangoproject.com/weblog/2018/mar/06/security-releases/>`__
|
| 579 |
+
|
| 580 |
+
Versions affected
|
| 581 |
+
~~~~~~~~~~~~~~~~~
|
| 582 |
+
|
| 583 |
+
* Django 2.0 :commit:`(patch) <94c5da1d17a6b0d378866c66b605102c19f7988c>`
|
| 584 |
+
* Django 1.11 :commit:`(patch) <a91436360b79a6ff995c3e5018bcc666dfaf1539>`
|
| 585 |
+
* Django 1.8 :commit:`(patch) <d17974a287a6ea2e361daff88fcc004cbd6835fa>`
|
| 586 |
+
|
| 587 |
+
March 6, 2018 - :cve:`2018-7536`
|
| 588 |
+
--------------------------------
|
| 589 |
+
|
| 590 |
+
Denial-of-service possibility in ``urlize`` and ``urlizetrunc`` template
|
| 591 |
+
filters. `Full description
|
| 592 |
+
<https://www.djangoproject.com/weblog/2018/mar/06/security-releases/>`__
|
| 593 |
+
|
| 594 |
+
Versions affected
|
| 595 |
+
~~~~~~~~~~~~~~~~~
|
| 596 |
+
|
| 597 |
+
* Django 2.0 :commit:`(patch) <e157315da3ae7005fa0683ffc9751dbeca7306c8>`
|
| 598 |
+
* Django 1.11 :commit:`(patch) <abf89d729f210c692a50e0ad3f75fb6bec6fae16>`
|
| 599 |
+
* Django 1.8 :commit:`(patch) <1ca63a66ef3163149ad822701273e8a1844192c2>`
|
| 600 |
+
|
| 601 |
+
February 1, 2018 - :cve:`2018-6188`
|
| 602 |
+
-----------------------------------
|
| 603 |
+
|
| 604 |
+
Information leakage in ``AuthenticationForm``. `Full description
|
| 605 |
+
<https://www.djangoproject.com/weblog/2018/feb/01/security-releases/>`__
|
| 606 |
+
|
| 607 |
+
Versions affected
|
| 608 |
+
~~~~~~~~~~~~~~~~~
|
| 609 |
+
|
| 610 |
+
* Django 2.0 :commit:`(patch) <c37bb28677295f6edda61d8ac461014ef0d3aeb2>`
|
| 611 |
+
* Django 1.11 :commit:`(patch) <57b95fedad5e0b83fc9c81466b7d1751c6427aae>`
|
| 612 |
+
|
| 613 |
+
September 5, 2017 - :cve:`2017-12794`
|
| 614 |
+
-------------------------------------
|
| 615 |
+
|
| 616 |
+
Possible XSS in traceback section of technical 500 debug page. `Full
|
| 617 |
+
description <https://www.djangoproject.com/weblog/2017/sep/05/security-releases/>`__
|
| 618 |
+
|
| 619 |
+
Versions affected
|
| 620 |
+
~~~~~~~~~~~~~~~~~
|
| 621 |
+
|
| 622 |
+
* Django 1.11 :commit:`(patch) <e35a0c56086924f331e9422daa266e907a4784cc>`
|
| 623 |
+
* Django 1.10 :commit:`(patch) <58e08e80e362db79eb0fd775dc81faad90dca47a>`
|
| 624 |
+
|
| 625 |
+
April 4, 2017 - :cve:`2017-7234`
|
| 626 |
+
--------------------------------
|
| 627 |
+
|
| 628 |
+
Open redirect vulnerability in ``django.views.static.serve()``. `Full
|
| 629 |
+
description <https://www.djangoproject.com/weblog/2017/apr/04/security-releases/>`__
|
| 630 |
+
|
| 631 |
+
Versions affected
|
| 632 |
+
~~~~~~~~~~~~~~~~~
|
| 633 |
+
|
| 634 |
+
* Django 1.10 :commit:`(patch) <2a9f6ef71b8e23fd267ee2be1be26dde8ab67037>`
|
| 635 |
+
* Django 1.9 :commit:`(patch) <5f1ffb07afc1e59729ce2b283124116d6c0659e4>`
|
| 636 |
+
* Django 1.8 :commit:`(patch) <4a6b945dffe8d10e7cec107d93e6efaebfbded29>`
|
| 637 |
+
|
| 638 |
+
April 4, 2017 - :cve:`2017-7233`
|
| 639 |
+
--------------------------------
|
| 640 |
+
|
| 641 |
+
Open redirect and possible XSS attack via user-supplied numeric redirect URLs.
|
| 642 |
+
`Full description <https://www.djangoproject.com/weblog/2017/apr/04/security-releases/>`__
|
| 643 |
+
|
| 644 |
+
Versions affected
|
| 645 |
+
~~~~~~~~~~~~~~~~~
|
| 646 |
+
|
| 647 |
+
* Django 1.10 :commit:`(patch) <f824655bc2c50b19d2f202d7640785caabc82787>`
|
| 648 |
+
* Django 1.9 :commit:`(patch) <254326cb3682389f55f886804d2c43f7b9f23e4f>`
|
| 649 |
+
* Django 1.8 :commit:`(patch) <8339277518c7d8ec280070a780915304654e3b66>`
|
| 650 |
+
|
| 651 |
+
November 1, 2016 - :cve:`2016-9014`
|
| 652 |
+
-----------------------------------
|
| 653 |
+
|
| 654 |
+
DNS rebinding vulnerability when ``DEBUG=True``. `Full description
|
| 655 |
+
<https://www.djangoproject.com/weblog/2016/nov/01/security-releases/>`__
|
| 656 |
+
|
| 657 |
+
Versions affected
|
| 658 |
+
~~~~~~~~~~~~~~~~~
|
| 659 |
+
|
| 660 |
+
* Django 1.10 :commit:`(patch) <884e113838e5a72b4b0ec9e5e87aa480f6aa4472>`
|
| 661 |
+
* Django 1.9 :commit:`(patch) <45acd6d836895a4c36575f48b3fb36a3dae98d19>`
|
| 662 |
+
* Django 1.8 :commit:`(patch) <c401ae9a7dfb1a94a8a61927ed541d6f93089587>`
|
| 663 |
+
|
| 664 |
+
November 1, 2016 - :cve:`2016-9013`
|
| 665 |
+
-----------------------------------
|
| 666 |
+
|
| 667 |
+
User with hardcoded password created when running tests on Oracle. `Full
|
| 668 |
+
description <https://www.djangoproject.com/weblog/2016/nov/01/security-releases/>`__
|
| 669 |
+
|
| 670 |
+
Versions affected
|
| 671 |
+
~~~~~~~~~~~~~~~~~
|
| 672 |
+
|
| 673 |
+
* Django 1.10 :commit:`(patch) <34e10720d81b8d407aa14d763b6a7fe8f13b4f2e>`
|
| 674 |
+
* Django 1.9 :commit:`(patch) <4844d86c7728c1a5a3bbce4ad336a8d32304072b>`
|
| 675 |
+
* Django 1.8 :commit:`(patch) <70f99952965a430daf69eeb9947079aae535d2d0>`
|
| 676 |
+
|
| 677 |
+
September 26, 2016 - :cve:`2016-7401`
|
| 678 |
+
-------------------------------------
|
| 679 |
+
|
| 680 |
+
CSRF protection bypass on a site with Google Analytics. `Full description
|
| 681 |
+
<https://www.djangoproject.com/weblog/2016/sep/26/security-releases/>`__
|
| 682 |
+
|
| 683 |
+
Versions affected
|
| 684 |
+
~~~~~~~~~~~~~~~~~
|
| 685 |
+
|
| 686 |
+
* Django 1.9 :commit:`(patch) <d1bc980db1c0fffd6d60677e62f70beadb9fe64a>`
|
| 687 |
+
* Django 1.8 :commit:`(patch) <6118ab7d0676f0d622278e5be215f14fb5410b6a>`
|
| 688 |
+
|
| 689 |
+
July 18, 2016 - :cve:`2016-6186`
|
| 690 |
+
--------------------------------
|
| 691 |
+
|
| 692 |
+
XSS in admin's add/change related popup. `Full description
|
| 693 |
+
<https://www.djangoproject.com/weblog/2016/jul/18/security-releases/>`__
|
| 694 |
+
|
| 695 |
+
Versions affected
|
| 696 |
+
~~~~~~~~~~~~~~~~~
|
| 697 |
+
|
| 698 |
+
* Django 1.9 :commit:`(patch) <d03bf6fe4e9bf5b07de62c1a271c4b41a7d3d158>`
|
| 699 |
+
* Django 1.8 :commit:`(patch) <f68e5a99164867ab0e071a936470958ed867479d>`
|
| 700 |
+
|
| 701 |
+
March 1, 2016 - :cve:`2016-2513`
|
| 702 |
+
--------------------------------
|
| 703 |
+
|
| 704 |
+
User enumeration through timing difference on password hasher work factor
|
| 705 |
+
upgrade. `Full description
|
| 706 |
+
<https://www.djangoproject.com/weblog/2016/mar/01/security-releases/>`__
|
| 707 |
+
|
| 708 |
+
Versions affected
|
| 709 |
+
~~~~~~~~~~~~~~~~~
|
| 710 |
+
|
| 711 |
+
* Django 1.9 :commit:`(patch) <af7d09b0c5c6ab68e629fd9baf736f9dd203b18e>`
|
| 712 |
+
* Django 1.8 :commit:`(patch) <f4e6e02f7713a6924d16540be279909ff4091eb6>`
|
| 713 |
+
|
| 714 |
+
March 1, 2016 - :cve:`2016-2512`
|
| 715 |
+
--------------------------------
|
| 716 |
+
|
| 717 |
+
Malicious redirect and possible XSS attack via user-supplied redirect URLs
|
| 718 |
+
containing basic auth. `Full description
|
| 719 |
+
<https://www.djangoproject.com/weblog/2016/mar/01/security-releases/>`__
|
| 720 |
+
|
| 721 |
+
Versions affected
|
| 722 |
+
~~~~~~~~~~~~~~~~~
|
| 723 |
+
|
| 724 |
+
* Django 1.9 :commit:`(patch) <fc6d147a63f89795dbcdecb0559256470fff4380>`
|
| 725 |
+
* Django 1.8 :commit:`(patch) <382ab137312961ad62feb8109d70a5a581fe8350>`
|
| 726 |
+
|
| 727 |
+
February 1, 2016 - :cve:`2016-2048`
|
| 728 |
+
-----------------------------------
|
| 729 |
+
|
| 730 |
+
User with "change" but not "add" permission can create objects for
|
| 731 |
+
``ModelAdmin``’s with ``save_as=True``. `Full description
|
| 732 |
+
<https://www.djangoproject.com/weblog/2016/feb/01/releases-192-and-189/>`__
|
| 733 |
+
|
| 734 |
+
Versions affected
|
| 735 |
+
~~~~~~~~~~~~~~~~~
|
| 736 |
+
|
| 737 |
+
* Django 1.9 :commit:`(patch) <adbca5e4db42542575734b8e5d26961c8ada7265>`
|
| 738 |
+
|
| 739 |
+
November 24, 2015 - :cve:`2015-8213`
|
| 740 |
+
------------------------------------
|
| 741 |
+
|
| 742 |
+
Settings leak possibility in ``date`` template filter. `Full description
|
| 743 |
+
<https://www.djangoproject.com/weblog/2015/nov/24/security-releases-issued/>`__
|
| 744 |
+
|
| 745 |
+
Versions affected
|
| 746 |
+
~~~~~~~~~~~~~~~~~
|
| 747 |
+
|
| 748 |
+
* Django 1.8 :commit:`(patch) <9f83fc2f66f5a0bac7c291aec55df66050bb6991>`
|
| 749 |
+
* Django 1.7 :commit:`(patch) <8a01c6b53169ee079cb21ac5919fdafcc8c5e172>`
|
| 750 |
+
|
| 751 |
+
August 18, 2015 - :cve:`2015-5963` / :cve:`2015-5964`
|
| 752 |
+
-----------------------------------------------------
|
| 753 |
+
|
| 754 |
+
Denial-of-service possibility in ``logout()`` view by filling session store.
|
| 755 |
+
`Full description <https://www.djangoproject.com/weblog/2015/aug/18/security-releases/>`__
|
| 756 |
+
|
| 757 |
+
Versions affected
|
| 758 |
+
~~~~~~~~~~~~~~~~~
|
| 759 |
+
|
| 760 |
+
* Django 1.8 :commit:`(patch) <2eb86b01d7b59be06076f6179a454d0fd0afaff6>`
|
| 761 |
+
* Django 1.7 :commit:`(patch) <2f5485346ee6f84b4e52068c04e043092daf55f7>`
|
| 762 |
+
* Django 1.4 :commit:`(patch) <575f59f9bc7c59a5e41a081d1f5f55fc859c5012>`
|
| 763 |
+
|
| 764 |
+
July 8, 2015 - :cve:`2015-5145`
|
| 765 |
+
-------------------------------
|
| 766 |
+
|
| 767 |
+
Denial-of-service possibility in URL validation. `Full description
|
| 768 |
+
<https://www.djangoproject.com/weblog/2015/jul/08/security-releases/>`__
|
| 769 |
+
|
| 770 |
+
Versions affected
|
| 771 |
+
~~~~~~~~~~~~~~~~~
|
| 772 |
+
|
| 773 |
+
* Django 1.8 :commit:`(patch) <8f9a4d3a2bc42f14bb437defd30c7315adbff22c>`
|
| 774 |
+
|
| 775 |
+
July 8, 2015 - :cve:`2015-5144`
|
| 776 |
+
-------------------------------
|
| 777 |
+
|
| 778 |
+
Header injection possibility since validators accept newlines in input. `Full
|
| 779 |
+
description <https://www.djangoproject.com/weblog/2015/jul/08/security-releases/>`__
|
| 780 |
+
|
| 781 |
+
Versions affected
|
| 782 |
+
~~~~~~~~~~~~~~~~~
|
| 783 |
+
|
| 784 |
+
* Django 1.8 :commit:`(patch) <574dd5e0b0fbb877ae5827b1603d298edc9bb2a0>`
|
| 785 |
+
* Django 1.7 :commit:`(patch) <ae49b4d994656bc037513dcd064cb9ce5bb85649>`
|
| 786 |
+
* Django 1.4 :commit:`(patch) <1ba1cdce7d58e6740fe51955d945b56ae51d072a>`
|
| 787 |
+
|
| 788 |
+
July 8, 2015 - :cve:`2015-5143`
|
| 789 |
+
-------------------------------
|
| 790 |
+
|
| 791 |
+
Denial-of-service possibility by filling session store. `Full
|
| 792 |
+
description <https://www.djangoproject.com/weblog/2015/jul/08/security-releases/>`__
|
| 793 |
+
|
| 794 |
+
Versions affected
|
| 795 |
+
~~~~~~~~~~~~~~~~~
|
| 796 |
+
|
| 797 |
+
* Django 1.8 :commit:`(patch) <66d12d1ababa8f062857ee5eb43276493720bf16>`
|
| 798 |
+
* Django 1.7 :commit:`(patch) <1828f4341ec53a8684112d24031b767eba557663>`
|
| 799 |
+
* Django 1.4 :commit:`(patch) <2e47f3e401c29bc2ba5ab794d483cb0820855fb9>`
|
| 800 |
+
|
| 801 |
+
May 20, 2015 - :cve:`2015-3982`
|
| 802 |
+
-------------------------------
|
| 803 |
+
|
| 804 |
+
Fixed session flushing in the cached_db backend. `Full description
|
| 805 |
+
<https://www.djangoproject.com/weblog/2015/may/20/security-release/>`__
|
| 806 |
+
|
| 807 |
+
Versions affected
|
| 808 |
+
~~~~~~~~~~~~~~~~~
|
| 809 |
+
|
| 810 |
+
* Django 1.8 :commit:`(patch) <31cb25adecba930bdeee4556709f5a1c42d88fd6>`
|
| 811 |
+
|
| 812 |
+
March 18, 2015 - :cve:`2015-2317`
|
| 813 |
+
---------------------------------
|
| 814 |
+
|
| 815 |
+
Mitigated possible XSS attack via user-supplied redirect URLs. `Full
|
| 816 |
+
description <https://www.djangoproject.com/weblog/2015/mar/18/security-releases/>`__
|
| 817 |
+
|
| 818 |
+
Versions affected
|
| 819 |
+
~~~~~~~~~~~~~~~~~
|
| 820 |
+
|
| 821 |
+
* Django 1.4 :commit:`(patch) <2342693b31f740a422abf7267c53b4e7bc487c1b>`
|
| 822 |
+
* Django 1.6 :commit:`(patch) <5510f070711540aaa8d3707776cd77494e688ef9>`
|
| 823 |
+
* Django 1.7 :commit:`(patch) <2a4113dbd532ce952308992633d802dc169a75f1>`
|
| 824 |
+
* Django 1.8 :commit:`(patch) <770427c2896a078925abfca2317486b284d22f04>`
|
| 825 |
+
|
| 826 |
+
March 18, 2015 - :cve:`2015-2316`
|
| 827 |
+
---------------------------------
|
| 828 |
+
|
| 829 |
+
Denial-of-service possibility with ``strip_tags()``. `Full description
|
| 830 |
+
<https://www.djangoproject.com/weblog/2015/mar/18/security-releases/>`__
|
| 831 |
+
|
| 832 |
+
Versions affected
|
| 833 |
+
~~~~~~~~~~~~~~~~~
|
| 834 |
+
|
| 835 |
+
* Django 1.6 :commit:`(patch) <b6b3cb9899214a23ebb0f4ebf0e0b300b0ee524f>`
|
| 836 |
+
* Django 1.7 :commit:`(patch) <e63363f8e075fa8d66326ad6a1cc3391cc95cd97>`
|
| 837 |
+
* Django 1.8 :commit:`(patch) <5447709a571cd5d95971f1d5d21d4a7edcf85bbd>`
|
| 838 |
+
|
| 839 |
+
March 9, 2015 - :cve:`2015-2241`
|
| 840 |
+
--------------------------------
|
| 841 |
+
|
| 842 |
+
XSS attack via properties in ``ModelAdmin.readonly_fields``. `Full description
|
| 843 |
+
<https://www.djangoproject.com/weblog/2015/mar/09/security-releases/>`__
|
| 844 |
+
|
| 845 |
+
Versions affected
|
| 846 |
+
~~~~~~~~~~~~~~~~~
|
| 847 |
+
|
| 848 |
+
* Django 1.7 :commit:`(patch) <d16e4e1d6f95e6f46bff53cc4fd0ab398b8e5059>`
|
| 849 |
+
* Django 1.8 :commit:`(patch) <2654e1b93923bac55f12b4e66c5e39b16695ace5>`
|
| 850 |
+
|
| 851 |
+
January 13, 2015 - :cve:`2015-0222`
|
| 852 |
+
-----------------------------------
|
| 853 |
+
|
| 854 |
+
Database denial-of-service with ``ModelMultipleChoiceField``. `Full description
|
| 855 |
+
<https://www.djangoproject.com/weblog/2015/jan/13/security/>`__
|
| 856 |
+
|
| 857 |
+
Versions affected
|
| 858 |
+
~~~~~~~~~~~~~~~~~
|
| 859 |
+
|
| 860 |
+
* Django 1.6 :commit:`(patch) <d7a06ee7e571b6dad07c0f5b519b1db02e2a476c>`
|
| 861 |
+
* Django 1.7 :commit:`(patch) <bcfb47780ce7caecb409a9e9c1c314266e41d392>`
|
| 862 |
+
|
| 863 |
+
January 13, 2015 - :cve:`2015-0221`
|
| 864 |
+
-----------------------------------
|
| 865 |
+
|
| 866 |
+
Denial-of-service attack against ``django.views.static.serve()``. `Full
|
| 867 |
+
description <https://www.djangoproject.com/weblog/2015/jan/13/security/>`__
|
| 868 |
+
|
| 869 |
+
Versions affected
|
| 870 |
+
~~~~~~~~~~~~~~~~~
|
| 871 |
+
|
| 872 |
+
* Django 1.4 :commit:`(patch) <d020da6646c5142bc092247d218a3d1ce3e993f7>`
|
| 873 |
+
* Django 1.6 :commit:`(patch) <553779c4055e8742cc832ed525b9ee34b174934f>`
|
| 874 |
+
* Django 1.7 :commit:`(patch) <818e59a3f0fbadf6c447754d202d88df025f8f2a>`
|
| 875 |
+
|
| 876 |
+
January 13, 2015 - :cve:`2015-0220`
|
| 877 |
+
-----------------------------------
|
| 878 |
+
|
| 879 |
+
Mitigated possible XSS attack via user-supplied redirect URLs. `Full
|
| 880 |
+
description <https://www.djangoproject.com/weblog/2015/jan/13/security/>`__
|
| 881 |
+
|
| 882 |
+
Versions affected
|
| 883 |
+
~~~~~~~~~~~~~~~~~
|
| 884 |
+
|
| 885 |
+
* Django 1.4 :commit:`(patch) <4c241f1b710da6419d9dca160e80b23b82db7758>`
|
| 886 |
+
* Django 1.6 :commit:`(patch) <72e0b033662faa11bb7f516f18a132728aa0ae28>`
|
| 887 |
+
* Django 1.7 :commit:`(patch) <de67dedc771ad2edec15c1d00c083a1a084e1e89>`
|
| 888 |
+
|
| 889 |
+
January 13, 2015 - :cve:`2015-0219`
|
| 890 |
+
-----------------------------------
|
| 891 |
+
|
| 892 |
+
WSGI header spoofing via underscore/dash conflation. `Full description
|
| 893 |
+
<https://www.djangoproject.com/weblog/2015/jan/13/security/>`__
|
| 894 |
+
|
| 895 |
+
Versions affected
|
| 896 |
+
~~~~~~~~~~~~~~~~~
|
| 897 |
+
|
| 898 |
+
* Django 1.4 :commit:`(patch) <4f6fffc1dc429f1ad428ecf8e6620739e8837450>`
|
| 899 |
+
* Django 1.6 :commit:`(patch) <d7597b31d5c03106eeba4be14a33b32a5e25f4ee>`
|
| 900 |
+
* Django 1.7 :commit:`(patch) <41b4bc73ee0da7b2e09f4af47fc1fd21144c710f>`
|
| 901 |
+
|
| 902 |
+
August 20, 2014 - :cve:`2014-0483`
|
| 903 |
+
----------------------------------
|
| 904 |
+
|
| 905 |
+
Data leakage via querystring manipulation in admin.
|
| 906 |
+
`Full description <https://www.djangoproject.com/weblog/2014/aug/20/security/>`__
|
| 907 |
+
|
| 908 |
+
Versions affected
|
| 909 |
+
~~~~~~~~~~~~~~~~~
|
| 910 |
+
|
| 911 |
+
* Django 1.4 :commit:`(patch) <027bd348642007617518379f8b02546abacaa6e0>`
|
| 912 |
+
* Django 1.5 :commit:`(patch) <2a446c896e7c814661fb9c4f212b071b2a7fa446>`
|
| 913 |
+
* Django 1.6 :commit:`(patch) <f7c494f2506250b8cb5923714360a3642ed63e0f>`
|
| 914 |
+
* Django 1.7 :commit:`(patch) <2b31342cdf14fc20e07c43d258f1e7334ad664a6>`
|
| 915 |
+
|
| 916 |
+
August 20, 2014 - :cve:`2014-0482`
|
| 917 |
+
----------------------------------
|
| 918 |
+
|
| 919 |
+
``RemoteUserMiddleware`` session hijacking. `Full description
|
| 920 |
+
<https://www.djangoproject.com/weblog/2014/aug/20/security/>`__
|
| 921 |
+
|
| 922 |
+
Versions affected
|
| 923 |
+
~~~~~~~~~~~~~~~~~
|
| 924 |
+
|
| 925 |
+
* Django 1.4 :commit:`(patch) <c9e3b9949cd55f090591fbdc4a114fcb8368b6d9>`
|
| 926 |
+
* Django 1.5 :commit:`(patch) <dd68f319b365f6cb38c5a6c106faf4f6142d7d88>`
|
| 927 |
+
* Django 1.6 :commit:`(patch) <0268b855f9eab3377f2821164ef3e66037789e09>`
|
| 928 |
+
* Django 1.7 :commit:`(patch) <1a45d059c70385fcd6f4a3955f3b4e4cc96d0150>`
|
| 929 |
+
|
| 930 |
+
August 20, 2014 - :cve:`2014-0481`
|
| 931 |
+
----------------------------------
|
| 932 |
+
|
| 933 |
+
File upload denial of service. `Full description
|
| 934 |
+
<https://www.djangoproject.com/weblog/2014/aug/20/security/>`__
|
| 935 |
+
|
| 936 |
+
Versions affected
|
| 937 |
+
~~~~~~~~~~~~~~~~~
|
| 938 |
+
|
| 939 |
+
* Django 1.4 :commit:`(patch) <30042d475bf084c6723c6217a21598d9247a9c41>`
|
| 940 |
+
* Django 1.5 :commit:`(patch) <26cd48e166ac4d84317c8ee6d63ac52a87e8da99>`
|
| 941 |
+
* Django 1.6 :commit:`(patch) <dd0c3f4ee1a30c1a1e6055061c6ba6e58c6b54d1>`
|
| 942 |
+
* Django 1.7 :commit:`(patch) <3123f8452cf49071be9110e277eea60ba0032216>`
|
| 943 |
+
|
| 944 |
+
August 20, 2014 - :cve:`2014-0480`
|
| 945 |
+
----------------------------------
|
| 946 |
+
|
| 947 |
+
``reverse()`` can generate URLs pointing to other hosts. `Full description
|
| 948 |
+
<https://www.djangoproject.com/weblog/2014/aug/20/security/>`__
|
| 949 |
+
|
| 950 |
+
Versions affected
|
| 951 |
+
~~~~~~~~~~~~~~~~~
|
| 952 |
+
|
| 953 |
+
* Django 1.4 :commit:`(patch) <c2fe73133b62a1d9e8f7a6b43966570b14618d7e>`
|
| 954 |
+
* Django 1.5 :commit:`(patch) <45ac9d4fb087d21902469fc22643f5201d41a0cd>`
|
| 955 |
+
* Django 1.6 :commit:`(patch) <da051da8df5e69944745072611351d4cfc6435d5>`
|
| 956 |
+
* Django 1.7 :commit:`(patch) <bf650a2ee78c6d1f4544a875dcc777cf27fe93e9>`
|
| 957 |
+
|
| 958 |
+
May 18, 2014 - :cve:`2014-3730`
|
| 959 |
+
-------------------------------
|
| 960 |
+
|
| 961 |
+
Malformed URLs from user input incorrectly validated. `Full description
|
| 962 |
+
<https://www.djangoproject.com/weblog/2014/may/14/security-releases-issued/>`__
|
| 963 |
+
|
| 964 |
+
Versions affected
|
| 965 |
+
~~~~~~~~~~~~~~~~~
|
| 966 |
+
|
| 967 |
+
* Django 1.4 :commit:`(patch) <7feb54bbae3f637ab3c4dd4831d4385964f574df>`
|
| 968 |
+
* Django 1.5 :commit:`(patch) <ad32c218850ad40972dcef57beb460f8c979dd6d>`
|
| 969 |
+
* Django 1.6 :commit:`(patch) <601107524523bca02376a0ddc1a06c6fdb8f22f3>`
|
| 970 |
+
* Django 1.7 :commit:`(patch) <e7b0cace455c2da24492660636bfd48c45a19cdf>`
|
| 971 |
+
|
| 972 |
+
May 18, 2014 - :cve:`2014-1418`
|
| 973 |
+
-------------------------------
|
| 974 |
+
|
| 975 |
+
Caches may be allowed to store and serve private data. `Full description
|
| 976 |
+
<https://www.djangoproject.com/weblog/2014/may/14/security-releases-issued/>`__
|
| 977 |
+
|
| 978 |
+
Versions affected
|
| 979 |
+
~~~~~~~~~~~~~~~~~
|
| 980 |
+
|
| 981 |
+
* Django 1.4 :commit:`(patch) <28e23306aa53bbbb8fb87db85f99d970b051026c>`
|
| 982 |
+
* Django 1.5 :commit:`(patch) <4001ec8698f577b973c5a540801d8a0bbea1205b>`
|
| 983 |
+
* Django 1.6 :commit:`(patch) <1abcf3a808b35abae5d425ed4d44cb6e886dc769>`
|
| 984 |
+
* Django 1.7 :commit:`(patch) <7fef18ba9e5a8b47bc24b5bb259c8bf3d3879f2a>`
|
| 985 |
+
|
| 986 |
+
April 21, 2014 - :cve:`2014-0474`
|
| 987 |
+
---------------------------------
|
| 988 |
+
|
| 989 |
+
MySQL typecasting causes unexpected query results. `Full description
|
| 990 |
+
<https://www.djangoproject.com/weblog/2014/apr/21/security/>`__
|
| 991 |
+
|
| 992 |
+
Versions affected
|
| 993 |
+
~~~~~~~~~~~~~~~~~
|
| 994 |
+
|
| 995 |
+
* Django 1.4 :commit:`(patch) <aa80f498de6d687e613860933ac58433ab71ea4b>`
|
| 996 |
+
* Django 1.5 :commit:`(patch) <985434fb1d6bf2335bf96c6ebf91c3674f1f399f>`
|
| 997 |
+
* Django 1.6 :commit:`(patch) <5f0829a27e85d89ad8c433f5c6a7a7d17c9e9292>`
|
| 998 |
+
* Django 1.7 :commit:`(patch) <34526c2f56b863c2103655a0893ac801667e86ea>`
|
| 999 |
+
|
| 1000 |
+
April 21, 2014 - :cve:`2014-0473`
|
| 1001 |
+
---------------------------------
|
| 1002 |
+
|
| 1003 |
+
Caching of anonymous pages could reveal CSRF token. `Full description
|
| 1004 |
+
<https://www.djangoproject.com/weblog/2014/apr/21/security/>`__
|
| 1005 |
+
|
| 1006 |
+
Versions affected
|
| 1007 |
+
~~~~~~~~~~~~~~~~~
|
| 1008 |
+
|
| 1009 |
+
* Django 1.4 :commit:`(patch) <1170f285ddd6a94a65f911a27788ba49ca08c0b0>`
|
| 1010 |
+
* Django 1.5 :commit:`(patch) <6872f42757d7ef6a97e0b6ec5db4d2615d8a2bd8>`
|
| 1011 |
+
* Django 1.6 :commit:`(patch) <d63e20942f3024f24cb8cd85a49461ba8a9b6736>`
|
| 1012 |
+
* Django 1.7 :commit:`(patch) <380545bf85cbf17fc698d136815b7691f8d023ca>`
|
| 1013 |
+
|
| 1014 |
+
April 21, 2014 - :cve:`2014-0472`
|
| 1015 |
+
---------------------------------
|
| 1016 |
+
|
| 1017 |
+
Unexpected code execution using ``reverse()``. `Full description
|
| 1018 |
+
<https://www.djangoproject.com/weblog/2014/apr/21/security/>`__
|
| 1019 |
+
|
| 1020 |
+
Versions affected
|
| 1021 |
+
~~~~~~~~~~~~~~~~~
|
| 1022 |
+
|
| 1023 |
+
* Django 1.4 :commit:`(patch) <c1a8c420fe4b27fb2caf5e46d23b5712fc0ac535>`
|
| 1024 |
+
* Django 1.5 :commit:`(patch) <2a5bcb69f42b84464b24b5c835dca6467b6aa7f1>`
|
| 1025 |
+
* Django 1.6 :commit:`(patch) <4352a50871e239ebcdf64eee6f0b88e714015c1b>`
|
| 1026 |
+
* Django 1.7 :commit:`(patch) <546740544d7f69254a67b06a3fc7fa0c43512958>`
|
| 1027 |
+
|
| 1028 |
+
September 14, 2013 - :cve:`2013-1443`
|
| 1029 |
+
-------------------------------------
|
| 1030 |
+
|
| 1031 |
+
Denial-of-service via large passwords. `Full description
|
| 1032 |
+
<https://www.djangoproject.com/weblog/2013/sep/15/security/>`__
|
| 1033 |
+
|
| 1034 |
+
Versions affected
|
| 1035 |
+
~~~~~~~~~~~~~~~~~
|
| 1036 |
+
|
| 1037 |
+
* Django 1.4 :commit:`(patch <3f3d887a6844ec2db743fee64c9e53e04d39a368>` and :commit:`Python compatibility fix) <6903d1690a92aa040adfb0c8eb37cf62e4206714>`
|
| 1038 |
+
* Django 1.5 :commit:`(patch) <22b74fa09d7ccbc8c52270d648a0da7f3f0fa2bc>`
|
| 1039 |
+
|
| 1040 |
+
September 10, 2013 - :cve:`2013-4315`
|
| 1041 |
+
-------------------------------------
|
| 1042 |
+
|
| 1043 |
+
Directory-traversal via ``ssi`` template tag. `Full description
|
| 1044 |
+
<https://www.djangoproject.com/weblog/2013/sep/10/security-releases-issued/>`__
|
| 1045 |
+
|
| 1046 |
+
Versions affected
|
| 1047 |
+
~~~~~~~~~~~~~~~~~
|
| 1048 |
+
|
| 1049 |
+
* Django 1.4 :commit:`(patch) <87d2750b39f6f2d54b7047225521a44dcd37e896>`
|
| 1050 |
+
* Django 1.5 :commit:`(patch) <988b61c550d798f9a66d17ee0511fb7a9a7f33ca>`
|
| 1051 |
+
|
| 1052 |
+
August 13, 2013 - :cve:`2013-6044`
|
| 1053 |
+
----------------------------------
|
| 1054 |
+
|
| 1055 |
+
Possible XSS via unvalidated URL redirect schemes. `Full description
|
| 1056 |
+
<https://www.djangoproject.com/weblog/2013/aug/13/security-releases-issued/>`__
|
| 1057 |
+
|
| 1058 |
+
Versions affected
|
| 1059 |
+
~~~~~~~~~~~~~~~~~
|
| 1060 |
+
|
| 1061 |
+
* Django 1.4 :commit:`(patch) <ec67af0bd609c412b76eaa4cc89968a2a8e5ad6a>`
|
| 1062 |
+
* Django 1.5 :commit:`(patch) <1a274ccd6bc1afbdac80344c9b6e5810c1162b5f>`
|
| 1063 |
+
|
| 1064 |
+
August 13, 2013 - :cve:`2013-4249`
|
| 1065 |
+
----------------------------------
|
| 1066 |
+
|
| 1067 |
+
XSS via admin trusting ``URLField`` values. `Full description
|
| 1068 |
+
<https://www.djangoproject.com/weblog/2013/aug/13/security-releases-issued/>`__
|
| 1069 |
+
|
| 1070 |
+
Versions affected
|
| 1071 |
+
~~~~~~~~~~~~~~~~~
|
| 1072 |
+
|
| 1073 |
+
* Django 1.5 :commit:`(patch) <90363e388c61874add3f3557ee654a996ec75d78>`
|
| 1074 |
+
|
| 1075 |
+
February 19, 2013 - :cve:`2013-0306`
|
| 1076 |
+
------------------------------------
|
| 1077 |
+
|
| 1078 |
+
Denial-of-service via formset ``max_num`` bypass. `Full description
|
| 1079 |
+
<https://www.djangoproject.com/weblog/2013/feb/19/security/>`__
|
| 1080 |
+
|
| 1081 |
+
Versions affected
|
| 1082 |
+
~~~~~~~~~~~~~~~~~
|
| 1083 |
+
|
| 1084 |
+
* Django 1.3 :commit:`(patch) <d7094bbce8cb838f3b40f504f198c098ff1cf727>`
|
| 1085 |
+
* Django 1.4 :commit:`(patch) <0cc350a896f70ace18280410eb616a9197d862b0>`
|
| 1086 |
+
|
| 1087 |
+
February 19, 2013 - :cve:`2013-0305`
|
| 1088 |
+
------------------------------------
|
| 1089 |
+
|
| 1090 |
+
Information leakage via admin history log. `Full description
|
| 1091 |
+
<https://www.djangoproject.com/weblog/2013/feb/19/security/>`__
|
| 1092 |
+
|
| 1093 |
+
Versions affected
|
| 1094 |
+
~~~~~~~~~~~~~~~~~
|
| 1095 |
+
|
| 1096 |
+
* Django 1.3 :commit:`(patch) <d3a45e10c8ac8268899999129daa27652ec0da35>`
|
| 1097 |
+
* Django 1.4 :commit:`(patch) <0e7861aec73702f7933ce2a93056f7983939f0d6>`
|
| 1098 |
+
|
| 1099 |
+
February 19, 2013 - :cve:`2013-1664` / :cve:`2013-1665`
|
| 1100 |
+
-------------------------------------------------------
|
| 1101 |
+
|
| 1102 |
+
Entity-based attacks against Python XML libraries. `Full description
|
| 1103 |
+
<https://www.djangoproject.com/weblog/2013/feb/19/security/>`__
|
| 1104 |
+
|
| 1105 |
+
Versions affected
|
| 1106 |
+
~~~~~~~~~~~~~~~~~
|
| 1107 |
+
|
| 1108 |
+
* Django 1.3 :commit:`(patch) <d19a27066b2247102e65412aa66917aff0091112>`
|
| 1109 |
+
* Django 1.4 :commit:`(patch) <1c60d07ba23e0350351c278ad28d0bd5aa410b40>`
|
| 1110 |
+
|
| 1111 |
+
February 19, 2013 - No CVE
|
| 1112 |
+
--------------------------
|
| 1113 |
+
|
| 1114 |
+
Additional hardening of ``Host`` header handling. `Full description
|
| 1115 |
+
<https://www.djangoproject.com/weblog/2013/feb/19/security/>`__
|
| 1116 |
+
|
| 1117 |
+
Versions affected
|
| 1118 |
+
~~~~~~~~~~~~~~~~~
|
| 1119 |
+
|
| 1120 |
+
* Django 1.3 :commit:`(patch) <27cd872e6e36a81d0bb6f5b8765a1705fecfc253>`
|
| 1121 |
+
* Django 1.4 :commit:`(patch) <9936fdb11d0bbf0bd242f259bfb97bbf849d16f8>`
|
| 1122 |
+
|
| 1123 |
+
December 10, 2012 - No CVE 2
|
| 1124 |
+
----------------------------
|
| 1125 |
+
|
| 1126 |
+
Additional hardening of redirect validation. `Full description
|
| 1127 |
+
<https://www.djangoproject.com/weblog/2012/dec/10/security/>`__
|
| 1128 |
+
|
| 1129 |
+
Versions affected
|
| 1130 |
+
~~~~~~~~~~~~~~~~~
|
| 1131 |
+
|
| 1132 |
+
* Django 1.3: :commit:`(patch) <1515eb46daa0897ba5ad5f0a2db8969255f1b343>`
|
| 1133 |
+
* Django 1.4: :commit:`(patch) <b2ae0a63aeec741f1e51bac9a95a27fd635f9652>`
|
| 1134 |
+
|
| 1135 |
+
December 10, 2012 - No CVE 1
|
| 1136 |
+
----------------------------
|
| 1137 |
+
|
| 1138 |
+
Additional hardening of ``Host`` header handling. `Full description
|
| 1139 |
+
<https://www.djangoproject.com/weblog/2012/dec/10/security/>`__
|
| 1140 |
+
|
| 1141 |
+
Versions affected
|
| 1142 |
+
~~~~~~~~~~~~~~~~~
|
| 1143 |
+
|
| 1144 |
+
* Django 1.3 :commit:`(patch) <2da4ace0bc1bc1d79bf43b368cb857f6f0cd6b1b>`
|
| 1145 |
+
* Django 1.4 :commit:`(patch) <319627c184e71ae267d6b7f000e293168c7b6e09>`
|
| 1146 |
+
|
| 1147 |
+
October 17, 2012 - :cve:`2012-4520`
|
| 1148 |
+
-----------------------------------
|
| 1149 |
+
|
| 1150 |
+
``Host`` header poisoning. `Full description
|
| 1151 |
+
<https://www.djangoproject.com/weblog/2012/oct/17/security/>`__
|
| 1152 |
+
|
| 1153 |
+
Versions affected
|
| 1154 |
+
~~~~~~~~~~~~~~~~~
|
| 1155 |
+
|
| 1156 |
+
* Django 1.3 :commit:`(patch) <b45c377f8f488955e0c7069cad3f3dd21910b071>`
|
| 1157 |
+
* Django 1.4 :commit:`(patch) <92d3430f12171f16f566c9050c40feefb830a4a3>`
|
| 1158 |
+
|
| 1159 |
+
July 30, 2012 - :cve:`2012-3444`
|
| 1160 |
+
--------------------------------
|
| 1161 |
+
|
| 1162 |
+
Denial-of-service via large image files. `Full description
|
| 1163 |
+
<https://www.djangoproject.com/weblog/2012/jul/30/security-releases-issued/>`__
|
| 1164 |
+
|
| 1165 |
+
Versions affected
|
| 1166 |
+
~~~~~~~~~~~~~~~~~
|
| 1167 |
+
|
| 1168 |
+
* Django 1.3 :commit:`(patch) <9ca0ff6268eeff92d0d0ac2c315d4b6a8e229155>`
|
| 1169 |
+
* Django 1.4 :commit:`(patch) <da33d67181b53fe6cc737ac1220153814a1509f6>`
|
| 1170 |
+
|
| 1171 |
+
July 30, 2012 - :cve:`2012-3443`
|
| 1172 |
+
--------------------------------
|
| 1173 |
+
|
| 1174 |
+
Denial-of-service via compressed image files. `Full description
|
| 1175 |
+
<https://www.djangoproject.com/weblog/2012/jul/30/security-releases-issued/>`__
|
| 1176 |
+
|
| 1177 |
+
Versions affected
|
| 1178 |
+
~~~~~~~~~~~~~~~~~
|
| 1179 |
+
|
| 1180 |
+
* Django 1.3: :commit:`(patch) <b2eb4787a0fff9c9993b78be5c698e85108f3446>`
|
| 1181 |
+
* Django 1.4: :commit:`(patch) <c14f325c4eef628bc7bfd8873c3a72aeb0219141>`
|
| 1182 |
+
|
| 1183 |
+
July 30, 2012 - :cve:`2012-3442`
|
| 1184 |
+
--------------------------------
|
| 1185 |
+
|
| 1186 |
+
XSS via failure to validate redirect scheme. `Full description
|
| 1187 |
+
<https://www.djangoproject.com/weblog/2012/jul/30/security-releases-issued/>`__
|
| 1188 |
+
|
| 1189 |
+
Versions affected
|
| 1190 |
+
~~~~~~~~~~~~~~~~~
|
| 1191 |
+
|
| 1192 |
+
* Django 1.3: :commit:`(patch) <4dea4883e6c50d75f215a6b9bcbd95273f57c72d>`
|
| 1193 |
+
* Django 1.4: :commit:`(patch) <e34685034b60be1112160e76091e5aee60149fa1>`
|
| 1194 |
+
|
| 1195 |
+
September 9, 2011 - :cve:`2011-4140`
|
| 1196 |
+
------------------------------------
|
| 1197 |
+
|
| 1198 |
+
Potential CSRF via ``Host`` header. `Full description
|
| 1199 |
+
<https://www.djangoproject.com/weblog/2011/sep/09/security-releases-issued/>`__
|
| 1200 |
+
|
| 1201 |
+
Versions affected
|
| 1202 |
+
~~~~~~~~~~~~~~~~~
|
| 1203 |
+
|
| 1204 |
+
This notification was an advisory only, so no patches were issued.
|
| 1205 |
+
|
| 1206 |
+
* Django 1.2
|
| 1207 |
+
* Django 1.3
|
| 1208 |
+
|
| 1209 |
+
September 9, 2011 - :cve:`2011-4139`
|
| 1210 |
+
------------------------------------
|
| 1211 |
+
|
| 1212 |
+
``Host`` header cache poisoning. `Full description
|
| 1213 |
+
<https://www.djangoproject.com/weblog/2011/sep/09/security-releases-issued/>`__
|
| 1214 |
+
|
| 1215 |
+
Versions affected
|
| 1216 |
+
~~~~~~~~~~~~~~~~~
|
| 1217 |
+
|
| 1218 |
+
* Django 1.2 :commit:`(patch) <c613af4d6485586c79d692b70a9acac429f3ca9d>`
|
| 1219 |
+
* Django 1.3 :commit:`(patch) <2f7fadc38efa58ac0a8f93f936b82332a199f396>`
|
| 1220 |
+
|
| 1221 |
+
September 9, 2011 - :cve:`2011-4138`
|
| 1222 |
+
------------------------------------
|
| 1223 |
+
|
| 1224 |
+
Information leakage/arbitrary request issuance via ``URLField.verify_exists``.
|
| 1225 |
+
`Full description
|
| 1226 |
+
<https://www.djangoproject.com/weblog/2011/sep/09/security-releases-issued/>`__
|
| 1227 |
+
|
| 1228 |
+
Versions affected
|
| 1229 |
+
~~~~~~~~~~~~~~~~~
|
| 1230 |
+
|
| 1231 |
+
* Django 1.2: :commit:`(patch) <7268f8af86186518821d775c530d5558fd726930>`
|
| 1232 |
+
* Django 1.3: :commit:`(patch) <1a76dbefdfc60e2d5954c0ba614c3d054ba9c3f0>`
|
| 1233 |
+
|
| 1234 |
+
September 9, 2011 - :cve:`2011-4137`
|
| 1235 |
+
------------------------------------
|
| 1236 |
+
|
| 1237 |
+
Denial-of-service via ``URLField.verify_exists``. `Full description
|
| 1238 |
+
<https://www.djangoproject.com/weblog/2011/sep/09/security-releases-issued/>`__
|
| 1239 |
+
|
| 1240 |
+
Versions affected
|
| 1241 |
+
~~~~~~~~~~~~~~~~~
|
| 1242 |
+
|
| 1243 |
+
* Django 1.2 :commit:`(patch) <7268f8af86186518821d775c530d5558fd726930>`
|
| 1244 |
+
* Django 1.3 :commit:`(patch) <1a76dbefdfc60e2d5954c0ba614c3d054ba9c3f0>`
|
| 1245 |
+
|
| 1246 |
+
September 9, 2011 - :cve:`2011-4136`
|
| 1247 |
+
------------------------------------
|
| 1248 |
+
|
| 1249 |
+
Session manipulation when using memory-cache-backed session. `Full description
|
| 1250 |
+
<https://www.djangoproject.com/weblog/2011/sep/09/security-releases-issued/>`__
|
| 1251 |
+
|
| 1252 |
+
Versions affected
|
| 1253 |
+
~~~~~~~~~~~~~~~~~
|
| 1254 |
+
|
| 1255 |
+
* Django 1.2 :commit:`(patch) <ac7c3a110f906e4dfed3a17451bf7fd9fcb81296>`
|
| 1256 |
+
* Django 1.3 :commit:`(patch) <fbe2eead2fa9d808658ca582241bcacb02618840>`
|
| 1257 |
+
|
| 1258 |
+
February 8, 2011 - :cve:`2011-0698`
|
| 1259 |
+
-----------------------------------
|
| 1260 |
+
|
| 1261 |
+
Directory-traversal on Windows via incorrect path-separator handling. `Full
|
| 1262 |
+
description <https://www.djangoproject.com/weblog/2011/feb/08/security/>`__
|
| 1263 |
+
|
| 1264 |
+
Versions affected
|
| 1265 |
+
~~~~~~~~~~~~~~~~~
|
| 1266 |
+
|
| 1267 |
+
* Django 1.1 :commit:`(patch) <570a32a047ea56265646217264b0d3dab1a14dbd>`
|
| 1268 |
+
* Django 1.2 :commit:`(patch) <194566480b15cf4e294d3f03ff587019b74044b2>`
|
| 1269 |
+
|
| 1270 |
+
February 8, 2011 - :cve:`2011-0697`
|
| 1271 |
+
-----------------------------------
|
| 1272 |
+
|
| 1273 |
+
XSS via unsanitized names of uploaded files. `Full description
|
| 1274 |
+
<https://www.djangoproject.com/weblog/2011/feb/08/security/>`__
|
| 1275 |
+
|
| 1276 |
+
Versions affected
|
| 1277 |
+
~~~~~~~~~~~~~~~~~
|
| 1278 |
+
|
| 1279 |
+
* Django 1.1 :commit:`(patch) <1966786d2dde73e17f39cf340eb33fcb5d73904e>`
|
| 1280 |
+
* Django 1.2 :commit:`(patch) <1f814a9547842dcfabdae09573055984af9d3fab>`
|
| 1281 |
+
|
| 1282 |
+
February 8, 2011 - :cve:`2011-0696`
|
| 1283 |
+
-----------------------------------
|
| 1284 |
+
|
| 1285 |
+
CSRF via forged HTTP headers. `Full description
|
| 1286 |
+
<https://www.djangoproject.com/weblog/2011/feb/08/security/>`__
|
| 1287 |
+
|
| 1288 |
+
Versions affected
|
| 1289 |
+
~~~~~~~~~~~~~~~~~
|
| 1290 |
+
|
| 1291 |
+
* Django 1.1 :commit:`(patch) <408c5c873ce1437c7eee9544ff279ecbad7e150a>`
|
| 1292 |
+
* Django 1.2 :commit:`(patch) <818e70344e7193f6ebc73c82ed574e6ce3c91afc>`
|
| 1293 |
+
|
| 1294 |
+
December 22, 2010 - :cve:`2010-4535`
|
| 1295 |
+
------------------------------------
|
| 1296 |
+
|
| 1297 |
+
Denial-of-service in password-reset mechanism. `Full description
|
| 1298 |
+
<https://www.djangoproject.com/weblog/2010/dec/22/security/>`__
|
| 1299 |
+
|
| 1300 |
+
Versions affected
|
| 1301 |
+
~~~~~~~~~~~~~~~~~
|
| 1302 |
+
|
| 1303 |
+
* Django 1.1 :commit:`(patch) <7f8dd9cbac074389af8d8fd235bf2cb657227b9a>`
|
| 1304 |
+
* Django 1.2 :commit:`(patch) <d5d8942a160685c403d381a279e72e09de5489a9>`
|
| 1305 |
+
|
| 1306 |
+
December 22, 2010 - :cve:`2010-4534`
|
| 1307 |
+
------------------------------------
|
| 1308 |
+
|
| 1309 |
+
Information leakage in administrative interface. `Full description
|
| 1310 |
+
<https://www.djangoproject.com/weblog/2010/dec/22/security/>`__
|
| 1311 |
+
|
| 1312 |
+
Versions affected
|
| 1313 |
+
~~~~~~~~~~~~~~~~~
|
| 1314 |
+
|
| 1315 |
+
* Django 1.1 :commit:`(patch) <17084839fd7e267da5729f2a27753322b9d415a0>`
|
| 1316 |
+
* Django 1.2 :commit:`(patch) <85207a245bf09fdebe486b4c7bbcb65300f2a693>`
|
| 1317 |
+
|
| 1318 |
+
September 8, 2010 - :cve:`2010-3082`
|
| 1319 |
+
------------------------------------
|
| 1320 |
+
|
| 1321 |
+
XSS via trusting unsafe cookie value. `Full description
|
| 1322 |
+
<https://www.djangoproject.com/weblog/2010/sep/08/security-release/>`__
|
| 1323 |
+
|
| 1324 |
+
Versions affected
|
| 1325 |
+
~~~~~~~~~~~~~~~~~
|
| 1326 |
+
|
| 1327 |
+
* Django 1.2 :commit:`(patch) <7f84657b6b2243cc787bdb9f296710c8d13ad0bd>`
|
| 1328 |
+
|
| 1329 |
+
October 9, 2009 - :cve:`2009-3965`
|
| 1330 |
+
----------------------------------
|
| 1331 |
+
|
| 1332 |
+
Denial-of-service via pathological regular expression performance. `Full
|
| 1333 |
+
description <https://www.djangoproject.com/weblog/2009/oct/09/security/>`__
|
| 1334 |
+
|
| 1335 |
+
Versions affected
|
| 1336 |
+
~~~~~~~~~~~~~~~~~
|
| 1337 |
+
|
| 1338 |
+
* Django 1.0 :commit:`(patch) <594a28a9044120bed58671dde8a805c9e0f6c79a>`
|
| 1339 |
+
* Django 1.1 :commit:`(patch) <e3e992e18b368fcd56aabafc1b5bf80a6e11b495>`
|
| 1340 |
+
|
| 1341 |
+
July 28, 2009 - :cve:`2009-2659`
|
| 1342 |
+
--------------------------------
|
| 1343 |
+
|
| 1344 |
+
Directory-traversal in development server media handler. `Full description
|
| 1345 |
+
<https://www.djangoproject.com/weblog/2009/jul/28/security/>`__
|
| 1346 |
+
|
| 1347 |
+
Versions affected
|
| 1348 |
+
~~~~~~~~~~~~~~~~~
|
| 1349 |
+
|
| 1350 |
+
* Django 0.96 :commit:`(patch) <da85d76fd6ca846f3b0ff414e042ddb5e62e2e69>`
|
| 1351 |
+
* Django 1.0 :commit:`(patch) <df7f917b7f51ba969faa49d000ffc79572c5dcb4>`
|
| 1352 |
+
|
| 1353 |
+
September 2, 2008 - :cve:`2008-3909`
|
| 1354 |
+
------------------------------------
|
| 1355 |
+
|
| 1356 |
+
CSRF via preservation of POST data during admin login. `Full description
|
| 1357 |
+
<https://www.djangoproject.com/weblog/2008/sep/02/security/>`__
|
| 1358 |
+
|
| 1359 |
+
Versions affected
|
| 1360 |
+
~~~~~~~~~~~~~~~~~
|
| 1361 |
+
|
| 1362 |
+
* Django 0.91 :commit:`(patch) <44debfeaa4473bd28872c735dd3d9afde6886752>`
|
| 1363 |
+
* Django 0.95 :commit:`(patch) <aee48854a164382c655acb9f18b3c06c3d238e81>`
|
| 1364 |
+
* Django 0.96 :commit:`(patch) <7e0972bded362bc4b851c109df2c8a6548481a8e>`
|
| 1365 |
+
|
| 1366 |
+
May 14, 2008 - :cve:`2008-2302`
|
| 1367 |
+
-------------------------------
|
| 1368 |
+
|
| 1369 |
+
XSS via admin login redirect. `Full description
|
| 1370 |
+
<https://www.djangoproject.com/weblog/2008/may/14/security/>`__
|
| 1371 |
+
|
| 1372 |
+
Versions affected
|
| 1373 |
+
~~~~~~~~~~~~~~~~~
|
| 1374 |
+
|
| 1375 |
+
* Django 0.91 :commit:`(patch) <6e657e2c404a96e744748209e896d8a69c15fdf2>`
|
| 1376 |
+
* Django 0.95 :commit:`(patch) <50ce7fb57d79e8940ccf6e2781f2f01df029b5c5>`
|
| 1377 |
+
* Django 0.96 :commit:`(patch) <7791e5c050cebf86d868c5dab7092185b125fdc9>`
|
| 1378 |
+
|
| 1379 |
+
October 26, 2007 - :cve:`2007-5712`
|
| 1380 |
+
-----------------------------------
|
| 1381 |
+
|
| 1382 |
+
Denial-of-service via arbitrarily-large ``Accept-Language`` header. `Full
|
| 1383 |
+
description <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>`__
|
| 1384 |
+
|
| 1385 |
+
Versions affected
|
| 1386 |
+
~~~~~~~~~~~~~~~~~
|
| 1387 |
+
|
| 1388 |
+
* Django 0.91 :commit:`(patch) <8bc36e726c9e8c75c681d3ad232df8e882aaac81>`
|
| 1389 |
+
* Django 0.95 :commit:`(patch) <412ed22502e11c50dbfee854627594f0e7e2c234>`
|
| 1390 |
+
* Django 0.96 :commit:`(patch) <7dd2dd08a79e388732ce00e2b5514f15bd6d0f6f>`
|
| 1391 |
+
|
| 1392 |
+
Issues prior to Django's security process
|
| 1393 |
+
=========================================
|
| 1394 |
+
|
| 1395 |
+
Some security issues were handled before Django had a formalized
|
| 1396 |
+
security process in use. For these, new releases may not have been
|
| 1397 |
+
issued at the time and CVEs may not have been assigned.
|
| 1398 |
+
|
| 1399 |
+
January 21, 2007 - :cve:`2007-0405`
|
| 1400 |
+
-----------------------------------
|
| 1401 |
+
|
| 1402 |
+
Apparent "caching" of authenticated user. `Full description
|
| 1403 |
+
<https://www.djangoproject.com/weblog/2007/jan/21/0951/>`__
|
| 1404 |
+
|
| 1405 |
+
Versions affected
|
| 1406 |
+
~~~~~~~~~~~~~~~~~
|
| 1407 |
+
|
| 1408 |
+
* Django 0.95 :commit:`(patch) <e89f0a65581f82a5740bfe989136cea75d09cd67>`
|
| 1409 |
+
|
| 1410 |
+
August 16, 2006 - :cve:`2007-0404`
|
| 1411 |
+
----------------------------------
|
| 1412 |
+
|
| 1413 |
+
Filename validation issue in translation framework. `Full description
|
| 1414 |
+
<https://www.djangoproject.com/weblog/2006/aug/16/compilemessages/>`__
|
| 1415 |
+
|
| 1416 |
+
Versions affected
|
| 1417 |
+
~~~~~~~~~~~~~~~~~
|
| 1418 |
+
|
| 1419 |
+
* Django 0.90 :commit:`(patch) <6eefa521be3c658dc0b38f8d62d52e9801e198ab>`
|
| 1420 |
+
* Django 0.91 :commit:`(patch) <d31e39173c29537e6a1613278c93634c18a3206e>`
|
| 1421 |
+
* Django 0.95 :commit:`(patch) <a132d411c6986418ee6c0edc331080aa792fee6e>`
|
| 1422 |
+
(released January 21 2007)
|
testbed/django__django/docs/topics/auth/customizing.txt
ADDED
|
@@ -0,0 +1,1213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
====================================
|
| 2 |
+
Customizing authentication in Django
|
| 3 |
+
====================================
|
| 4 |
+
|
| 5 |
+
The authentication that comes with Django is good enough for most common cases,
|
| 6 |
+
but you may have needs not met by the out-of-the-box defaults. Customizing
|
| 7 |
+
authentication in your projects requires understanding what points of the
|
| 8 |
+
provided system are extensible or replaceable. This document provides details
|
| 9 |
+
about how the auth system can be customized.
|
| 10 |
+
|
| 11 |
+
:ref:`Authentication backends <authentication-backends>` provide an extensible
|
| 12 |
+
system for when a username and password stored with the user model need to be
|
| 13 |
+
authenticated against a different service than Django's default.
|
| 14 |
+
|
| 15 |
+
You can give your models :ref:`custom permissions <custom-permissions>` that
|
| 16 |
+
can be checked through Django's authorization system.
|
| 17 |
+
|
| 18 |
+
You can :ref:`extend <extending-user>` the default ``User`` model, or
|
| 19 |
+
:ref:`substitute <auth-custom-user>` a completely customized model.
|
| 20 |
+
|
| 21 |
+
.. _authentication-backends:
|
| 22 |
+
|
| 23 |
+
Other authentication sources
|
| 24 |
+
============================
|
| 25 |
+
|
| 26 |
+
There may be times you have the need to hook into another authentication source
|
| 27 |
+
-- that is, another source of usernames and passwords or authentication
|
| 28 |
+
methods.
|
| 29 |
+
|
| 30 |
+
For example, your company may already have an LDAP setup that stores a username
|
| 31 |
+
and password for every employee. It'd be a hassle for both the network
|
| 32 |
+
administrator and the users themselves if users had separate accounts in LDAP
|
| 33 |
+
and the Django-based applications.
|
| 34 |
+
|
| 35 |
+
So, to handle situations like this, the Django authentication system lets you
|
| 36 |
+
plug in other authentication sources. You can override Django's default
|
| 37 |
+
database-based scheme, or you can use the default system in tandem with other
|
| 38 |
+
systems.
|
| 39 |
+
|
| 40 |
+
See the :ref:`authentication backend reference
|
| 41 |
+
<authentication-backends-reference>` for information on the authentication
|
| 42 |
+
backends included with Django.
|
| 43 |
+
|
| 44 |
+
Specifying authentication backends
|
| 45 |
+
----------------------------------
|
| 46 |
+
|
| 47 |
+
Behind the scenes, Django maintains a list of "authentication backends" that it
|
| 48 |
+
checks for authentication. When somebody calls
|
| 49 |
+
:func:`django.contrib.auth.authenticate()` -- as described in :ref:`How to log
|
| 50 |
+
a user in <how-to-log-a-user-in>` -- Django tries authenticating across
|
| 51 |
+
all of its authentication backends. If the first authentication method fails,
|
| 52 |
+
Django tries the second one, and so on, until all backends have been attempted.
|
| 53 |
+
|
| 54 |
+
The list of authentication backends to use is specified in the
|
| 55 |
+
:setting:`AUTHENTICATION_BACKENDS` setting. This should be a list of Python
|
| 56 |
+
path names that point to Python classes that know how to authenticate. These
|
| 57 |
+
classes can be anywhere on your Python path.
|
| 58 |
+
|
| 59 |
+
By default, :setting:`AUTHENTICATION_BACKENDS` is set to::
|
| 60 |
+
|
| 61 |
+
["django.contrib.auth.backends.ModelBackend"]
|
| 62 |
+
|
| 63 |
+
That's the basic authentication backend that checks the Django users database
|
| 64 |
+
and queries the built-in permissions. It does not provide protection against
|
| 65 |
+
brute force attacks via any rate limiting mechanism. You may either implement
|
| 66 |
+
your own rate limiting mechanism in a custom auth backend, or use the
|
| 67 |
+
mechanisms provided by most web servers.
|
| 68 |
+
|
| 69 |
+
The order of :setting:`AUTHENTICATION_BACKENDS` matters, so if the same
|
| 70 |
+
username and password is valid in multiple backends, Django will stop
|
| 71 |
+
processing at the first positive match.
|
| 72 |
+
|
| 73 |
+
If a backend raises a :class:`~django.core.exceptions.PermissionDenied`
|
| 74 |
+
exception, authentication will immediately fail. Django won't check the
|
| 75 |
+
backends that follow.
|
| 76 |
+
|
| 77 |
+
.. note::
|
| 78 |
+
|
| 79 |
+
Once a user has authenticated, Django stores which backend was used to
|
| 80 |
+
authenticate the user in the user's session, and reuses the same backend
|
| 81 |
+
for the duration of that session whenever access to the currently
|
| 82 |
+
authenticated user is needed. This effectively means that authentication
|
| 83 |
+
sources are cached on a per-session basis, so if you change
|
| 84 |
+
:setting:`AUTHENTICATION_BACKENDS`, you'll need to clear out session data if
|
| 85 |
+
you need to force users to re-authenticate using different methods. A
|
| 86 |
+
simple way to do that is to execute ``Session.objects.all().delete()``.
|
| 87 |
+
|
| 88 |
+
Writing an authentication backend
|
| 89 |
+
---------------------------------
|
| 90 |
+
|
| 91 |
+
An authentication backend is a class that implements two required methods:
|
| 92 |
+
``get_user(user_id)`` and ``authenticate(request, **credentials)``, as well as
|
| 93 |
+
a set of optional permission related :ref:`authorization methods
|
| 94 |
+
<authorization_methods>`.
|
| 95 |
+
|
| 96 |
+
The ``get_user`` method takes a ``user_id`` -- which could be a username,
|
| 97 |
+
database ID or whatever, but has to be the primary key of your user object --
|
| 98 |
+
and returns a user object or ``None``.
|
| 99 |
+
|
| 100 |
+
The ``authenticate`` method takes a ``request`` argument and credentials as
|
| 101 |
+
keyword arguments. Most of the time, it'll look like this::
|
| 102 |
+
|
| 103 |
+
from django.contrib.auth.backends import BaseBackend
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
class MyBackend(BaseBackend):
|
| 107 |
+
def authenticate(self, request, username=None, password=None):
|
| 108 |
+
# Check the username/password and return a user.
|
| 109 |
+
...
|
| 110 |
+
|
| 111 |
+
But it could also authenticate a token, like so::
|
| 112 |
+
|
| 113 |
+
from django.contrib.auth.backends import BaseBackend
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class MyBackend(BaseBackend):
|
| 117 |
+
def authenticate(self, request, token=None):
|
| 118 |
+
# Check the token and return a user.
|
| 119 |
+
...
|
| 120 |
+
|
| 121 |
+
Either way, ``authenticate()`` should check the credentials it gets and return
|
| 122 |
+
a user object that matches those credentials if the credentials are valid. If
|
| 123 |
+
they're not valid, it should return ``None``.
|
| 124 |
+
|
| 125 |
+
``request`` is an :class:`~django.http.HttpRequest` and may be ``None`` if it
|
| 126 |
+
wasn't provided to :func:`~django.contrib.auth.authenticate` (which passes it
|
| 127 |
+
on to the backend).
|
| 128 |
+
|
| 129 |
+
The Django admin is tightly coupled to the Django :ref:`User object
|
| 130 |
+
<user-objects>`. The best way to deal with this is to create a Django ``User``
|
| 131 |
+
object for each user that exists for your backend (e.g., in your LDAP
|
| 132 |
+
directory, your external SQL database, etc.) You can either write a script to
|
| 133 |
+
do this in advance, or your ``authenticate`` method can do it the first time a
|
| 134 |
+
user logs in.
|
| 135 |
+
|
| 136 |
+
Here's an example backend that authenticates against a username and password
|
| 137 |
+
variable defined in your ``settings.py`` file and creates a Django ``User``
|
| 138 |
+
object the first time a user authenticates::
|
| 139 |
+
|
| 140 |
+
from django.conf import settings
|
| 141 |
+
from django.contrib.auth.backends import BaseBackend
|
| 142 |
+
from django.contrib.auth.hashers import check_password
|
| 143 |
+
from django.contrib.auth.models import User
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
class SettingsBackend(BaseBackend):
|
| 147 |
+
"""
|
| 148 |
+
Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.
|
| 149 |
+
|
| 150 |
+
Use the login name and a hash of the password. For example:
|
| 151 |
+
|
| 152 |
+
ADMIN_LOGIN = 'admin'
|
| 153 |
+
ADMIN_PASSWORD = 'pbkdf2_sha256$30000$Vo0VlMnkR4Bk$qEvtdyZRWTcOsCnI/oQ7fVOu1XAURIZYoOZ3iq8Dr4M='
|
| 154 |
+
"""
|
| 155 |
+
|
| 156 |
+
def authenticate(self, request, username=None, password=None):
|
| 157 |
+
login_valid = settings.ADMIN_LOGIN == username
|
| 158 |
+
pwd_valid = check_password(password, settings.ADMIN_PASSWORD)
|
| 159 |
+
if login_valid and pwd_valid:
|
| 160 |
+
try:
|
| 161 |
+
user = User.objects.get(username=username)
|
| 162 |
+
except User.DoesNotExist:
|
| 163 |
+
# Create a new user. There's no need to set a password
|
| 164 |
+
# because only the password from settings.py is checked.
|
| 165 |
+
user = User(username=username)
|
| 166 |
+
user.is_staff = True
|
| 167 |
+
user.is_superuser = True
|
| 168 |
+
user.save()
|
| 169 |
+
return user
|
| 170 |
+
return None
|
| 171 |
+
|
| 172 |
+
def get_user(self, user_id):
|
| 173 |
+
try:
|
| 174 |
+
return User.objects.get(pk=user_id)
|
| 175 |
+
except User.DoesNotExist:
|
| 176 |
+
return None
|
| 177 |
+
|
| 178 |
+
.. _authorization_methods:
|
| 179 |
+
|
| 180 |
+
Handling authorization in custom backends
|
| 181 |
+
-----------------------------------------
|
| 182 |
+
|
| 183 |
+
Custom auth backends can provide their own permissions.
|
| 184 |
+
|
| 185 |
+
The user model and its manager will delegate permission lookup functions
|
| 186 |
+
(:meth:`~django.contrib.auth.models.User.get_user_permissions()`,
|
| 187 |
+
:meth:`~django.contrib.auth.models.User.get_group_permissions()`,
|
| 188 |
+
:meth:`~django.contrib.auth.models.User.get_all_permissions()`,
|
| 189 |
+
:meth:`~django.contrib.auth.models.User.has_perm()`,
|
| 190 |
+
:meth:`~django.contrib.auth.models.User.has_module_perms()`, and
|
| 191 |
+
:meth:`~django.contrib.auth.models.UserManager.with_perm()`) to any
|
| 192 |
+
authentication backend that implements these functions.
|
| 193 |
+
|
| 194 |
+
The permissions given to the user will be the superset of all permissions
|
| 195 |
+
returned by all backends. That is, Django grants a permission to a user that
|
| 196 |
+
any one backend grants.
|
| 197 |
+
|
| 198 |
+
If a backend raises a :class:`~django.core.exceptions.PermissionDenied`
|
| 199 |
+
exception in :meth:`~django.contrib.auth.models.User.has_perm()` or
|
| 200 |
+
:meth:`~django.contrib.auth.models.User.has_module_perms()`, the authorization
|
| 201 |
+
will immediately fail and Django won't check the backends that follow.
|
| 202 |
+
|
| 203 |
+
A backend could implement permissions for the magic admin like this::
|
| 204 |
+
|
| 205 |
+
from django.contrib.auth.backends import BaseBackend
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
class MagicAdminBackend(BaseBackend):
|
| 209 |
+
def has_perm(self, user_obj, perm, obj=None):
|
| 210 |
+
return user_obj.username == settings.ADMIN_LOGIN
|
| 211 |
+
|
| 212 |
+
This gives full permissions to the user granted access in the above example.
|
| 213 |
+
Notice that in addition to the same arguments given to the associated
|
| 214 |
+
:class:`django.contrib.auth.models.User` functions, the backend auth functions
|
| 215 |
+
all take the user object, which may be an anonymous user, as an argument.
|
| 216 |
+
|
| 217 |
+
A full authorization implementation can be found in the ``ModelBackend`` class
|
| 218 |
+
in :source:`django/contrib/auth/backends.py`, which is the default backend and
|
| 219 |
+
queries the ``auth_permission`` table most of the time.
|
| 220 |
+
|
| 221 |
+
.. _anonymous_auth:
|
| 222 |
+
|
| 223 |
+
Authorization for anonymous users
|
| 224 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 225 |
+
|
| 226 |
+
An anonymous user is one that is not authenticated i.e. they have provided no
|
| 227 |
+
valid authentication details. However, that does not necessarily mean they are
|
| 228 |
+
not authorized to do anything. At the most basic level, most websites
|
| 229 |
+
authorize anonymous users to browse most of the site, and many allow anonymous
|
| 230 |
+
posting of comments etc.
|
| 231 |
+
|
| 232 |
+
Django's permission framework does not have a place to store permissions for
|
| 233 |
+
anonymous users. However, the user object passed to an authentication backend
|
| 234 |
+
may be an :class:`django.contrib.auth.models.AnonymousUser` object, allowing
|
| 235 |
+
the backend to specify custom authorization behavior for anonymous users. This
|
| 236 |
+
is especially useful for the authors of reusable apps, who can delegate all
|
| 237 |
+
questions of authorization to the auth backend, rather than needing settings,
|
| 238 |
+
for example, to control anonymous access.
|
| 239 |
+
|
| 240 |
+
.. _inactive_auth:
|
| 241 |
+
|
| 242 |
+
Authorization for inactive users
|
| 243 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 244 |
+
|
| 245 |
+
An inactive user is one that has its
|
| 246 |
+
:attr:`~django.contrib.auth.models.User.is_active` field set to ``False``. The
|
| 247 |
+
:class:`~django.contrib.auth.backends.ModelBackend` and
|
| 248 |
+
:class:`~django.contrib.auth.backends.RemoteUserBackend` authentication
|
| 249 |
+
backends prohibits these users from authenticating. If a custom user model
|
| 250 |
+
doesn't have an :attr:`~django.contrib.auth.models.CustomUser.is_active` field,
|
| 251 |
+
all users will be allowed to authenticate.
|
| 252 |
+
|
| 253 |
+
You can use :class:`~django.contrib.auth.backends.AllowAllUsersModelBackend`
|
| 254 |
+
or :class:`~django.contrib.auth.backends.AllowAllUsersRemoteUserBackend` if you
|
| 255 |
+
want to allow inactive users to authenticate.
|
| 256 |
+
|
| 257 |
+
The support for anonymous users in the permission system allows for a scenario
|
| 258 |
+
where anonymous users have permissions to do something while inactive
|
| 259 |
+
authenticated users do not.
|
| 260 |
+
|
| 261 |
+
Do not forget to test for the ``is_active`` attribute of the user in your own
|
| 262 |
+
backend permission methods.
|
| 263 |
+
|
| 264 |
+
Handling object permissions
|
| 265 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 266 |
+
|
| 267 |
+
Django's permission framework has a foundation for object permissions, though
|
| 268 |
+
there is no implementation for it in the core. That means that checking for
|
| 269 |
+
object permissions will always return ``False`` or an empty list (depending on
|
| 270 |
+
the check performed). An authentication backend will receive the keyword
|
| 271 |
+
parameters ``obj`` and ``user_obj`` for each object related authorization
|
| 272 |
+
method and can return the object level permission as appropriate.
|
| 273 |
+
|
| 274 |
+
.. _custom-permissions:
|
| 275 |
+
|
| 276 |
+
Custom permissions
|
| 277 |
+
==================
|
| 278 |
+
|
| 279 |
+
To create custom permissions for a given model object, use the ``permissions``
|
| 280 |
+
:ref:`model Meta attribute <meta-options>`.
|
| 281 |
+
|
| 282 |
+
This example ``Task`` model creates two custom permissions, i.e., actions users
|
| 283 |
+
can or cannot do with ``Task`` instances, specific to your application::
|
| 284 |
+
|
| 285 |
+
class Task(models.Model):
|
| 286 |
+
...
|
| 287 |
+
|
| 288 |
+
class Meta:
|
| 289 |
+
permissions = [
|
| 290 |
+
("change_task_status", "Can change the status of tasks"),
|
| 291 |
+
("close_task", "Can remove a task by setting its status as closed"),
|
| 292 |
+
]
|
| 293 |
+
|
| 294 |
+
The only thing this does is create those extra permissions when you run
|
| 295 |
+
:djadmin:`manage.py migrate <migrate>` (the function that creates permissions
|
| 296 |
+
is connected to the :data:`~django.db.models.signals.post_migrate` signal).
|
| 297 |
+
Your code is in charge of checking the value of these permissions when a user
|
| 298 |
+
is trying to access the functionality provided by the application (changing the
|
| 299 |
+
status of tasks or closing tasks.) Continuing the above example, the following
|
| 300 |
+
checks if a user may close tasks::
|
| 301 |
+
|
| 302 |
+
user.has_perm("app.close_task")
|
| 303 |
+
|
| 304 |
+
.. _extending-user:
|
| 305 |
+
|
| 306 |
+
Extending the existing ``User`` model
|
| 307 |
+
=====================================
|
| 308 |
+
|
| 309 |
+
There are two ways to extend the default
|
| 310 |
+
:class:`~django.contrib.auth.models.User` model without substituting your own
|
| 311 |
+
model. If the changes you need are purely behavioral, and don't require any
|
| 312 |
+
change to what is stored in the database, you can create a :ref:`proxy model
|
| 313 |
+
<proxy-models>` based on :class:`~django.contrib.auth.models.User`. This
|
| 314 |
+
allows for any of the features offered by proxy models including default
|
| 315 |
+
ordering, custom managers, or custom model methods.
|
| 316 |
+
|
| 317 |
+
If you wish to store information related to ``User``, you can use a
|
| 318 |
+
:class:`~django.db.models.OneToOneField` to a model containing the fields for
|
| 319 |
+
additional information. This one-to-one model is often called a profile model,
|
| 320 |
+
as it might store non-auth related information about a site user. For example
|
| 321 |
+
you might create an Employee model::
|
| 322 |
+
|
| 323 |
+
from django.contrib.auth.models import User
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
class Employee(models.Model):
|
| 327 |
+
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
| 328 |
+
department = models.CharField(max_length=100)
|
| 329 |
+
|
| 330 |
+
Assuming an existing Employee Fred Smith who has both a User and Employee
|
| 331 |
+
model, you can access the related information using Django's standard related
|
| 332 |
+
model conventions:
|
| 333 |
+
|
| 334 |
+
.. code-block:: pycon
|
| 335 |
+
|
| 336 |
+
>>> u = User.objects.get(username="fsmith")
|
| 337 |
+
>>> freds_department = u.employee.department
|
| 338 |
+
|
| 339 |
+
To add a profile model's fields to the user page in the admin, define an
|
| 340 |
+
:class:`~django.contrib.admin.InlineModelAdmin` (for this example, we'll use a
|
| 341 |
+
:class:`~django.contrib.admin.StackedInline`) in your app's ``admin.py`` and
|
| 342 |
+
add it to a ``UserAdmin`` class which is registered with the
|
| 343 |
+
:class:`~django.contrib.auth.models.User` class::
|
| 344 |
+
|
| 345 |
+
from django.contrib import admin
|
| 346 |
+
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
| 347 |
+
from django.contrib.auth.models import User
|
| 348 |
+
|
| 349 |
+
from my_user_profile_app.models import Employee
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
# Define an inline admin descriptor for Employee model
|
| 353 |
+
# which acts a bit like a singleton
|
| 354 |
+
class EmployeeInline(admin.StackedInline):
|
| 355 |
+
model = Employee
|
| 356 |
+
can_delete = False
|
| 357 |
+
verbose_name_plural = "employee"
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
# Define a new User admin
|
| 361 |
+
class UserAdmin(BaseUserAdmin):
|
| 362 |
+
inlines = [EmployeeInline]
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
# Re-register UserAdmin
|
| 366 |
+
admin.site.unregister(User)
|
| 367 |
+
admin.site.register(User, UserAdmin)
|
| 368 |
+
|
| 369 |
+
These profile models are not special in any way - they are just Django models
|
| 370 |
+
that happen to have a one-to-one link with a user model. As such, they aren't
|
| 371 |
+
auto created when a user is created, but
|
| 372 |
+
a :attr:`django.db.models.signals.post_save` could be used to create or update
|
| 373 |
+
related models as appropriate.
|
| 374 |
+
|
| 375 |
+
Using related models results in additional queries or joins to retrieve the
|
| 376 |
+
related data. Depending on your needs, a custom user model that includes the
|
| 377 |
+
related fields may be your better option, however, existing relations to the
|
| 378 |
+
default user model within your project's apps may justify the extra database
|
| 379 |
+
load.
|
| 380 |
+
|
| 381 |
+
.. _auth-custom-user:
|
| 382 |
+
|
| 383 |
+
Substituting a custom ``User`` model
|
| 384 |
+
====================================
|
| 385 |
+
|
| 386 |
+
Some kinds of projects may have authentication requirements for which Django's
|
| 387 |
+
built-in :class:`~django.contrib.auth.models.User` model is not always
|
| 388 |
+
appropriate. For instance, on some sites it makes more sense to use an email
|
| 389 |
+
address as your identification token instead of a username.
|
| 390 |
+
|
| 391 |
+
Django allows you to override the default user model by providing a value for
|
| 392 |
+
the :setting:`AUTH_USER_MODEL` setting that references a custom model::
|
| 393 |
+
|
| 394 |
+
AUTH_USER_MODEL = "myapp.MyUser"
|
| 395 |
+
|
| 396 |
+
This dotted pair describes the :attr:`~django.apps.AppConfig.label` of the
|
| 397 |
+
Django app (which must be in your :setting:`INSTALLED_APPS`), and the name of
|
| 398 |
+
the Django model that you wish to use as your user model.
|
| 399 |
+
|
| 400 |
+
Using a custom user model when starting a project
|
| 401 |
+
-------------------------------------------------
|
| 402 |
+
|
| 403 |
+
If you're starting a new project, it's highly recommended to set up a custom
|
| 404 |
+
user model, even if the default :class:`~django.contrib.auth.models.User` model
|
| 405 |
+
is sufficient for you. This model behaves identically to the default user
|
| 406 |
+
model, but you'll be able to customize it in the future if the need arises::
|
| 407 |
+
|
| 408 |
+
from django.contrib.auth.models import AbstractUser
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
class User(AbstractUser):
|
| 412 |
+
pass
|
| 413 |
+
|
| 414 |
+
Don't forget to point :setting:`AUTH_USER_MODEL` to it. Do this before creating
|
| 415 |
+
any migrations or running ``manage.py migrate`` for the first time.
|
| 416 |
+
|
| 417 |
+
Also, register the model in the app's ``admin.py``::
|
| 418 |
+
|
| 419 |
+
from django.contrib import admin
|
| 420 |
+
from django.contrib.auth.admin import UserAdmin
|
| 421 |
+
from .models import User
|
| 422 |
+
|
| 423 |
+
admin.site.register(User, UserAdmin)
|
| 424 |
+
|
| 425 |
+
Changing to a custom user model mid-project
|
| 426 |
+
-------------------------------------------
|
| 427 |
+
|
| 428 |
+
Changing :setting:`AUTH_USER_MODEL` after you've created database tables is
|
| 429 |
+
significantly more difficult since it affects foreign keys and many-to-many
|
| 430 |
+
relationships, for example.
|
| 431 |
+
|
| 432 |
+
This change can't be done automatically and requires manually fixing your
|
| 433 |
+
schema, moving your data from the old user table, and possibly manually
|
| 434 |
+
reapplying some migrations. See :ticket:`25313` for an outline of the steps.
|
| 435 |
+
|
| 436 |
+
Due to limitations of Django's dynamic dependency feature for swappable
|
| 437 |
+
models, the model referenced by :setting:`AUTH_USER_MODEL` must be created in
|
| 438 |
+
the first migration of its app (usually called ``0001_initial``); otherwise,
|
| 439 |
+
you'll have dependency issues.
|
| 440 |
+
|
| 441 |
+
In addition, you may run into a ``CircularDependencyError`` when running your
|
| 442 |
+
migrations as Django won't be able to automatically break the dependency loop
|
| 443 |
+
due to the dynamic dependency. If you see this error, you should break the loop
|
| 444 |
+
by moving the models depended on by your user model into a second migration.
|
| 445 |
+
(You can try making two normal models that have a ``ForeignKey`` to each other
|
| 446 |
+
and seeing how ``makemigrations`` resolves that circular dependency if you want
|
| 447 |
+
to see how it's usually done.)
|
| 448 |
+
|
| 449 |
+
Reusable apps and ``AUTH_USER_MODEL``
|
| 450 |
+
-------------------------------------
|
| 451 |
+
|
| 452 |
+
Reusable apps shouldn't implement a custom user model. A project may use many
|
| 453 |
+
apps, and two reusable apps that implemented a custom user model couldn't be
|
| 454 |
+
used together. If you need to store per user information in your app, use
|
| 455 |
+
a :class:`~django.db.models.ForeignKey` or
|
| 456 |
+
:class:`~django.db.models.OneToOneField` to ``settings.AUTH_USER_MODEL``
|
| 457 |
+
as described below.
|
| 458 |
+
|
| 459 |
+
Referencing the ``User`` model
|
| 460 |
+
------------------------------
|
| 461 |
+
|
| 462 |
+
.. currentmodule:: django.contrib.auth
|
| 463 |
+
|
| 464 |
+
If you reference :class:`~django.contrib.auth.models.User` directly (for
|
| 465 |
+
example, by referring to it in a foreign key), your code will not work in
|
| 466 |
+
projects where the :setting:`AUTH_USER_MODEL` setting has been changed to a
|
| 467 |
+
different user model.
|
| 468 |
+
|
| 469 |
+
.. function:: get_user_model()
|
| 470 |
+
|
| 471 |
+
Instead of referring to :class:`~django.contrib.auth.models.User` directly,
|
| 472 |
+
you should reference the user model using
|
| 473 |
+
``django.contrib.auth.get_user_model()``. This method will return the
|
| 474 |
+
currently active user model -- the custom user model if one is specified, or
|
| 475 |
+
:class:`~django.contrib.auth.models.User` otherwise.
|
| 476 |
+
|
| 477 |
+
When you define a foreign key or many-to-many relations to the user model,
|
| 478 |
+
you should specify the custom model using the :setting:`AUTH_USER_MODEL`
|
| 479 |
+
setting. For example::
|
| 480 |
+
|
| 481 |
+
from django.conf import settings
|
| 482 |
+
from django.db import models
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
class Article(models.Model):
|
| 486 |
+
author = models.ForeignKey(
|
| 487 |
+
settings.AUTH_USER_MODEL,
|
| 488 |
+
on_delete=models.CASCADE,
|
| 489 |
+
)
|
| 490 |
+
|
| 491 |
+
When connecting to signals sent by the user model, you should specify
|
| 492 |
+
the custom model using the :setting:`AUTH_USER_MODEL` setting. For example::
|
| 493 |
+
|
| 494 |
+
from django.conf import settings
|
| 495 |
+
from django.db.models.signals import post_save
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
def post_save_receiver(sender, instance, created, **kwargs):
|
| 499 |
+
pass
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)
|
| 503 |
+
|
| 504 |
+
Generally speaking, it's easiest to refer to the user model with the
|
| 505 |
+
:setting:`AUTH_USER_MODEL` setting in code that's executed at import time,
|
| 506 |
+
however, it's also possible to call ``get_user_model()`` while Django
|
| 507 |
+
is importing models, so you could use
|
| 508 |
+
``models.ForeignKey(get_user_model(), ...)``.
|
| 509 |
+
|
| 510 |
+
If your app is tested with multiple user models, using
|
| 511 |
+
``@override_settings(AUTH_USER_MODEL=...)`` for example, and you cache the
|
| 512 |
+
result of ``get_user_model()`` in a module-level variable, you may need to
|
| 513 |
+
listen to the :data:`~django.test.signals.setting_changed` signal to clear
|
| 514 |
+
the cache. For example::
|
| 515 |
+
|
| 516 |
+
from django.apps import apps
|
| 517 |
+
from django.contrib.auth import get_user_model
|
| 518 |
+
from django.core.signals import setting_changed
|
| 519 |
+
from django.dispatch import receiver
|
| 520 |
+
|
| 521 |
+
|
| 522 |
+
@receiver(setting_changed)
|
| 523 |
+
def user_model_swapped(*, setting, **kwargs):
|
| 524 |
+
if setting == "AUTH_USER_MODEL":
|
| 525 |
+
apps.clear_cache()
|
| 526 |
+
from myapp import some_module
|
| 527 |
+
|
| 528 |
+
some_module.UserModel = get_user_model()
|
| 529 |
+
|
| 530 |
+
.. _specifying-custom-user-model:
|
| 531 |
+
|
| 532 |
+
Specifying a custom user model
|
| 533 |
+
------------------------------
|
| 534 |
+
|
| 535 |
+
When you start your project with a custom user model, stop to consider if this
|
| 536 |
+
is the right choice for your project.
|
| 537 |
+
|
| 538 |
+
Keeping all user related information in one model removes the need for
|
| 539 |
+
additional or more complex database queries to retrieve related models. On the
|
| 540 |
+
other hand, it may be more suitable to store app-specific user information in a
|
| 541 |
+
model that has a relation with your custom user model. That allows each app to
|
| 542 |
+
specify its own user data requirements without potentially conflicting or
|
| 543 |
+
breaking assumptions by other apps. It also means that you would keep your user
|
| 544 |
+
model as simple as possible, focused on authentication, and following the
|
| 545 |
+
minimum requirements Django expects custom user models to meet.
|
| 546 |
+
|
| 547 |
+
If you use the default authentication backend, then your model must have a
|
| 548 |
+
single unique field that can be used for identification purposes. This can
|
| 549 |
+
be a username, an email address, or any other unique attribute. A non-unique
|
| 550 |
+
username field is allowed if you use a custom authentication backend that
|
| 551 |
+
can support it.
|
| 552 |
+
|
| 553 |
+
The easiest way to construct a compliant custom user model is to inherit from
|
| 554 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser`.
|
| 555 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser` provides the core
|
| 556 |
+
implementation of a user model, including hashed passwords and tokenized
|
| 557 |
+
password resets. You must then provide some key implementation details:
|
| 558 |
+
|
| 559 |
+
.. currentmodule:: django.contrib.auth
|
| 560 |
+
|
| 561 |
+
.. class:: models.CustomUser
|
| 562 |
+
|
| 563 |
+
.. attribute:: USERNAME_FIELD
|
| 564 |
+
|
| 565 |
+
A string describing the name of the field on the user model that is
|
| 566 |
+
used as the unique identifier. This will usually be a username of some
|
| 567 |
+
kind, but it can also be an email address, or any other unique
|
| 568 |
+
identifier. The field *must* be unique (e.g. have ``unique=True`` set
|
| 569 |
+
in its definition), unless you use a custom authentication backend that
|
| 570 |
+
can support non-unique usernames.
|
| 571 |
+
|
| 572 |
+
In the following example, the field ``identifier`` is used
|
| 573 |
+
as the identifying field::
|
| 574 |
+
|
| 575 |
+
class MyUser(AbstractBaseUser):
|
| 576 |
+
identifier = models.CharField(max_length=40, unique=True)
|
| 577 |
+
...
|
| 578 |
+
USERNAME_FIELD = "identifier"
|
| 579 |
+
|
| 580 |
+
.. attribute:: EMAIL_FIELD
|
| 581 |
+
|
| 582 |
+
A string describing the name of the email field on the ``User`` model.
|
| 583 |
+
This value is returned by
|
| 584 |
+
:meth:`~models.AbstractBaseUser.get_email_field_name`.
|
| 585 |
+
|
| 586 |
+
.. attribute:: REQUIRED_FIELDS
|
| 587 |
+
|
| 588 |
+
A list of the field names that will be prompted for when creating a
|
| 589 |
+
user via the :djadmin:`createsuperuser` management command. The user
|
| 590 |
+
will be prompted to supply a value for each of these fields. It must
|
| 591 |
+
include any field for which :attr:`~django.db.models.Field.blank` is
|
| 592 |
+
``False`` or undefined and may include additional fields you want
|
| 593 |
+
prompted for when a user is created interactively.
|
| 594 |
+
``REQUIRED_FIELDS`` has no effect in other parts of Django, like
|
| 595 |
+
creating a user in the admin.
|
| 596 |
+
|
| 597 |
+
For example, here is the partial definition for a user model that
|
| 598 |
+
defines two required fields - a date of birth and height::
|
| 599 |
+
|
| 600 |
+
class MyUser(AbstractBaseUser):
|
| 601 |
+
...
|
| 602 |
+
date_of_birth = models.DateField()
|
| 603 |
+
height = models.FloatField()
|
| 604 |
+
...
|
| 605 |
+
REQUIRED_FIELDS = ["date_of_birth", "height"]
|
| 606 |
+
|
| 607 |
+
.. note::
|
| 608 |
+
|
| 609 |
+
``REQUIRED_FIELDS`` must contain all required fields on your user
|
| 610 |
+
model, but should *not* contain the ``USERNAME_FIELD`` or
|
| 611 |
+
``password`` as these fields will always be prompted for.
|
| 612 |
+
|
| 613 |
+
.. attribute:: is_active
|
| 614 |
+
|
| 615 |
+
A boolean attribute that indicates whether the user is considered
|
| 616 |
+
"active". This attribute is provided as an attribute on
|
| 617 |
+
``AbstractBaseUser`` defaulting to ``True``. How you choose to
|
| 618 |
+
implement it will depend on the details of your chosen auth backends.
|
| 619 |
+
See the documentation of the :attr:`is_active attribute on the built-in
|
| 620 |
+
user model <django.contrib.auth.models.User.is_active>` for details.
|
| 621 |
+
|
| 622 |
+
.. method:: get_full_name()
|
| 623 |
+
|
| 624 |
+
Optional. A longer formal identifier for the user such as their full
|
| 625 |
+
name. If implemented, this appears alongside the username in an
|
| 626 |
+
object's history in :mod:`django.contrib.admin`.
|
| 627 |
+
|
| 628 |
+
.. method:: get_short_name()
|
| 629 |
+
|
| 630 |
+
Optional. A short, informal identifier for the user such as their
|
| 631 |
+
first name. If implemented, this replaces the username in the greeting
|
| 632 |
+
to the user in the header of :mod:`django.contrib.admin`.
|
| 633 |
+
|
| 634 |
+
.. admonition:: Importing ``AbstractBaseUser``
|
| 635 |
+
|
| 636 |
+
``AbstractBaseUser`` and ``BaseUserManager`` are importable from
|
| 637 |
+
``django.contrib.auth.base_user`` so that they can be imported without
|
| 638 |
+
including ``django.contrib.auth`` in :setting:`INSTALLED_APPS`.
|
| 639 |
+
|
| 640 |
+
The following attributes and methods are available on any subclass of
|
| 641 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser`:
|
| 642 |
+
|
| 643 |
+
.. class:: models.AbstractBaseUser
|
| 644 |
+
|
| 645 |
+
.. method:: get_username()
|
| 646 |
+
|
| 647 |
+
Returns the value of the field nominated by ``USERNAME_FIELD``.
|
| 648 |
+
|
| 649 |
+
.. method:: clean()
|
| 650 |
+
|
| 651 |
+
Normalizes the username by calling :meth:`normalize_username`. If you
|
| 652 |
+
override this method, be sure to call ``super()`` to retain the
|
| 653 |
+
normalization.
|
| 654 |
+
|
| 655 |
+
.. classmethod:: get_email_field_name()
|
| 656 |
+
|
| 657 |
+
Returns the name of the email field specified by the
|
| 658 |
+
:attr:`~models.CustomUser.EMAIL_FIELD` attribute. Defaults to
|
| 659 |
+
``'email'`` if ``EMAIL_FIELD`` isn't specified.
|
| 660 |
+
|
| 661 |
+
.. classmethod:: normalize_username(username)
|
| 662 |
+
|
| 663 |
+
Applies NFKC Unicode normalization to usernames so that visually
|
| 664 |
+
identical characters with different Unicode code points are considered
|
| 665 |
+
identical.
|
| 666 |
+
|
| 667 |
+
.. attribute:: models.AbstractBaseUser.is_authenticated
|
| 668 |
+
|
| 669 |
+
Read-only attribute which is always ``True`` (as opposed to
|
| 670 |
+
``AnonymousUser.is_authenticated`` which is always ``False``).
|
| 671 |
+
This is a way to tell if the user has been authenticated. This does not
|
| 672 |
+
imply any permissions and doesn't check if the user is active or has
|
| 673 |
+
a valid session. Even though normally you will check this attribute on
|
| 674 |
+
``request.user`` to find out whether it has been populated by the
|
| 675 |
+
:class:`~django.contrib.auth.middleware.AuthenticationMiddleware`
|
| 676 |
+
(representing the currently logged-in user), you should know this
|
| 677 |
+
attribute is ``True`` for any :class:`~models.User` instance.
|
| 678 |
+
|
| 679 |
+
.. attribute:: models.AbstractBaseUser.is_anonymous
|
| 680 |
+
|
| 681 |
+
Read-only attribute which is always ``False``. This is a way of
|
| 682 |
+
differentiating :class:`~models.User` and :class:`~models.AnonymousUser`
|
| 683 |
+
objects. Generally, you should prefer using
|
| 684 |
+
:attr:`~models.User.is_authenticated` to this attribute.
|
| 685 |
+
|
| 686 |
+
.. method:: models.AbstractBaseUser.set_password(raw_password)
|
| 687 |
+
|
| 688 |
+
Sets the user's password to the given raw string, taking care of the
|
| 689 |
+
password hashing. Doesn't save the
|
| 690 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser` object.
|
| 691 |
+
|
| 692 |
+
When the raw_password is ``None``, the password will be set to an
|
| 693 |
+
unusable password, as if
|
| 694 |
+
:meth:`~django.contrib.auth.models.AbstractBaseUser.set_unusable_password()`
|
| 695 |
+
were used.
|
| 696 |
+
|
| 697 |
+
.. method:: models.AbstractBaseUser.check_password(raw_password)
|
| 698 |
+
.. method:: models.AbstractBaseUser.acheck_password(raw_password)
|
| 699 |
+
|
| 700 |
+
*Asynchronous version*: ``acheck_password()``
|
| 701 |
+
|
| 702 |
+
Returns ``True`` if the given raw string is the correct password for
|
| 703 |
+
the user. (This takes care of the password hashing in making the
|
| 704 |
+
comparison.)
|
| 705 |
+
|
| 706 |
+
.. versionchanged:: 5.0
|
| 707 |
+
|
| 708 |
+
``acheck_password()`` method was added.
|
| 709 |
+
|
| 710 |
+
.. method:: models.AbstractBaseUser.set_unusable_password()
|
| 711 |
+
|
| 712 |
+
Marks the user as having no password set. This isn't the same as
|
| 713 |
+
having a blank string for a password.
|
| 714 |
+
:meth:`~django.contrib.auth.models.AbstractBaseUser.check_password()` for this user
|
| 715 |
+
will never return ``True``. Doesn't save the
|
| 716 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser` object.
|
| 717 |
+
|
| 718 |
+
You may need this if authentication for your application takes place
|
| 719 |
+
against an existing external source such as an LDAP directory.
|
| 720 |
+
|
| 721 |
+
.. method:: models.AbstractBaseUser.has_usable_password()
|
| 722 |
+
|
| 723 |
+
Returns ``False`` if
|
| 724 |
+
:meth:`~django.contrib.auth.models.AbstractBaseUser.set_unusable_password()` has
|
| 725 |
+
been called for this user.
|
| 726 |
+
|
| 727 |
+
.. method:: models.AbstractBaseUser.get_session_auth_hash()
|
| 728 |
+
|
| 729 |
+
Returns an HMAC of the password field. Used for
|
| 730 |
+
:ref:`session-invalidation-on-password-change`.
|
| 731 |
+
|
| 732 |
+
.. method:: models.AbstractBaseUser.get_session_auth_fallback_hash()
|
| 733 |
+
|
| 734 |
+
.. versionadded:: 4.1.8
|
| 735 |
+
|
| 736 |
+
Yields the HMAC of the password field using
|
| 737 |
+
:setting:`SECRET_KEY_FALLBACKS`. Used by ``get_user()``.
|
| 738 |
+
|
| 739 |
+
:class:`~models.AbstractUser` subclasses :class:`~models.AbstractBaseUser`:
|
| 740 |
+
|
| 741 |
+
.. class:: models.AbstractUser
|
| 742 |
+
|
| 743 |
+
.. method:: clean()
|
| 744 |
+
|
| 745 |
+
Normalizes the email by calling
|
| 746 |
+
:meth:`.BaseUserManager.normalize_email`. If you override this method,
|
| 747 |
+
be sure to call ``super()`` to retain the normalization.
|
| 748 |
+
|
| 749 |
+
Writing a manager for a custom user model
|
| 750 |
+
-----------------------------------------
|
| 751 |
+
|
| 752 |
+
You should also define a custom manager for your user model. If your user model
|
| 753 |
+
defines ``username``, ``email``, ``is_staff``, ``is_active``, ``is_superuser``,
|
| 754 |
+
``last_login``, and ``date_joined`` fields the same as Django's default user,
|
| 755 |
+
you can install Django's :class:`~django.contrib.auth.models.UserManager`;
|
| 756 |
+
however, if your user model defines different fields, you'll need to define a
|
| 757 |
+
custom manager that extends :class:`~django.contrib.auth.models.BaseUserManager`
|
| 758 |
+
providing two additional methods:
|
| 759 |
+
|
| 760 |
+
.. class:: models.CustomUserManager
|
| 761 |
+
|
| 762 |
+
.. method:: models.CustomUserManager.create_user(username_field, password=None, **other_fields)
|
| 763 |
+
|
| 764 |
+
The prototype of ``create_user()`` should accept the username field,
|
| 765 |
+
plus all required fields as arguments. For example, if your user model
|
| 766 |
+
uses ``email`` as the username field, and has ``date_of_birth`` as a
|
| 767 |
+
required field, then ``create_user`` should be defined as::
|
| 768 |
+
|
| 769 |
+
def create_user(self, email, date_of_birth, password=None):
|
| 770 |
+
# create user here
|
| 771 |
+
...
|
| 772 |
+
|
| 773 |
+
.. method:: models.CustomUserManager.create_superuser(username_field, password=None, **other_fields)
|
| 774 |
+
|
| 775 |
+
The prototype of ``create_superuser()`` should accept the username
|
| 776 |
+
field, plus all required fields as arguments. For example, if your user
|
| 777 |
+
model uses ``email`` as the username field, and has ``date_of_birth``
|
| 778 |
+
as a required field, then ``create_superuser`` should be defined as::
|
| 779 |
+
|
| 780 |
+
def create_superuser(self, email, date_of_birth, password=None):
|
| 781 |
+
# create superuser here
|
| 782 |
+
...
|
| 783 |
+
|
| 784 |
+
For a :class:`~.ForeignKey` in :attr:`.USERNAME_FIELD` or
|
| 785 |
+
:attr:`.REQUIRED_FIELDS`, these methods receive the value of the
|
| 786 |
+
:attr:`~.ForeignKey.to_field` (the :attr:`~django.db.models.Field.primary_key`
|
| 787 |
+
by default) of an existing instance.
|
| 788 |
+
|
| 789 |
+
:class:`~django.contrib.auth.models.BaseUserManager` provides the following
|
| 790 |
+
utility methods:
|
| 791 |
+
|
| 792 |
+
.. class:: models.BaseUserManager
|
| 793 |
+
|
| 794 |
+
.. classmethod:: models.BaseUserManager.normalize_email(email)
|
| 795 |
+
|
| 796 |
+
Normalizes email addresses by lowercasing the domain portion of the
|
| 797 |
+
email address.
|
| 798 |
+
|
| 799 |
+
.. method:: models.BaseUserManager.get_by_natural_key(username)
|
| 800 |
+
|
| 801 |
+
Retrieves a user instance using the contents of the field
|
| 802 |
+
nominated by ``USERNAME_FIELD``.
|
| 803 |
+
|
| 804 |
+
.. method:: models.BaseUserManager.make_random_password(length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789')
|
| 805 |
+
|
| 806 |
+
.. deprecated:: 4.2
|
| 807 |
+
|
| 808 |
+
Returns a random password with the given length and given string of
|
| 809 |
+
allowed characters. Note that the default value of ``allowed_chars``
|
| 810 |
+
doesn't contain letters that can cause user confusion, including:
|
| 811 |
+
|
| 812 |
+
* ``i``, ``l``, ``I``, and ``1`` (lowercase letter i, lowercase
|
| 813 |
+
letter L, uppercase letter i, and the number one)
|
| 814 |
+
* ``o``, ``O``, and ``0`` (lowercase letter o, uppercase letter o,
|
| 815 |
+
and zero)
|
| 816 |
+
|
| 817 |
+
Extending Django's default ``User``
|
| 818 |
+
-----------------------------------
|
| 819 |
+
|
| 820 |
+
If you're entirely happy with Django's :class:`~django.contrib.auth.models.User`
|
| 821 |
+
model, but you want to add some additional profile information, you could
|
| 822 |
+
subclass :class:`django.contrib.auth.models.AbstractUser` and add your custom
|
| 823 |
+
profile fields, although we'd recommend a separate model as described in
|
| 824 |
+
:ref:`specifying-custom-user-model`. ``AbstractUser`` provides the full
|
| 825 |
+
implementation of the default :class:`~django.contrib.auth.models.User` as an
|
| 826 |
+
:ref:`abstract model <abstract-base-classes>`.
|
| 827 |
+
|
| 828 |
+
.. _custom-users-and-the-built-in-auth-forms:
|
| 829 |
+
|
| 830 |
+
Custom users and the built-in auth forms
|
| 831 |
+
----------------------------------------
|
| 832 |
+
|
| 833 |
+
Django's built-in :ref:`forms <built-in-auth-forms>` and :ref:`views
|
| 834 |
+
<built-in-auth-views>` make certain assumptions about the user model that they
|
| 835 |
+
are working with.
|
| 836 |
+
|
| 837 |
+
The following forms are compatible with any subclass of
|
| 838 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser`:
|
| 839 |
+
|
| 840 |
+
* :class:`~django.contrib.auth.forms.AuthenticationForm`: Uses the username
|
| 841 |
+
field specified by :attr:`~models.CustomUser.USERNAME_FIELD`.
|
| 842 |
+
* :class:`~django.contrib.auth.forms.SetPasswordForm`
|
| 843 |
+
* :class:`~django.contrib.auth.forms.PasswordChangeForm`
|
| 844 |
+
* :class:`~django.contrib.auth.forms.AdminPasswordChangeForm`
|
| 845 |
+
|
| 846 |
+
The following forms make assumptions about the user model and can be used as-is
|
| 847 |
+
if those assumptions are met:
|
| 848 |
+
|
| 849 |
+
* :class:`~django.contrib.auth.forms.PasswordResetForm`: Assumes that the user
|
| 850 |
+
model has a field that stores the user's email address with the name returned
|
| 851 |
+
by :meth:`~models.AbstractBaseUser.get_email_field_name` (``email`` by
|
| 852 |
+
default) that can be used to identify the user and a boolean field named
|
| 853 |
+
``is_active`` to prevent password resets for inactive users.
|
| 854 |
+
|
| 855 |
+
Finally, the following forms are tied to
|
| 856 |
+
:class:`~django.contrib.auth.models.User` and need to be rewritten or extended
|
| 857 |
+
to work with a custom user model:
|
| 858 |
+
|
| 859 |
+
* :class:`~django.contrib.auth.forms.UserCreationForm`
|
| 860 |
+
* :class:`~django.contrib.auth.forms.UserChangeForm`
|
| 861 |
+
|
| 862 |
+
If your custom user model is a subclass of ``AbstractUser``, then you can
|
| 863 |
+
extend these forms in this manner::
|
| 864 |
+
|
| 865 |
+
from django.contrib.auth.forms import UserCreationForm
|
| 866 |
+
from myapp.models import CustomUser
|
| 867 |
+
|
| 868 |
+
|
| 869 |
+
class CustomUserCreationForm(UserCreationForm):
|
| 870 |
+
class Meta(UserCreationForm.Meta):
|
| 871 |
+
model = CustomUser
|
| 872 |
+
fields = UserCreationForm.Meta.fields + ("custom_field",)
|
| 873 |
+
|
| 874 |
+
.. versionchanged:: 4.2
|
| 875 |
+
|
| 876 |
+
In older versions, :class:`~django.contrib.auth.forms.UserCreationForm`
|
| 877 |
+
didn't save many-to-many form fields for a custom user model.
|
| 878 |
+
|
| 879 |
+
Custom users and :mod:`django.contrib.admin`
|
| 880 |
+
--------------------------------------------
|
| 881 |
+
|
| 882 |
+
If you want your custom user model to also work with the admin, your user model
|
| 883 |
+
must define some additional attributes and methods. These methods allow the
|
| 884 |
+
admin to control access of the user to admin content:
|
| 885 |
+
|
| 886 |
+
.. class:: models.CustomUser
|
| 887 |
+
:noindex:
|
| 888 |
+
|
| 889 |
+
.. attribute:: is_staff
|
| 890 |
+
|
| 891 |
+
Returns ``True`` if the user is allowed to have access to the admin site.
|
| 892 |
+
|
| 893 |
+
.. attribute:: is_active
|
| 894 |
+
|
| 895 |
+
Returns ``True`` if the user account is currently active.
|
| 896 |
+
|
| 897 |
+
.. method:: has_perm(perm, obj=None):
|
| 898 |
+
|
| 899 |
+
Returns ``True`` if the user has the named permission. If ``obj`` is
|
| 900 |
+
provided, the permission needs to be checked against a specific object
|
| 901 |
+
instance.
|
| 902 |
+
|
| 903 |
+
.. method:: has_module_perms(app_label):
|
| 904 |
+
|
| 905 |
+
Returns ``True`` if the user has permission to access models in
|
| 906 |
+
the given app.
|
| 907 |
+
|
| 908 |
+
You will also need to register your custom user model with the admin. If
|
| 909 |
+
your custom user model extends ``django.contrib.auth.models.AbstractUser``,
|
| 910 |
+
you can use Django's existing ``django.contrib.auth.admin.UserAdmin``
|
| 911 |
+
class. However, if your user model extends
|
| 912 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser`, you'll need to define
|
| 913 |
+
a custom ``ModelAdmin`` class. It may be possible to subclass the default
|
| 914 |
+
``django.contrib.auth.admin.UserAdmin``; however, you'll need to
|
| 915 |
+
override any of the definitions that refer to fields on
|
| 916 |
+
``django.contrib.auth.models.AbstractUser`` that aren't on your
|
| 917 |
+
custom user class.
|
| 918 |
+
|
| 919 |
+
.. note::
|
| 920 |
+
|
| 921 |
+
If you are using a custom ``ModelAdmin`` which is a subclass of
|
| 922 |
+
``django.contrib.auth.admin.UserAdmin``, then you need to add your custom
|
| 923 |
+
fields to ``fieldsets`` (for fields to be used in editing users) and to
|
| 924 |
+
``add_fieldsets`` (for fields to be used when creating a user). For
|
| 925 |
+
example::
|
| 926 |
+
|
| 927 |
+
from django.contrib.auth.admin import UserAdmin
|
| 928 |
+
|
| 929 |
+
|
| 930 |
+
class CustomUserAdmin(UserAdmin):
|
| 931 |
+
...
|
| 932 |
+
fieldsets = UserAdmin.fieldsets + ((None, {"fields": ["custom_field"]}),)
|
| 933 |
+
add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ["custom_field"]}),)
|
| 934 |
+
|
| 935 |
+
See :ref:`a full example <custom-users-admin-full-example>` for more
|
| 936 |
+
details.
|
| 937 |
+
|
| 938 |
+
Custom users and permissions
|
| 939 |
+
----------------------------
|
| 940 |
+
|
| 941 |
+
To make it easy to include Django's permission framework into your own user
|
| 942 |
+
class, Django provides :class:`~django.contrib.auth.models.PermissionsMixin`.
|
| 943 |
+
This is an abstract model you can include in the class hierarchy for your user
|
| 944 |
+
model, giving you all the methods and database fields necessary to support
|
| 945 |
+
Django's permission model.
|
| 946 |
+
|
| 947 |
+
:class:`~django.contrib.auth.models.PermissionsMixin` provides the following
|
| 948 |
+
methods and attributes:
|
| 949 |
+
|
| 950 |
+
.. class:: models.PermissionsMixin
|
| 951 |
+
|
| 952 |
+
.. attribute:: models.PermissionsMixin.is_superuser
|
| 953 |
+
|
| 954 |
+
Boolean. Designates that this user has all permissions without
|
| 955 |
+
explicitly assigning them.
|
| 956 |
+
|
| 957 |
+
.. method:: models.PermissionsMixin.get_user_permissions(obj=None)
|
| 958 |
+
|
| 959 |
+
Returns a set of permission strings that the user has directly.
|
| 960 |
+
|
| 961 |
+
If ``obj`` is passed in, only returns the user permissions for this
|
| 962 |
+
specific object.
|
| 963 |
+
|
| 964 |
+
.. method:: models.PermissionsMixin.get_group_permissions(obj=None)
|
| 965 |
+
|
| 966 |
+
Returns a set of permission strings that the user has, through their
|
| 967 |
+
groups.
|
| 968 |
+
|
| 969 |
+
If ``obj`` is passed in, only returns the group permissions for
|
| 970 |
+
this specific object.
|
| 971 |
+
|
| 972 |
+
.. method:: models.PermissionsMixin.get_all_permissions(obj=None)
|
| 973 |
+
|
| 974 |
+
Returns a set of permission strings that the user has, both through
|
| 975 |
+
group and user permissions.
|
| 976 |
+
|
| 977 |
+
If ``obj`` is passed in, only returns the permissions for this
|
| 978 |
+
specific object.
|
| 979 |
+
|
| 980 |
+
.. method:: models.PermissionsMixin.has_perm(perm, obj=None)
|
| 981 |
+
|
| 982 |
+
Returns ``True`` if the user has the specified permission, where
|
| 983 |
+
``perm`` is in the format ``"<app label>.<permission codename>"`` (see
|
| 984 |
+
:ref:`permissions <topic-authorization>`). If :attr:`.User.is_active`
|
| 985 |
+
and :attr:`~.User.is_superuser` are both ``True``, this method always
|
| 986 |
+
returns ``True``.
|
| 987 |
+
|
| 988 |
+
If ``obj`` is passed in, this method won't check for a permission for
|
| 989 |
+
the model, but for this specific object.
|
| 990 |
+
|
| 991 |
+
.. method:: models.PermissionsMixin.has_perms(perm_list, obj=None)
|
| 992 |
+
|
| 993 |
+
Returns ``True`` if the user has each of the specified permissions,
|
| 994 |
+
where each perm is in the format
|
| 995 |
+
``"<app label>.<permission codename>"``. If :attr:`.User.is_active` and
|
| 996 |
+
:attr:`~.User.is_superuser` are both ``True``, this method always
|
| 997 |
+
returns ``True``.
|
| 998 |
+
|
| 999 |
+
If ``obj`` is passed in, this method won't check for permissions for
|
| 1000 |
+
the model, but for the specific object.
|
| 1001 |
+
|
| 1002 |
+
.. method:: models.PermissionsMixin.has_module_perms(package_name)
|
| 1003 |
+
|
| 1004 |
+
Returns ``True`` if the user has any permissions in the given package
|
| 1005 |
+
(the Django app label). If :attr:`.User.is_active` and
|
| 1006 |
+
:attr:`~.User.is_superuser` are both ``True``, this method always
|
| 1007 |
+
returns ``True``.
|
| 1008 |
+
|
| 1009 |
+
.. admonition:: ``PermissionsMixin`` and ``ModelBackend``
|
| 1010 |
+
|
| 1011 |
+
If you don't include the
|
| 1012 |
+
:class:`~django.contrib.auth.models.PermissionsMixin`, you must ensure you
|
| 1013 |
+
don't invoke the permissions methods on ``ModelBackend``. ``ModelBackend``
|
| 1014 |
+
assumes that certain fields are available on your user model. If your user
|
| 1015 |
+
model doesn't provide those fields, you'll receive database errors when
|
| 1016 |
+
you check permissions.
|
| 1017 |
+
|
| 1018 |
+
Custom users and proxy models
|
| 1019 |
+
-----------------------------
|
| 1020 |
+
|
| 1021 |
+
One limitation of custom user models is that installing a custom user model
|
| 1022 |
+
will break any proxy model extending :class:`~django.contrib.auth.models.User`.
|
| 1023 |
+
Proxy models must be based on a concrete base class; by defining a custom user
|
| 1024 |
+
model, you remove the ability of Django to reliably identify the base class.
|
| 1025 |
+
|
| 1026 |
+
If your project uses proxy models, you must either modify the proxy to extend
|
| 1027 |
+
the user model that's in use in your project, or merge your proxy's behavior
|
| 1028 |
+
into your :class:`~django.contrib.auth.models.User` subclass.
|
| 1029 |
+
|
| 1030 |
+
.. _custom-users-admin-full-example:
|
| 1031 |
+
|
| 1032 |
+
A full example
|
| 1033 |
+
--------------
|
| 1034 |
+
|
| 1035 |
+
Here is an example of an admin-compliant custom user app. This user model uses
|
| 1036 |
+
an email address as the username, and has a required date of birth; it
|
| 1037 |
+
provides no permission checking beyond an ``admin`` flag on the user account.
|
| 1038 |
+
This model would be compatible with all the built-in auth forms and views,
|
| 1039 |
+
except for the user creation forms. This example illustrates how most of the
|
| 1040 |
+
components work together, but is not intended to be copied directly into
|
| 1041 |
+
projects for production use.
|
| 1042 |
+
|
| 1043 |
+
This code would all live in a ``models.py`` file for a custom
|
| 1044 |
+
authentication app::
|
| 1045 |
+
|
| 1046 |
+
from django.db import models
|
| 1047 |
+
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
|
| 1048 |
+
|
| 1049 |
+
|
| 1050 |
+
class MyUserManager(BaseUserManager):
|
| 1051 |
+
def create_user(self, email, date_of_birth, password=None):
|
| 1052 |
+
"""
|
| 1053 |
+
Creates and saves a User with the given email, date of
|
| 1054 |
+
birth and password.
|
| 1055 |
+
"""
|
| 1056 |
+
if not email:
|
| 1057 |
+
raise ValueError("Users must have an email address")
|
| 1058 |
+
|
| 1059 |
+
user = self.model(
|
| 1060 |
+
email=self.normalize_email(email),
|
| 1061 |
+
date_of_birth=date_of_birth,
|
| 1062 |
+
)
|
| 1063 |
+
|
| 1064 |
+
user.set_password(password)
|
| 1065 |
+
user.save(using=self._db)
|
| 1066 |
+
return user
|
| 1067 |
+
|
| 1068 |
+
def create_superuser(self, email, date_of_birth, password=None):
|
| 1069 |
+
"""
|
| 1070 |
+
Creates and saves a superuser with the given email, date of
|
| 1071 |
+
birth and password.
|
| 1072 |
+
"""
|
| 1073 |
+
user = self.create_user(
|
| 1074 |
+
email,
|
| 1075 |
+
password=password,
|
| 1076 |
+
date_of_birth=date_of_birth,
|
| 1077 |
+
)
|
| 1078 |
+
user.is_admin = True
|
| 1079 |
+
user.save(using=self._db)
|
| 1080 |
+
return user
|
| 1081 |
+
|
| 1082 |
+
|
| 1083 |
+
class MyUser(AbstractBaseUser):
|
| 1084 |
+
email = models.EmailField(
|
| 1085 |
+
verbose_name="email address",
|
| 1086 |
+
max_length=255,
|
| 1087 |
+
unique=True,
|
| 1088 |
+
)
|
| 1089 |
+
date_of_birth = models.DateField()
|
| 1090 |
+
is_active = models.BooleanField(default=True)
|
| 1091 |
+
is_admin = models.BooleanField(default=False)
|
| 1092 |
+
|
| 1093 |
+
objects = MyUserManager()
|
| 1094 |
+
|
| 1095 |
+
USERNAME_FIELD = "email"
|
| 1096 |
+
REQUIRED_FIELDS = ["date_of_birth"]
|
| 1097 |
+
|
| 1098 |
+
def __str__(self):
|
| 1099 |
+
return self.email
|
| 1100 |
+
|
| 1101 |
+
def has_perm(self, perm, obj=None):
|
| 1102 |
+
"Does the user have a specific permission?"
|
| 1103 |
+
# Simplest possible answer: Yes, always
|
| 1104 |
+
return True
|
| 1105 |
+
|
| 1106 |
+
def has_module_perms(self, app_label):
|
| 1107 |
+
"Does the user have permissions to view the app `app_label`?"
|
| 1108 |
+
# Simplest possible answer: Yes, always
|
| 1109 |
+
return True
|
| 1110 |
+
|
| 1111 |
+
@property
|
| 1112 |
+
def is_staff(self):
|
| 1113 |
+
"Is the user a member of staff?"
|
| 1114 |
+
# Simplest possible answer: All admins are staff
|
| 1115 |
+
return self.is_admin
|
| 1116 |
+
|
| 1117 |
+
Then, to register this custom user model with Django's admin, the following
|
| 1118 |
+
code would be required in the app's ``admin.py`` file::
|
| 1119 |
+
|
| 1120 |
+
from django import forms
|
| 1121 |
+
from django.contrib import admin
|
| 1122 |
+
from django.contrib.auth.models import Group
|
| 1123 |
+
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
| 1124 |
+
from django.contrib.auth.forms import ReadOnlyPasswordHashField
|
| 1125 |
+
from django.core.exceptions import ValidationError
|
| 1126 |
+
|
| 1127 |
+
from customauth.models import MyUser
|
| 1128 |
+
|
| 1129 |
+
|
| 1130 |
+
class UserCreationForm(forms.ModelForm):
|
| 1131 |
+
"""A form for creating new users. Includes all the required
|
| 1132 |
+
fields, plus a repeated password."""
|
| 1133 |
+
|
| 1134 |
+
password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
|
| 1135 |
+
password2 = forms.CharField(
|
| 1136 |
+
label="Password confirmation", widget=forms.PasswordInput
|
| 1137 |
+
)
|
| 1138 |
+
|
| 1139 |
+
class Meta:
|
| 1140 |
+
model = MyUser
|
| 1141 |
+
fields = ["email", "date_of_birth"]
|
| 1142 |
+
|
| 1143 |
+
def clean_password2(self):
|
| 1144 |
+
# Check that the two password entries match
|
| 1145 |
+
password1 = self.cleaned_data.get("password1")
|
| 1146 |
+
password2 = self.cleaned_data.get("password2")
|
| 1147 |
+
if password1 and password2 and password1 != password2:
|
| 1148 |
+
raise ValidationError("Passwords don't match")
|
| 1149 |
+
return password2
|
| 1150 |
+
|
| 1151 |
+
def save(self, commit=True):
|
| 1152 |
+
# Save the provided password in hashed format
|
| 1153 |
+
user = super().save(commit=False)
|
| 1154 |
+
user.set_password(self.cleaned_data["password1"])
|
| 1155 |
+
if commit:
|
| 1156 |
+
user.save()
|
| 1157 |
+
return user
|
| 1158 |
+
|
| 1159 |
+
|
| 1160 |
+
class UserChangeForm(forms.ModelForm):
|
| 1161 |
+
"""A form for updating users. Includes all the fields on
|
| 1162 |
+
the user, but replaces the password field with admin's
|
| 1163 |
+
disabled password hash display field.
|
| 1164 |
+
"""
|
| 1165 |
+
|
| 1166 |
+
password = ReadOnlyPasswordHashField()
|
| 1167 |
+
|
| 1168 |
+
class Meta:
|
| 1169 |
+
model = MyUser
|
| 1170 |
+
fields = ["email", "password", "date_of_birth", "is_active", "is_admin"]
|
| 1171 |
+
|
| 1172 |
+
|
| 1173 |
+
class UserAdmin(BaseUserAdmin):
|
| 1174 |
+
# The forms to add and change user instances
|
| 1175 |
+
form = UserChangeForm
|
| 1176 |
+
add_form = UserCreationForm
|
| 1177 |
+
|
| 1178 |
+
# The fields to be used in displaying the User model.
|
| 1179 |
+
# These override the definitions on the base UserAdmin
|
| 1180 |
+
# that reference specific fields on auth.User.
|
| 1181 |
+
list_display = ["email", "date_of_birth", "is_admin"]
|
| 1182 |
+
list_filter = ["is_admin"]
|
| 1183 |
+
fieldsets = [
|
| 1184 |
+
(None, {"fields": ["email", "password"]}),
|
| 1185 |
+
("Personal info", {"fields": ["date_of_birth"]}),
|
| 1186 |
+
("Permissions", {"fields": ["is_admin"]}),
|
| 1187 |
+
]
|
| 1188 |
+
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
|
| 1189 |
+
# overrides get_fieldsets to use this attribute when creating a user.
|
| 1190 |
+
add_fieldsets = [
|
| 1191 |
+
(
|
| 1192 |
+
None,
|
| 1193 |
+
{
|
| 1194 |
+
"classes": ["wide"],
|
| 1195 |
+
"fields": ["email", "date_of_birth", "password1", "password2"],
|
| 1196 |
+
},
|
| 1197 |
+
),
|
| 1198 |
+
]
|
| 1199 |
+
search_fields = ["email"]
|
| 1200 |
+
ordering = ["email"]
|
| 1201 |
+
filter_horizontal = []
|
| 1202 |
+
|
| 1203 |
+
|
| 1204 |
+
# Now register the new UserAdmin...
|
| 1205 |
+
admin.site.register(MyUser, UserAdmin)
|
| 1206 |
+
# ... and, since we're not using Django's built-in permissions,
|
| 1207 |
+
# unregister the Group model from admin.
|
| 1208 |
+
admin.site.unregister(Group)
|
| 1209 |
+
|
| 1210 |
+
Finally, specify the custom model as the default user model for your project
|
| 1211 |
+
using the :setting:`AUTH_USER_MODEL` setting in your ``settings.py``::
|
| 1212 |
+
|
| 1213 |
+
AUTH_USER_MODEL = "customauth.MyUser"
|
testbed/django__django/docs/topics/auth/default.txt
ADDED
|
@@ -0,0 +1,1884 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
======================================
|
| 2 |
+
Using the Django authentication system
|
| 3 |
+
======================================
|
| 4 |
+
|
| 5 |
+
.. currentmodule:: django.contrib.auth
|
| 6 |
+
|
| 7 |
+
This document explains the usage of Django's authentication system in its
|
| 8 |
+
default configuration. This configuration has evolved to serve the most common
|
| 9 |
+
project needs, handling a reasonably wide range of tasks, and has a careful
|
| 10 |
+
implementation of passwords and permissions. For projects where authentication
|
| 11 |
+
needs differ from the default, Django supports extensive :doc:`extension and
|
| 12 |
+
customization </topics/auth/customizing>` of authentication.
|
| 13 |
+
|
| 14 |
+
Django authentication provides both authentication and authorization together
|
| 15 |
+
and is generally referred to as the authentication system, as these features
|
| 16 |
+
are somewhat coupled.
|
| 17 |
+
|
| 18 |
+
.. _user-objects:
|
| 19 |
+
|
| 20 |
+
``User`` objects
|
| 21 |
+
================
|
| 22 |
+
|
| 23 |
+
:class:`~django.contrib.auth.models.User` objects are the core of the
|
| 24 |
+
authentication system. They typically represent the people interacting with
|
| 25 |
+
your site and are used to enable things like restricting access, registering
|
| 26 |
+
user profiles, associating content with creators etc. Only one class of user
|
| 27 |
+
exists in Django's authentication framework, i.e., :attr:`'superusers'
|
| 28 |
+
<django.contrib.auth.models.User.is_superuser>` or admin :attr:`'staff'
|
| 29 |
+
<django.contrib.auth.models.User.is_staff>` users are just user objects with
|
| 30 |
+
special attributes set, not different classes of user objects.
|
| 31 |
+
|
| 32 |
+
The primary attributes of the default user are:
|
| 33 |
+
|
| 34 |
+
* :attr:`~django.contrib.auth.models.User.username`
|
| 35 |
+
* :attr:`~django.contrib.auth.models.User.password`
|
| 36 |
+
* :attr:`~django.contrib.auth.models.User.email`
|
| 37 |
+
* :attr:`~django.contrib.auth.models.User.first_name`
|
| 38 |
+
* :attr:`~django.contrib.auth.models.User.last_name`
|
| 39 |
+
|
| 40 |
+
See the :class:`full API documentation <django.contrib.auth.models.User>` for
|
| 41 |
+
full reference, the documentation that follows is more task oriented.
|
| 42 |
+
|
| 43 |
+
.. _topics-auth-creating-users:
|
| 44 |
+
|
| 45 |
+
Creating users
|
| 46 |
+
--------------
|
| 47 |
+
|
| 48 |
+
The most direct way to create users is to use the included
|
| 49 |
+
:meth:`~django.contrib.auth.models.UserManager.create_user` helper function:
|
| 50 |
+
|
| 51 |
+
.. code-block:: pycon
|
| 52 |
+
|
| 53 |
+
>>> from django.contrib.auth.models import User
|
| 54 |
+
>>> user = User.objects.create_user("john", "lennon@thebeatles.com", "johnpassword")
|
| 55 |
+
|
| 56 |
+
# At this point, user is a User object that has already been saved
|
| 57 |
+
# to the database. You can continue to change its attributes
|
| 58 |
+
# if you want to change other fields.
|
| 59 |
+
>>> user.last_name = "Lennon"
|
| 60 |
+
>>> user.save()
|
| 61 |
+
|
| 62 |
+
If you have the Django admin installed, you can also :ref:`create users
|
| 63 |
+
interactively <auth-admin>`.
|
| 64 |
+
|
| 65 |
+
.. _topics-auth-creating-superusers:
|
| 66 |
+
|
| 67 |
+
Creating superusers
|
| 68 |
+
-------------------
|
| 69 |
+
|
| 70 |
+
Create superusers using the :djadmin:`createsuperuser` command:
|
| 71 |
+
|
| 72 |
+
.. console::
|
| 73 |
+
|
| 74 |
+
$ python manage.py createsuperuser --username=joe --email=joe@example.com
|
| 75 |
+
|
| 76 |
+
You will be prompted for a password. After you enter one, the user will be
|
| 77 |
+
created immediately. If you leave off the :option:`--username <createsuperuser
|
| 78 |
+
--username>` or :option:`--email <createsuperuser --email>` options, it will
|
| 79 |
+
prompt you for those values.
|
| 80 |
+
|
| 81 |
+
Changing passwords
|
| 82 |
+
------------------
|
| 83 |
+
|
| 84 |
+
Django does not store raw (clear text) passwords on the user model, but only
|
| 85 |
+
a hash (see :doc:`documentation of how passwords are managed
|
| 86 |
+
</topics/auth/passwords>` for full details). Because of this, do not attempt to
|
| 87 |
+
manipulate the password attribute of the user directly. This is why a helper
|
| 88 |
+
function is used when creating a user.
|
| 89 |
+
|
| 90 |
+
To change a user's password, you have several options:
|
| 91 |
+
|
| 92 |
+
:djadmin:`manage.py changepassword *username* <changepassword>` offers a method
|
| 93 |
+
of changing a user's password from the command line. It prompts you to
|
| 94 |
+
change the password of a given user which you must enter twice. If
|
| 95 |
+
they both match, the new password will be changed immediately. If you
|
| 96 |
+
do not supply a user, the command will attempt to change the password
|
| 97 |
+
whose username matches the current system user.
|
| 98 |
+
|
| 99 |
+
You can also change a password programmatically, using
|
| 100 |
+
:meth:`~django.contrib.auth.models.User.set_password()`:
|
| 101 |
+
|
| 102 |
+
.. code-block:: pycon
|
| 103 |
+
|
| 104 |
+
>>> from django.contrib.auth.models import User
|
| 105 |
+
>>> u = User.objects.get(username="john")
|
| 106 |
+
>>> u.set_password("new password")
|
| 107 |
+
>>> u.save()
|
| 108 |
+
|
| 109 |
+
If you have the Django admin installed, you can also change user's passwords
|
| 110 |
+
on the :ref:`authentication system's admin pages <auth-admin>`.
|
| 111 |
+
|
| 112 |
+
Django also provides :ref:`views <built-in-auth-views>` and :ref:`forms
|
| 113 |
+
<built-in-auth-forms>` that may be used to allow users to change their own
|
| 114 |
+
passwords.
|
| 115 |
+
|
| 116 |
+
Changing a user's password will log out all their sessions. See
|
| 117 |
+
:ref:`session-invalidation-on-password-change` for details.
|
| 118 |
+
|
| 119 |
+
Authenticating users
|
| 120 |
+
--------------------
|
| 121 |
+
|
| 122 |
+
.. function:: authenticate(request=None, **credentials)
|
| 123 |
+
.. function:: aauthenticate(request=None, **credentials)
|
| 124 |
+
|
| 125 |
+
*Asynchronous version*: ``aauthenticate()``
|
| 126 |
+
|
| 127 |
+
Use :func:`~django.contrib.auth.authenticate()` to verify a set of
|
| 128 |
+
credentials. It takes credentials as keyword arguments, ``username`` and
|
| 129 |
+
``password`` for the default case, checks them against each
|
| 130 |
+
:ref:`authentication backend <authentication-backends>`, and returns a
|
| 131 |
+
:class:`~django.contrib.auth.models.User` object if the credentials are
|
| 132 |
+
valid for a backend. If the credentials aren't valid for any backend or if
|
| 133 |
+
a backend raises :class:`~django.core.exceptions.PermissionDenied`, it
|
| 134 |
+
returns ``None``. For example::
|
| 135 |
+
|
| 136 |
+
from django.contrib.auth import authenticate
|
| 137 |
+
|
| 138 |
+
user = authenticate(username="john", password="secret")
|
| 139 |
+
if user is not None:
|
| 140 |
+
# A backend authenticated the credentials
|
| 141 |
+
...
|
| 142 |
+
else:
|
| 143 |
+
# No backend authenticated the credentials
|
| 144 |
+
...
|
| 145 |
+
|
| 146 |
+
``request`` is an optional :class:`~django.http.HttpRequest` which is
|
| 147 |
+
passed on the ``authenticate()`` method of the authentication backends.
|
| 148 |
+
|
| 149 |
+
.. note::
|
| 150 |
+
|
| 151 |
+
This is a low level way to authenticate a set of credentials; for
|
| 152 |
+
example, it's used by the
|
| 153 |
+
:class:`~django.contrib.auth.middleware.RemoteUserMiddleware`. Unless
|
| 154 |
+
you are writing your own authentication system, you probably won't use
|
| 155 |
+
this. Rather if you're looking for a way to login a user, use the
|
| 156 |
+
:class:`~django.contrib.auth.views.LoginView`.
|
| 157 |
+
|
| 158 |
+
.. versionchanged:: 5.0
|
| 159 |
+
|
| 160 |
+
``aauthenticate()`` function was added.
|
| 161 |
+
|
| 162 |
+
.. _topic-authorization:
|
| 163 |
+
|
| 164 |
+
Permissions and Authorization
|
| 165 |
+
=============================
|
| 166 |
+
|
| 167 |
+
Django comes with a built-in permissions system. It provides a way to assign
|
| 168 |
+
permissions to specific users and groups of users.
|
| 169 |
+
|
| 170 |
+
It's used by the Django admin site, but you're welcome to use it in your own
|
| 171 |
+
code.
|
| 172 |
+
|
| 173 |
+
The Django admin site uses permissions as follows:
|
| 174 |
+
|
| 175 |
+
* Access to view objects is limited to users with the "view" or "change"
|
| 176 |
+
permission for that type of object.
|
| 177 |
+
* Access to view the "add" form and add an object is limited to users with
|
| 178 |
+
the "add" permission for that type of object.
|
| 179 |
+
* Access to view the change list, view the "change" form and change an
|
| 180 |
+
object is limited to users with the "change" permission for that type of
|
| 181 |
+
object.
|
| 182 |
+
* Access to delete an object is limited to users with the "delete"
|
| 183 |
+
permission for that type of object.
|
| 184 |
+
|
| 185 |
+
Permissions can be set not only per type of object, but also per specific
|
| 186 |
+
object instance. By using the
|
| 187 |
+
:meth:`~django.contrib.admin.ModelAdmin.has_view_permission`,
|
| 188 |
+
:meth:`~django.contrib.admin.ModelAdmin.has_add_permission`,
|
| 189 |
+
:meth:`~django.contrib.admin.ModelAdmin.has_change_permission` and
|
| 190 |
+
:meth:`~django.contrib.admin.ModelAdmin.has_delete_permission` methods provided
|
| 191 |
+
by the :class:`~django.contrib.admin.ModelAdmin` class, it is possible to
|
| 192 |
+
customize permissions for different object instances of the same type.
|
| 193 |
+
|
| 194 |
+
:class:`~django.contrib.auth.models.User` objects have two many-to-many
|
| 195 |
+
fields: ``groups`` and ``user_permissions``.
|
| 196 |
+
:class:`~django.contrib.auth.models.User` objects can access their related
|
| 197 |
+
objects in the same way as any other :doc:`Django model
|
| 198 |
+
</topics/db/models>`::
|
| 199 |
+
|
| 200 |
+
myuser.groups.set([group_list])
|
| 201 |
+
myuser.groups.add(group, group, ...)
|
| 202 |
+
myuser.groups.remove(group, group, ...)
|
| 203 |
+
myuser.groups.clear()
|
| 204 |
+
myuser.user_permissions.set([permission_list])
|
| 205 |
+
myuser.user_permissions.add(permission, permission, ...)
|
| 206 |
+
myuser.user_permissions.remove(permission, permission, ...)
|
| 207 |
+
myuser.user_permissions.clear()
|
| 208 |
+
|
| 209 |
+
Default permissions
|
| 210 |
+
-------------------
|
| 211 |
+
|
| 212 |
+
When ``django.contrib.auth`` is listed in your :setting:`INSTALLED_APPS`
|
| 213 |
+
setting, it will ensure that four default permissions -- add, change, delete,
|
| 214 |
+
and view -- are created for each Django model defined in one of your installed
|
| 215 |
+
applications.
|
| 216 |
+
|
| 217 |
+
These permissions will be created when you run :djadmin:`manage.py migrate
|
| 218 |
+
<migrate>`; the first time you run ``migrate`` after adding
|
| 219 |
+
``django.contrib.auth`` to :setting:`INSTALLED_APPS`, the default permissions
|
| 220 |
+
will be created for all previously-installed models, as well as for any new
|
| 221 |
+
models being installed at that time. Afterward, it will create default
|
| 222 |
+
permissions for new models each time you run :djadmin:`manage.py migrate
|
| 223 |
+
<migrate>` (the function that creates permissions is connected to the
|
| 224 |
+
:data:`~django.db.models.signals.post_migrate` signal).
|
| 225 |
+
|
| 226 |
+
Assuming you have an application with an
|
| 227 |
+
:attr:`~django.db.models.Options.app_label` ``foo`` and a model named ``Bar``,
|
| 228 |
+
to test for basic permissions you should use:
|
| 229 |
+
|
| 230 |
+
* add: ``user.has_perm('foo.add_bar')``
|
| 231 |
+
* change: ``user.has_perm('foo.change_bar')``
|
| 232 |
+
* delete: ``user.has_perm('foo.delete_bar')``
|
| 233 |
+
* view: ``user.has_perm('foo.view_bar')``
|
| 234 |
+
|
| 235 |
+
The :class:`~django.contrib.auth.models.Permission` model is rarely accessed
|
| 236 |
+
directly.
|
| 237 |
+
|
| 238 |
+
Groups
|
| 239 |
+
------
|
| 240 |
+
|
| 241 |
+
:class:`django.contrib.auth.models.Group` models are a generic way of
|
| 242 |
+
categorizing users so you can apply permissions, or some other label, to those
|
| 243 |
+
users. A user can belong to any number of groups.
|
| 244 |
+
|
| 245 |
+
A user in a group automatically has the permissions granted to that group. For
|
| 246 |
+
example, if the group ``Site editors`` has the permission
|
| 247 |
+
``can_edit_home_page``, any user in that group will have that permission.
|
| 248 |
+
|
| 249 |
+
Beyond permissions, groups are a convenient way to categorize users to give
|
| 250 |
+
them some label, or extended functionality. For example, you could create a
|
| 251 |
+
group ``'Special users'``, and you could write code that could, say, give them
|
| 252 |
+
access to a members-only portion of your site, or send them members-only email
|
| 253 |
+
messages.
|
| 254 |
+
|
| 255 |
+
Programmatically creating permissions
|
| 256 |
+
-------------------------------------
|
| 257 |
+
|
| 258 |
+
While :ref:`custom permissions <custom-permissions>` can be defined within
|
| 259 |
+
a model's ``Meta`` class, you can also create permissions directly. For
|
| 260 |
+
example, you can create the ``can_publish`` permission for a ``BlogPost`` model
|
| 261 |
+
in ``myapp``::
|
| 262 |
+
|
| 263 |
+
from myapp.models import BlogPost
|
| 264 |
+
from django.contrib.auth.models import Permission
|
| 265 |
+
from django.contrib.contenttypes.models import ContentType
|
| 266 |
+
|
| 267 |
+
content_type = ContentType.objects.get_for_model(BlogPost)
|
| 268 |
+
permission = Permission.objects.create(
|
| 269 |
+
codename="can_publish",
|
| 270 |
+
name="Can Publish Posts",
|
| 271 |
+
content_type=content_type,
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
The permission can then be assigned to a
|
| 275 |
+
:class:`~django.contrib.auth.models.User` via its ``user_permissions``
|
| 276 |
+
attribute or to a :class:`~django.contrib.auth.models.Group` via its
|
| 277 |
+
``permissions`` attribute.
|
| 278 |
+
|
| 279 |
+
.. admonition:: Proxy models need their own content type
|
| 280 |
+
|
| 281 |
+
If you want to create :ref:`permissions for a proxy model
|
| 282 |
+
<proxy-models-permissions-topic>`, pass ``for_concrete_model=False`` to
|
| 283 |
+
:meth:`.ContentTypeManager.get_for_model` to get the appropriate
|
| 284 |
+
``ContentType``::
|
| 285 |
+
|
| 286 |
+
content_type = ContentType.objects.get_for_model(
|
| 287 |
+
BlogPostProxy, for_concrete_model=False
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
Permission caching
|
| 291 |
+
------------------
|
| 292 |
+
|
| 293 |
+
The :class:`~django.contrib.auth.backends.ModelBackend` caches permissions on
|
| 294 |
+
the user object after the first time they need to be fetched for a permissions
|
| 295 |
+
check. This is typically fine for the request-response cycle since permissions
|
| 296 |
+
aren't typically checked immediately after they are added (in the admin, for
|
| 297 |
+
example). If you are adding permissions and checking them immediately
|
| 298 |
+
afterward, in a test or view for example, the easiest solution is to re-fetch
|
| 299 |
+
the user from the database. For example::
|
| 300 |
+
|
| 301 |
+
from django.contrib.auth.models import Permission, User
|
| 302 |
+
from django.contrib.contenttypes.models import ContentType
|
| 303 |
+
from django.shortcuts import get_object_or_404
|
| 304 |
+
|
| 305 |
+
from myapp.models import BlogPost
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
def user_gains_perms(request, user_id):
|
| 309 |
+
user = get_object_or_404(User, pk=user_id)
|
| 310 |
+
# any permission check will cache the current set of permissions
|
| 311 |
+
user.has_perm("myapp.change_blogpost")
|
| 312 |
+
|
| 313 |
+
content_type = ContentType.objects.get_for_model(BlogPost)
|
| 314 |
+
permission = Permission.objects.get(
|
| 315 |
+
codename="change_blogpost",
|
| 316 |
+
content_type=content_type,
|
| 317 |
+
)
|
| 318 |
+
user.user_permissions.add(permission)
|
| 319 |
+
|
| 320 |
+
# Checking the cached permission set
|
| 321 |
+
user.has_perm("myapp.change_blogpost") # False
|
| 322 |
+
|
| 323 |
+
# Request new instance of User
|
| 324 |
+
# Be aware that user.refresh_from_db() won't clear the cache.
|
| 325 |
+
user = get_object_or_404(User, pk=user_id)
|
| 326 |
+
|
| 327 |
+
# Permission cache is repopulated from the database
|
| 328 |
+
user.has_perm("myapp.change_blogpost") # True
|
| 329 |
+
|
| 330 |
+
...
|
| 331 |
+
|
| 332 |
+
.. _proxy-models-permissions-topic:
|
| 333 |
+
|
| 334 |
+
Proxy models
|
| 335 |
+
------------
|
| 336 |
+
|
| 337 |
+
Proxy models work exactly the same way as concrete models. Permissions are
|
| 338 |
+
created using the own content type of the proxy model. Proxy models don't
|
| 339 |
+
inherit the permissions of the concrete model they subclass::
|
| 340 |
+
|
| 341 |
+
class Person(models.Model):
|
| 342 |
+
class Meta:
|
| 343 |
+
permissions = [("can_eat_pizzas", "Can eat pizzas")]
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
class Student(Person):
|
| 347 |
+
class Meta:
|
| 348 |
+
proxy = True
|
| 349 |
+
permissions = [("can_deliver_pizzas", "Can deliver pizzas")]
|
| 350 |
+
|
| 351 |
+
.. code-block:: pycon
|
| 352 |
+
|
| 353 |
+
>>> # Fetch the content type for the proxy model.
|
| 354 |
+
>>> content_type = ContentType.objects.get_for_model(Student, for_concrete_model=False)
|
| 355 |
+
>>> student_permissions = Permission.objects.filter(content_type=content_type)
|
| 356 |
+
>>> [p.codename for p in student_permissions]
|
| 357 |
+
['add_student', 'change_student', 'delete_student', 'view_student',
|
| 358 |
+
'can_deliver_pizzas']
|
| 359 |
+
>>> for permission in student_permissions:
|
| 360 |
+
... user.user_permissions.add(permission)
|
| 361 |
+
...
|
| 362 |
+
>>> user.has_perm("app.add_person")
|
| 363 |
+
False
|
| 364 |
+
>>> user.has_perm("app.can_eat_pizzas")
|
| 365 |
+
False
|
| 366 |
+
>>> user.has_perms(("app.add_student", "app.can_deliver_pizzas"))
|
| 367 |
+
True
|
| 368 |
+
|
| 369 |
+
.. _auth-web-requests:
|
| 370 |
+
|
| 371 |
+
Authentication in web requests
|
| 372 |
+
==============================
|
| 373 |
+
|
| 374 |
+
Django uses :doc:`sessions </topics/http/sessions>` and middleware to hook the
|
| 375 |
+
authentication system into :class:`request objects <django.http.HttpRequest>`.
|
| 376 |
+
|
| 377 |
+
These provide a :attr:`request.user <django.http.HttpRequest.user>` attribute
|
| 378 |
+
and a :meth:`request.auser <django.http.HttpRequest.auser>` async method
|
| 379 |
+
on every request which represents the current user. If the current user has not
|
| 380 |
+
logged in, this attribute will be set to an instance
|
| 381 |
+
of :class:`~django.contrib.auth.models.AnonymousUser`, otherwise it will be an
|
| 382 |
+
instance of :class:`~django.contrib.auth.models.User`.
|
| 383 |
+
|
| 384 |
+
You can tell them apart with
|
| 385 |
+
:attr:`~django.contrib.auth.models.User.is_authenticated`, like so::
|
| 386 |
+
|
| 387 |
+
if request.user.is_authenticated:
|
| 388 |
+
# Do something for authenticated users.
|
| 389 |
+
...
|
| 390 |
+
else:
|
| 391 |
+
# Do something for anonymous users.
|
| 392 |
+
...
|
| 393 |
+
|
| 394 |
+
Or in an asynchronous view::
|
| 395 |
+
|
| 396 |
+
user = await request.auser()
|
| 397 |
+
if user.is_authenticated:
|
| 398 |
+
# Do something for authenticated users.
|
| 399 |
+
...
|
| 400 |
+
else:
|
| 401 |
+
# Do something for anonymous users.
|
| 402 |
+
...
|
| 403 |
+
|
| 404 |
+
.. versionchanged:: 5.0
|
| 405 |
+
|
| 406 |
+
The :meth:`.HttpRequest.auser` method was added.
|
| 407 |
+
|
| 408 |
+
.. _how-to-log-a-user-in:
|
| 409 |
+
|
| 410 |
+
How to log a user in
|
| 411 |
+
--------------------
|
| 412 |
+
|
| 413 |
+
If you have an authenticated user you want to attach to the current session
|
| 414 |
+
- this is done with a :func:`~django.contrib.auth.login` function.
|
| 415 |
+
|
| 416 |
+
.. function:: login(request, user, backend=None)
|
| 417 |
+
.. function:: alogin(request, user, backend=None)
|
| 418 |
+
|
| 419 |
+
*Asynchronous version*: ``alogin()``
|
| 420 |
+
|
| 421 |
+
To log a user in, from a view, use :func:`~django.contrib.auth.login()`. It
|
| 422 |
+
takes an :class:`~django.http.HttpRequest` object and a
|
| 423 |
+
:class:`~django.contrib.auth.models.User` object.
|
| 424 |
+
:func:`~django.contrib.auth.login()` saves the user's ID in the session,
|
| 425 |
+
using Django's session framework.
|
| 426 |
+
|
| 427 |
+
Note that any data set during the anonymous session is retained in the
|
| 428 |
+
session after a user logs in.
|
| 429 |
+
|
| 430 |
+
This example shows how you might use both
|
| 431 |
+
:func:`~django.contrib.auth.authenticate()` and
|
| 432 |
+
:func:`~django.contrib.auth.login()`::
|
| 433 |
+
|
| 434 |
+
from django.contrib.auth import authenticate, login
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
def my_view(request):
|
| 438 |
+
username = request.POST["username"]
|
| 439 |
+
password = request.POST["password"]
|
| 440 |
+
user = authenticate(request, username=username, password=password)
|
| 441 |
+
if user is not None:
|
| 442 |
+
login(request, user)
|
| 443 |
+
# Redirect to a success page.
|
| 444 |
+
...
|
| 445 |
+
else:
|
| 446 |
+
# Return an 'invalid login' error message.
|
| 447 |
+
...
|
| 448 |
+
|
| 449 |
+
.. versionchanged:: 5.0
|
| 450 |
+
|
| 451 |
+
``alogin()`` function was added.
|
| 452 |
+
|
| 453 |
+
Selecting the authentication backend
|
| 454 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 455 |
+
|
| 456 |
+
When a user logs in, the user's ID and the backend that was used for
|
| 457 |
+
authentication are saved in the user's session. This allows the same
|
| 458 |
+
:ref:`authentication backend <authentication-backends>` to fetch the user's
|
| 459 |
+
details on a future request. The authentication backend to save in the session
|
| 460 |
+
is selected as follows:
|
| 461 |
+
|
| 462 |
+
#. Use the value of the optional ``backend`` argument, if provided.
|
| 463 |
+
#. Use the value of the ``user.backend`` attribute, if present. This allows
|
| 464 |
+
pairing :func:`~django.contrib.auth.authenticate()` and
|
| 465 |
+
:func:`~django.contrib.auth.login()`:
|
| 466 |
+
:func:`~django.contrib.auth.authenticate()`
|
| 467 |
+
sets the ``user.backend`` attribute on the user object it returns.
|
| 468 |
+
#. Use the ``backend`` in :setting:`AUTHENTICATION_BACKENDS`, if there is only
|
| 469 |
+
one.
|
| 470 |
+
#. Otherwise, raise an exception.
|
| 471 |
+
|
| 472 |
+
In cases 1 and 2, the value of the ``backend`` argument or the ``user.backend``
|
| 473 |
+
attribute should be a dotted import path string (like that found in
|
| 474 |
+
:setting:`AUTHENTICATION_BACKENDS`), not the actual backend class.
|
| 475 |
+
|
| 476 |
+
How to log a user out
|
| 477 |
+
---------------------
|
| 478 |
+
|
| 479 |
+
.. function:: logout(request)
|
| 480 |
+
.. function:: alogout(request)
|
| 481 |
+
|
| 482 |
+
*Asynchronous version*: ``alogout()``
|
| 483 |
+
|
| 484 |
+
To log out a user who has been logged in via
|
| 485 |
+
:func:`django.contrib.auth.login()`, use
|
| 486 |
+
:func:`django.contrib.auth.logout()` within your view. It takes an
|
| 487 |
+
:class:`~django.http.HttpRequest` object and has no return value.
|
| 488 |
+
Example::
|
| 489 |
+
|
| 490 |
+
from django.contrib.auth import logout
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
def logout_view(request):
|
| 494 |
+
logout(request)
|
| 495 |
+
# Redirect to a success page.
|
| 496 |
+
|
| 497 |
+
Note that :func:`~django.contrib.auth.logout()` doesn't throw any errors if
|
| 498 |
+
the user wasn't logged in.
|
| 499 |
+
|
| 500 |
+
When you call :func:`~django.contrib.auth.logout()`, the session data for
|
| 501 |
+
the current request is completely cleaned out. All existing data is
|
| 502 |
+
removed. This is to prevent another person from using the same web browser
|
| 503 |
+
to log in and have access to the previous user's session data. If you want
|
| 504 |
+
to put anything into the session that will be available to the user
|
| 505 |
+
immediately after logging out, do that *after* calling
|
| 506 |
+
:func:`django.contrib.auth.logout()`.
|
| 507 |
+
|
| 508 |
+
.. versionchanged:: 5.0
|
| 509 |
+
|
| 510 |
+
``alogout()`` function was added.
|
| 511 |
+
|
| 512 |
+
Limiting access to logged-in users
|
| 513 |
+
----------------------------------
|
| 514 |
+
|
| 515 |
+
The raw way
|
| 516 |
+
~~~~~~~~~~~
|
| 517 |
+
|
| 518 |
+
The raw way to limit access to pages is to check
|
| 519 |
+
:attr:`request.user.is_authenticated
|
| 520 |
+
<django.contrib.auth.models.User.is_authenticated>` and either redirect to a
|
| 521 |
+
login page::
|
| 522 |
+
|
| 523 |
+
from django.conf import settings
|
| 524 |
+
from django.shortcuts import redirect
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
def my_view(request):
|
| 528 |
+
if not request.user.is_authenticated:
|
| 529 |
+
return redirect(f"{settings.LOGIN_URL}?next={request.path}")
|
| 530 |
+
# ...
|
| 531 |
+
|
| 532 |
+
...or display an error message::
|
| 533 |
+
|
| 534 |
+
from django.shortcuts import render
|
| 535 |
+
|
| 536 |
+
|
| 537 |
+
def my_view(request):
|
| 538 |
+
if not request.user.is_authenticated:
|
| 539 |
+
return render(request, "myapp/login_error.html")
|
| 540 |
+
# ...
|
| 541 |
+
|
| 542 |
+
.. currentmodule:: django.contrib.auth.decorators
|
| 543 |
+
|
| 544 |
+
The ``login_required`` decorator
|
| 545 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 546 |
+
|
| 547 |
+
.. function:: login_required(redirect_field_name='next', login_url=None)
|
| 548 |
+
|
| 549 |
+
As a shortcut, you can use the convenient
|
| 550 |
+
:func:`~django.contrib.auth.decorators.login_required` decorator::
|
| 551 |
+
|
| 552 |
+
from django.contrib.auth.decorators import login_required
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
@login_required
|
| 556 |
+
def my_view(request):
|
| 557 |
+
...
|
| 558 |
+
|
| 559 |
+
:func:`~django.contrib.auth.decorators.login_required` does the following:
|
| 560 |
+
|
| 561 |
+
* If the user isn't logged in, redirect to
|
| 562 |
+
:setting:`settings.LOGIN_URL <LOGIN_URL>`, passing the current absolute
|
| 563 |
+
path in the query string. Example: ``/accounts/login/?next=/polls/3/``.
|
| 564 |
+
|
| 565 |
+
* If the user is logged in, execute the view normally. The view code is
|
| 566 |
+
free to assume the user is logged in.
|
| 567 |
+
|
| 568 |
+
By default, the path that the user should be redirected to upon
|
| 569 |
+
successful authentication is stored in a query string parameter called
|
| 570 |
+
``"next"``. If you would prefer to use a different name for this parameter,
|
| 571 |
+
:func:`~django.contrib.auth.decorators.login_required` takes an
|
| 572 |
+
optional ``redirect_field_name`` parameter::
|
| 573 |
+
|
| 574 |
+
from django.contrib.auth.decorators import login_required
|
| 575 |
+
|
| 576 |
+
|
| 577 |
+
@login_required(redirect_field_name="my_redirect_field")
|
| 578 |
+
def my_view(request):
|
| 579 |
+
...
|
| 580 |
+
|
| 581 |
+
Note that if you provide a value to ``redirect_field_name``, you will most
|
| 582 |
+
likely need to customize your login template as well, since the template
|
| 583 |
+
context variable which stores the redirect path will use the value of
|
| 584 |
+
``redirect_field_name`` as its key rather than ``"next"`` (the default).
|
| 585 |
+
|
| 586 |
+
:func:`~django.contrib.auth.decorators.login_required` also takes an
|
| 587 |
+
optional ``login_url`` parameter. Example::
|
| 588 |
+
|
| 589 |
+
from django.contrib.auth.decorators import login_required
|
| 590 |
+
|
| 591 |
+
|
| 592 |
+
@login_required(login_url="/accounts/login/")
|
| 593 |
+
def my_view(request):
|
| 594 |
+
...
|
| 595 |
+
|
| 596 |
+
Note that if you don't specify the ``login_url`` parameter, you'll need to
|
| 597 |
+
ensure that the :setting:`settings.LOGIN_URL <LOGIN_URL>` and your login
|
| 598 |
+
view are properly associated. For example, using the defaults, add the
|
| 599 |
+
following lines to your URLconf::
|
| 600 |
+
|
| 601 |
+
from django.contrib.auth import views as auth_views
|
| 602 |
+
|
| 603 |
+
path("accounts/login/", auth_views.LoginView.as_view()),
|
| 604 |
+
|
| 605 |
+
The :setting:`settings.LOGIN_URL <LOGIN_URL>` also accepts view function
|
| 606 |
+
names and :ref:`named URL patterns <naming-url-patterns>`. This allows you
|
| 607 |
+
to freely remap your login view within your URLconf without having to
|
| 608 |
+
update the setting.
|
| 609 |
+
|
| 610 |
+
.. note::
|
| 611 |
+
|
| 612 |
+
The ``login_required`` decorator does NOT check the ``is_active`` flag on a
|
| 613 |
+
user, but the default :setting:`AUTHENTICATION_BACKENDS` reject inactive
|
| 614 |
+
users.
|
| 615 |
+
|
| 616 |
+
.. seealso::
|
| 617 |
+
|
| 618 |
+
If you are writing custom views for Django's admin (or need the same
|
| 619 |
+
authorization check that the built-in views use), you may find the
|
| 620 |
+
:func:`django.contrib.admin.views.decorators.staff_member_required`
|
| 621 |
+
decorator a useful alternative to ``login_required()``.
|
| 622 |
+
|
| 623 |
+
.. currentmodule:: django.contrib.auth.mixins
|
| 624 |
+
|
| 625 |
+
The ``LoginRequiredMixin`` mixin
|
| 626 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 627 |
+
|
| 628 |
+
When using :doc:`class-based views </topics/class-based-views/index>`, you can
|
| 629 |
+
achieve the same behavior as with ``login_required`` by using the
|
| 630 |
+
``LoginRequiredMixin``. This mixin should be at the leftmost position in the
|
| 631 |
+
inheritance list.
|
| 632 |
+
|
| 633 |
+
.. class:: LoginRequiredMixin
|
| 634 |
+
|
| 635 |
+
If a view is using this mixin, all requests by non-authenticated users will
|
| 636 |
+
be redirected to the login page or shown an HTTP 403 Forbidden error,
|
| 637 |
+
depending on the
|
| 638 |
+
:attr:`~django.contrib.auth.mixins.AccessMixin.raise_exception` parameter.
|
| 639 |
+
|
| 640 |
+
You can set any of the parameters of
|
| 641 |
+
:class:`~django.contrib.auth.mixins.AccessMixin` to customize the handling
|
| 642 |
+
of unauthorized users::
|
| 643 |
+
|
| 644 |
+
|
| 645 |
+
from django.contrib.auth.mixins import LoginRequiredMixin
|
| 646 |
+
|
| 647 |
+
|
| 648 |
+
class MyView(LoginRequiredMixin, View):
|
| 649 |
+
login_url = "/login/"
|
| 650 |
+
redirect_field_name = "redirect_to"
|
| 651 |
+
|
| 652 |
+
.. note::
|
| 653 |
+
|
| 654 |
+
Just as the ``login_required`` decorator, this mixin does NOT check the
|
| 655 |
+
``is_active`` flag on a user, but the default
|
| 656 |
+
:setting:`AUTHENTICATION_BACKENDS` reject inactive users.
|
| 657 |
+
|
| 658 |
+
.. currentmodule:: django.contrib.auth.decorators
|
| 659 |
+
|
| 660 |
+
Limiting access to logged-in users that pass a test
|
| 661 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 662 |
+
|
| 663 |
+
To limit access based on certain permissions or some other test, you'd do
|
| 664 |
+
essentially the same thing as described in the previous section.
|
| 665 |
+
|
| 666 |
+
You can run your test on :attr:`request.user <django.http.HttpRequest.user>` in
|
| 667 |
+
the view directly. For example, this view checks to make sure the user has an
|
| 668 |
+
email in the desired domain and if not, redirects to the login page::
|
| 669 |
+
|
| 670 |
+
from django.shortcuts import redirect
|
| 671 |
+
|
| 672 |
+
|
| 673 |
+
def my_view(request):
|
| 674 |
+
if not request.user.email.endswith("@example.com"):
|
| 675 |
+
return redirect("/login/?next=%s" % request.path)
|
| 676 |
+
# ...
|
| 677 |
+
|
| 678 |
+
.. function:: user_passes_test(test_func, login_url=None, redirect_field_name='next')
|
| 679 |
+
|
| 680 |
+
As a shortcut, you can use the convenient ``user_passes_test`` decorator
|
| 681 |
+
which performs a redirect when the callable returns ``False``::
|
| 682 |
+
|
| 683 |
+
from django.contrib.auth.decorators import user_passes_test
|
| 684 |
+
|
| 685 |
+
|
| 686 |
+
def email_check(user):
|
| 687 |
+
return user.email.endswith("@example.com")
|
| 688 |
+
|
| 689 |
+
|
| 690 |
+
@user_passes_test(email_check)
|
| 691 |
+
def my_view(request):
|
| 692 |
+
...
|
| 693 |
+
|
| 694 |
+
:func:`~django.contrib.auth.decorators.user_passes_test` takes a required
|
| 695 |
+
argument: a callable that takes a
|
| 696 |
+
:class:`~django.contrib.auth.models.User` object and returns ``True`` if
|
| 697 |
+
the user is allowed to view the page. Note that
|
| 698 |
+
:func:`~django.contrib.auth.decorators.user_passes_test` does not
|
| 699 |
+
automatically check that the :class:`~django.contrib.auth.models.User` is
|
| 700 |
+
not anonymous.
|
| 701 |
+
|
| 702 |
+
:func:`~django.contrib.auth.decorators.user_passes_test` takes two
|
| 703 |
+
optional arguments:
|
| 704 |
+
|
| 705 |
+
``login_url``
|
| 706 |
+
Lets you specify the URL that users who don't pass the test will be
|
| 707 |
+
redirected to. It may be a login page and defaults to
|
| 708 |
+
:setting:`settings.LOGIN_URL <LOGIN_URL>` if you don't specify one.
|
| 709 |
+
|
| 710 |
+
``redirect_field_name``
|
| 711 |
+
Same as for :func:`~django.contrib.auth.decorators.login_required`.
|
| 712 |
+
Setting it to ``None`` removes it from the URL, which you may want to do
|
| 713 |
+
if you are redirecting users that don't pass the test to a non-login
|
| 714 |
+
page where there's no "next page".
|
| 715 |
+
|
| 716 |
+
For example::
|
| 717 |
+
|
| 718 |
+
@user_passes_test(email_check, login_url="/login/")
|
| 719 |
+
def my_view(request):
|
| 720 |
+
...
|
| 721 |
+
|
| 722 |
+
.. currentmodule:: django.contrib.auth.mixins
|
| 723 |
+
|
| 724 |
+
.. class:: UserPassesTestMixin
|
| 725 |
+
|
| 726 |
+
When using :doc:`class-based views </topics/class-based-views/index>`, you
|
| 727 |
+
can use the ``UserPassesTestMixin`` to do this.
|
| 728 |
+
|
| 729 |
+
.. method:: test_func()
|
| 730 |
+
|
| 731 |
+
You have to override the ``test_func()`` method of the class to
|
| 732 |
+
provide the test that is performed. Furthermore, you can set any of the
|
| 733 |
+
parameters of :class:`~django.contrib.auth.mixins.AccessMixin` to
|
| 734 |
+
customize the handling of unauthorized users::
|
| 735 |
+
|
| 736 |
+
from django.contrib.auth.mixins import UserPassesTestMixin
|
| 737 |
+
|
| 738 |
+
|
| 739 |
+
class MyView(UserPassesTestMixin, View):
|
| 740 |
+
def test_func(self):
|
| 741 |
+
return self.request.user.email.endswith("@example.com")
|
| 742 |
+
|
| 743 |
+
.. method:: get_test_func()
|
| 744 |
+
|
| 745 |
+
You can also override the ``get_test_func()`` method to have the mixin
|
| 746 |
+
use a differently named function for its checks (instead of
|
| 747 |
+
:meth:`test_func`).
|
| 748 |
+
|
| 749 |
+
.. admonition:: Stacking ``UserPassesTestMixin``
|
| 750 |
+
|
| 751 |
+
Due to the way ``UserPassesTestMixin`` is implemented, you cannot stack
|
| 752 |
+
them in your inheritance list. The following does NOT work::
|
| 753 |
+
|
| 754 |
+
class TestMixin1(UserPassesTestMixin):
|
| 755 |
+
def test_func(self):
|
| 756 |
+
return self.request.user.email.endswith("@example.com")
|
| 757 |
+
|
| 758 |
+
|
| 759 |
+
class TestMixin2(UserPassesTestMixin):
|
| 760 |
+
def test_func(self):
|
| 761 |
+
return self.request.user.username.startswith("django")
|
| 762 |
+
|
| 763 |
+
|
| 764 |
+
class MyView(TestMixin1, TestMixin2, View):
|
| 765 |
+
...
|
| 766 |
+
|
| 767 |
+
If ``TestMixin1`` would call ``super()`` and take that result into
|
| 768 |
+
account, ``TestMixin1`` wouldn't work standalone anymore.
|
| 769 |
+
|
| 770 |
+
.. currentmodule:: django.contrib.auth.decorators
|
| 771 |
+
|
| 772 |
+
The ``permission_required`` decorator
|
| 773 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 774 |
+
|
| 775 |
+
.. function:: permission_required(perm, login_url=None, raise_exception=False)
|
| 776 |
+
|
| 777 |
+
It's a relatively common task to check whether a user has a particular
|
| 778 |
+
permission. For that reason, Django provides a shortcut for that case: the
|
| 779 |
+
:func:`~django.contrib.auth.decorators.permission_required()` decorator.::
|
| 780 |
+
|
| 781 |
+
from django.contrib.auth.decorators import permission_required
|
| 782 |
+
|
| 783 |
+
|
| 784 |
+
@permission_required("polls.add_choice")
|
| 785 |
+
def my_view(request):
|
| 786 |
+
...
|
| 787 |
+
|
| 788 |
+
Just like the :meth:`~django.contrib.auth.models.User.has_perm` method,
|
| 789 |
+
permission names take the form ``"<app label>.<permission codename>"``
|
| 790 |
+
(i.e. ``polls.add_choice`` for a permission on a model in the ``polls``
|
| 791 |
+
application).
|
| 792 |
+
|
| 793 |
+
The decorator may also take an iterable of permissions, in which case the
|
| 794 |
+
user must have all of the permissions in order to access the view.
|
| 795 |
+
|
| 796 |
+
Note that :func:`~django.contrib.auth.decorators.permission_required()`
|
| 797 |
+
also takes an optional ``login_url`` parameter::
|
| 798 |
+
|
| 799 |
+
from django.contrib.auth.decorators import permission_required
|
| 800 |
+
|
| 801 |
+
|
| 802 |
+
@permission_required("polls.add_choice", login_url="/loginpage/")
|
| 803 |
+
def my_view(request):
|
| 804 |
+
...
|
| 805 |
+
|
| 806 |
+
As in the :func:`~django.contrib.auth.decorators.login_required` decorator,
|
| 807 |
+
``login_url`` defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`.
|
| 808 |
+
|
| 809 |
+
If the ``raise_exception`` parameter is given, the decorator will raise
|
| 810 |
+
:exc:`~django.core.exceptions.PermissionDenied`, prompting :ref:`the 403
|
| 811 |
+
(HTTP Forbidden) view<http_forbidden_view>` instead of redirecting to the
|
| 812 |
+
login page.
|
| 813 |
+
|
| 814 |
+
If you want to use ``raise_exception`` but also give your users a chance to
|
| 815 |
+
login first, you can add the
|
| 816 |
+
:func:`~django.contrib.auth.decorators.login_required` decorator::
|
| 817 |
+
|
| 818 |
+
from django.contrib.auth.decorators import login_required, permission_required
|
| 819 |
+
|
| 820 |
+
|
| 821 |
+
@login_required
|
| 822 |
+
@permission_required("polls.add_choice", raise_exception=True)
|
| 823 |
+
def my_view(request):
|
| 824 |
+
...
|
| 825 |
+
|
| 826 |
+
This also avoids a redirect loop when :class:`.LoginView`'s
|
| 827 |
+
``redirect_authenticated_user=True`` and the logged-in user doesn't have
|
| 828 |
+
all of the required permissions.
|
| 829 |
+
|
| 830 |
+
.. currentmodule:: django.contrib.auth.mixins
|
| 831 |
+
|
| 832 |
+
The ``PermissionRequiredMixin`` mixin
|
| 833 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 834 |
+
|
| 835 |
+
To apply permission checks to :doc:`class-based views
|
| 836 |
+
</ref/class-based-views/index>`, you can use the ``PermissionRequiredMixin``:
|
| 837 |
+
|
| 838 |
+
.. class:: PermissionRequiredMixin
|
| 839 |
+
|
| 840 |
+
This mixin, just like the ``permission_required``
|
| 841 |
+
decorator, checks whether the user accessing a view has all given
|
| 842 |
+
permissions. You should specify the permission (or an iterable of
|
| 843 |
+
permissions) using the ``permission_required`` parameter::
|
| 844 |
+
|
| 845 |
+
from django.contrib.auth.mixins import PermissionRequiredMixin
|
| 846 |
+
|
| 847 |
+
|
| 848 |
+
class MyView(PermissionRequiredMixin, View):
|
| 849 |
+
permission_required = "polls.add_choice"
|
| 850 |
+
# Or multiple of permissions:
|
| 851 |
+
permission_required = ["polls.view_choice", "polls.change_choice"]
|
| 852 |
+
|
| 853 |
+
You can set any of the parameters of
|
| 854 |
+
:class:`~django.contrib.auth.mixins.AccessMixin` to customize the handling
|
| 855 |
+
of unauthorized users.
|
| 856 |
+
|
| 857 |
+
You may also override these methods:
|
| 858 |
+
|
| 859 |
+
.. method:: get_permission_required()
|
| 860 |
+
|
| 861 |
+
Returns an iterable of permission names used by the mixin. Defaults to
|
| 862 |
+
the ``permission_required`` attribute, converted to a tuple if
|
| 863 |
+
necessary.
|
| 864 |
+
|
| 865 |
+
.. method:: has_permission()
|
| 866 |
+
|
| 867 |
+
Returns a boolean denoting whether the current user has permission to
|
| 868 |
+
execute the decorated view. By default, this returns the result of
|
| 869 |
+
calling :meth:`~django.contrib.auth.models.User.has_perms()` with the
|
| 870 |
+
list of permissions returned by :meth:`get_permission_required()`.
|
| 871 |
+
|
| 872 |
+
Redirecting unauthorized requests in class-based views
|
| 873 |
+
------------------------------------------------------
|
| 874 |
+
|
| 875 |
+
To ease the handling of access restrictions in :doc:`class-based views
|
| 876 |
+
</ref/class-based-views/index>`, the ``AccessMixin`` can be used to configure
|
| 877 |
+
the behavior of a view when access is denied. Authenticated users are denied
|
| 878 |
+
access with an HTTP 403 Forbidden response. Anonymous users are redirected to
|
| 879 |
+
the login page or shown an HTTP 403 Forbidden response, depending on the
|
| 880 |
+
:attr:`~django.contrib.auth.mixins.AccessMixin.raise_exception` attribute.
|
| 881 |
+
|
| 882 |
+
.. class:: AccessMixin
|
| 883 |
+
|
| 884 |
+
.. attribute:: login_url
|
| 885 |
+
|
| 886 |
+
Default return value for :meth:`get_login_url`. Defaults to ``None``
|
| 887 |
+
in which case :meth:`get_login_url` falls back to
|
| 888 |
+
:setting:`settings.LOGIN_URL <LOGIN_URL>`.
|
| 889 |
+
|
| 890 |
+
.. attribute:: permission_denied_message
|
| 891 |
+
|
| 892 |
+
Default return value for :meth:`get_permission_denied_message`.
|
| 893 |
+
Defaults to an empty string.
|
| 894 |
+
|
| 895 |
+
.. attribute:: redirect_field_name
|
| 896 |
+
|
| 897 |
+
Default return value for :meth:`get_redirect_field_name`. Defaults to
|
| 898 |
+
``"next"``.
|
| 899 |
+
|
| 900 |
+
.. attribute:: raise_exception
|
| 901 |
+
|
| 902 |
+
If this attribute is set to ``True``, a
|
| 903 |
+
:class:`~django.core.exceptions.PermissionDenied` exception is raised
|
| 904 |
+
when the conditions are not met. When ``False`` (the default),
|
| 905 |
+
anonymous users are redirected to the login page.
|
| 906 |
+
|
| 907 |
+
.. method:: get_login_url()
|
| 908 |
+
|
| 909 |
+
Returns the URL that users who don't pass the test will be redirected
|
| 910 |
+
to. Returns :attr:`login_url` if set, or :setting:`settings.LOGIN_URL
|
| 911 |
+
<LOGIN_URL>` otherwise.
|
| 912 |
+
|
| 913 |
+
.. method:: get_permission_denied_message()
|
| 914 |
+
|
| 915 |
+
When :attr:`raise_exception` is ``True``, this method can be used to
|
| 916 |
+
control the error message passed to the error handler for display to
|
| 917 |
+
the user. Returns the :attr:`permission_denied_message` attribute by
|
| 918 |
+
default.
|
| 919 |
+
|
| 920 |
+
.. method:: get_redirect_field_name()
|
| 921 |
+
|
| 922 |
+
Returns the name of the query parameter that will contain the URL the
|
| 923 |
+
user should be redirected to after a successful login. If you set this
|
| 924 |
+
to ``None``, a query parameter won't be added. Returns the
|
| 925 |
+
:attr:`redirect_field_name` attribute by default.
|
| 926 |
+
|
| 927 |
+
.. method:: handle_no_permission()
|
| 928 |
+
|
| 929 |
+
Depending on the value of ``raise_exception``, the method either raises
|
| 930 |
+
a :exc:`~django.core.exceptions.PermissionDenied` exception or
|
| 931 |
+
redirects the user to the ``login_url``, optionally including the
|
| 932 |
+
``redirect_field_name`` if it is set.
|
| 933 |
+
|
| 934 |
+
.. currentmodule:: django.contrib.auth
|
| 935 |
+
|
| 936 |
+
.. _session-invalidation-on-password-change:
|
| 937 |
+
|
| 938 |
+
Session invalidation on password change
|
| 939 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 940 |
+
|
| 941 |
+
If your :setting:`AUTH_USER_MODEL` inherits from
|
| 942 |
+
:class:`~django.contrib.auth.models.AbstractBaseUser` or implements its own
|
| 943 |
+
:meth:`~django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash()`
|
| 944 |
+
method, authenticated sessions will include the hash returned by this function.
|
| 945 |
+
In the :class:`~django.contrib.auth.models.AbstractBaseUser` case, this is an
|
| 946 |
+
HMAC of the password field. Django verifies that the hash in the session for
|
| 947 |
+
each request matches the one that's computed during the request. This allows a
|
| 948 |
+
user to log out all of their sessions by changing their password.
|
| 949 |
+
|
| 950 |
+
The default password change views included with Django,
|
| 951 |
+
:class:`~django.contrib.auth.views.PasswordChangeView` and the
|
| 952 |
+
``user_change_password`` view in the :mod:`django.contrib.auth` admin, update
|
| 953 |
+
the session with the new password hash so that a user changing their own
|
| 954 |
+
password won't log themselves out. If you have a custom password change view
|
| 955 |
+
and wish to have similar behavior, use the :func:`update_session_auth_hash`
|
| 956 |
+
function.
|
| 957 |
+
|
| 958 |
+
.. function:: update_session_auth_hash(request, user)
|
| 959 |
+
.. function:: aupdate_session_auth_hash(request, user)
|
| 960 |
+
|
| 961 |
+
*Asynchronous version*: ``aupdate_session_auth_hash()``
|
| 962 |
+
|
| 963 |
+
This function takes the current request and the updated user object from
|
| 964 |
+
which the new session hash will be derived and updates the session hash
|
| 965 |
+
appropriately. It also rotates the session key so that a stolen session
|
| 966 |
+
cookie will be invalidated.
|
| 967 |
+
|
| 968 |
+
Example usage::
|
| 969 |
+
|
| 970 |
+
from django.contrib.auth import update_session_auth_hash
|
| 971 |
+
|
| 972 |
+
|
| 973 |
+
def password_change(request):
|
| 974 |
+
if request.method == "POST":
|
| 975 |
+
form = PasswordChangeForm(user=request.user, data=request.POST)
|
| 976 |
+
if form.is_valid():
|
| 977 |
+
form.save()
|
| 978 |
+
update_session_auth_hash(request, form.user)
|
| 979 |
+
else:
|
| 980 |
+
...
|
| 981 |
+
|
| 982 |
+
.. versionchanged:: 5.0
|
| 983 |
+
|
| 984 |
+
``aupdate_session_auth_hash()`` function was added.
|
| 985 |
+
|
| 986 |
+
.. note::
|
| 987 |
+
|
| 988 |
+
Since
|
| 989 |
+
:meth:`~django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash()`
|
| 990 |
+
is based on :setting:`SECRET_KEY`, secret key values must be
|
| 991 |
+
rotated to avoid invalidating existing sessions when updating your site to
|
| 992 |
+
use a new secret. See :setting:`SECRET_KEY_FALLBACKS` for details.
|
| 993 |
+
|
| 994 |
+
.. _built-in-auth-views:
|
| 995 |
+
|
| 996 |
+
Authentication Views
|
| 997 |
+
--------------------
|
| 998 |
+
|
| 999 |
+
.. module:: django.contrib.auth.views
|
| 1000 |
+
|
| 1001 |
+
Django provides several views that you can use for handling login, logout, and
|
| 1002 |
+
password management. These make use of the :ref:`stock auth forms
|
| 1003 |
+
<built-in-auth-forms>` but you can pass in your own forms as well.
|
| 1004 |
+
|
| 1005 |
+
Django provides no default template for the authentication views. You should
|
| 1006 |
+
create your own templates for the views you want to use. The template context
|
| 1007 |
+
is documented in each view, see :ref:`all-authentication-views`.
|
| 1008 |
+
|
| 1009 |
+
.. _using-the-views:
|
| 1010 |
+
|
| 1011 |
+
Using the views
|
| 1012 |
+
~~~~~~~~~~~~~~~
|
| 1013 |
+
|
| 1014 |
+
There are different methods to implement these views in your project. The
|
| 1015 |
+
easiest way is to include the provided URLconf in ``django.contrib.auth.urls``
|
| 1016 |
+
in your own URLconf, for example::
|
| 1017 |
+
|
| 1018 |
+
urlpatterns = [
|
| 1019 |
+
path("accounts/", include("django.contrib.auth.urls")),
|
| 1020 |
+
]
|
| 1021 |
+
|
| 1022 |
+
This will include the following URL patterns:
|
| 1023 |
+
|
| 1024 |
+
.. code-block:: text
|
| 1025 |
+
|
| 1026 |
+
accounts/login/ [name='login']
|
| 1027 |
+
accounts/logout/ [name='logout']
|
| 1028 |
+
accounts/password_change/ [name='password_change']
|
| 1029 |
+
accounts/password_change/done/ [name='password_change_done']
|
| 1030 |
+
accounts/password_reset/ [name='password_reset']
|
| 1031 |
+
accounts/password_reset/done/ [name='password_reset_done']
|
| 1032 |
+
accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
|
| 1033 |
+
accounts/reset/done/ [name='password_reset_complete']
|
| 1034 |
+
|
| 1035 |
+
The views provide a URL name for easier reference. See :doc:`the URL
|
| 1036 |
+
documentation </topics/http/urls>` for details on using named URL patterns.
|
| 1037 |
+
|
| 1038 |
+
If you want more control over your URLs, you can reference a specific view in
|
| 1039 |
+
your URLconf::
|
| 1040 |
+
|
| 1041 |
+
from django.contrib.auth import views as auth_views
|
| 1042 |
+
|
| 1043 |
+
urlpatterns = [
|
| 1044 |
+
path("change-password/", auth_views.PasswordChangeView.as_view()),
|
| 1045 |
+
]
|
| 1046 |
+
|
| 1047 |
+
The views have optional arguments you can use to alter the behavior of the
|
| 1048 |
+
view. For example, if you want to change the template name a view uses, you can
|
| 1049 |
+
provide the ``template_name`` argument. A way to do this is to provide keyword
|
| 1050 |
+
arguments in the URLconf, these will be passed on to the view. For example::
|
| 1051 |
+
|
| 1052 |
+
urlpatterns = [
|
| 1053 |
+
path(
|
| 1054 |
+
"change-password/",
|
| 1055 |
+
auth_views.PasswordChangeView.as_view(template_name="change-password.html"),
|
| 1056 |
+
),
|
| 1057 |
+
]
|
| 1058 |
+
|
| 1059 |
+
All views are :doc:`class-based </topics/class-based-views/index>`, which allows
|
| 1060 |
+
you to easily customize them by subclassing.
|
| 1061 |
+
|
| 1062 |
+
.. _all-authentication-views:
|
| 1063 |
+
|
| 1064 |
+
All authentication views
|
| 1065 |
+
~~~~~~~~~~~~~~~~~~~~~~~~
|
| 1066 |
+
|
| 1067 |
+
This is a list with all the views ``django.contrib.auth`` provides. For
|
| 1068 |
+
implementation details see :ref:`using-the-views`.
|
| 1069 |
+
|
| 1070 |
+
.. class:: LoginView
|
| 1071 |
+
|
| 1072 |
+
**URL name:** ``login``
|
| 1073 |
+
|
| 1074 |
+
See :doc:`the URL documentation </topics/http/urls>` for details on using
|
| 1075 |
+
named URL patterns.
|
| 1076 |
+
|
| 1077 |
+
**Methods and Attributes**
|
| 1078 |
+
|
| 1079 |
+
.. attribute:: template_name
|
| 1080 |
+
|
| 1081 |
+
The name of a template to display for the view used to log the user in.
|
| 1082 |
+
Defaults to :file:`registration/login.html`.
|
| 1083 |
+
|
| 1084 |
+
.. attribute:: next_page
|
| 1085 |
+
|
| 1086 |
+
The URL to redirect to after login. Defaults to
|
| 1087 |
+
:setting:`LOGIN_REDIRECT_URL`.
|
| 1088 |
+
|
| 1089 |
+
.. attribute:: redirect_field_name
|
| 1090 |
+
|
| 1091 |
+
The name of a ``GET`` field containing the URL to redirect to after
|
| 1092 |
+
login. Defaults to ``next``. Overrides the
|
| 1093 |
+
:meth:`get_default_redirect_url` URL if the given ``GET`` parameter is
|
| 1094 |
+
passed.
|
| 1095 |
+
|
| 1096 |
+
.. attribute:: authentication_form
|
| 1097 |
+
|
| 1098 |
+
A callable (typically a form class) to use for authentication. Defaults
|
| 1099 |
+
to :class:`~django.contrib.auth.forms.AuthenticationForm`.
|
| 1100 |
+
|
| 1101 |
+
.. attribute:: extra_context
|
| 1102 |
+
|
| 1103 |
+
A dictionary of context data that will be added to the default context
|
| 1104 |
+
data passed to the template.
|
| 1105 |
+
|
| 1106 |
+
.. attribute:: redirect_authenticated_user
|
| 1107 |
+
|
| 1108 |
+
A boolean that controls whether or not authenticated users accessing
|
| 1109 |
+
the login page will be redirected as if they had just successfully
|
| 1110 |
+
logged in. Defaults to ``False``.
|
| 1111 |
+
|
| 1112 |
+
.. warning::
|
| 1113 |
+
|
| 1114 |
+
If you enable ``redirect_authenticated_user``, other websites will
|
| 1115 |
+
be able to determine if their visitors are authenticated on your
|
| 1116 |
+
site by requesting redirect URLs to image files on your website. To
|
| 1117 |
+
avoid this "`social media fingerprinting
|
| 1118 |
+
<https://robinlinus.github.io/socialmedia-leak/>`_" information
|
| 1119 |
+
leakage, host all images and your favicon on a separate domain.
|
| 1120 |
+
|
| 1121 |
+
Enabling ``redirect_authenticated_user`` can also result in a
|
| 1122 |
+
redirect loop when using the :func:`.permission_required` decorator
|
| 1123 |
+
unless the ``raise_exception`` parameter is used.
|
| 1124 |
+
|
| 1125 |
+
.. attribute:: success_url_allowed_hosts
|
| 1126 |
+
|
| 1127 |
+
A :class:`set` of hosts, in addition to :meth:`request.get_host()
|
| 1128 |
+
<django.http.HttpRequest.get_host>`, that are safe for redirecting
|
| 1129 |
+
after login. Defaults to an empty :class:`set`.
|
| 1130 |
+
|
| 1131 |
+
.. method:: get_default_redirect_url()
|
| 1132 |
+
|
| 1133 |
+
Returns the URL to redirect to after login. The default implementation
|
| 1134 |
+
resolves and returns :attr:`next_page` if set, or
|
| 1135 |
+
:setting:`LOGIN_REDIRECT_URL` otherwise.
|
| 1136 |
+
|
| 1137 |
+
Here's what ``LoginView`` does:
|
| 1138 |
+
|
| 1139 |
+
* If called via ``GET``, it displays a login form that POSTs to the
|
| 1140 |
+
same URL. More on this in a bit.
|
| 1141 |
+
|
| 1142 |
+
* If called via ``POST`` with user submitted credentials, it tries to log
|
| 1143 |
+
the user in. If login is successful, the view redirects to the URL
|
| 1144 |
+
specified in ``next``. If ``next`` isn't provided, it redirects to
|
| 1145 |
+
:setting:`settings.LOGIN_REDIRECT_URL <LOGIN_REDIRECT_URL>` (which
|
| 1146 |
+
defaults to ``/accounts/profile/``). If login isn't successful, it
|
| 1147 |
+
redisplays the login form.
|
| 1148 |
+
|
| 1149 |
+
It's your responsibility to provide the html for the login template
|
| 1150 |
+
, called ``registration/login.html`` by default. This template gets passed
|
| 1151 |
+
four template context variables:
|
| 1152 |
+
|
| 1153 |
+
* ``form``: A :class:`~django.forms.Form` object representing the
|
| 1154 |
+
:class:`~django.contrib.auth.forms.AuthenticationForm`.
|
| 1155 |
+
|
| 1156 |
+
* ``next``: The URL to redirect to after successful login. This may
|
| 1157 |
+
contain a query string, too.
|
| 1158 |
+
|
| 1159 |
+
* ``site``: The current :class:`~django.contrib.sites.models.Site`,
|
| 1160 |
+
according to the :setting:`SITE_ID` setting. If you don't have the
|
| 1161 |
+
site framework installed, this will be set to an instance of
|
| 1162 |
+
:class:`~django.contrib.sites.requests.RequestSite`, which derives the
|
| 1163 |
+
site name and domain from the current
|
| 1164 |
+
:class:`~django.http.HttpRequest`.
|
| 1165 |
+
|
| 1166 |
+
* ``site_name``: An alias for ``site.name``. If you don't have the site
|
| 1167 |
+
framework installed, this will be set to the value of
|
| 1168 |
+
:attr:`request.META['SERVER_NAME'] <django.http.HttpRequest.META>`.
|
| 1169 |
+
For more on sites, see :doc:`/ref/contrib/sites`.
|
| 1170 |
+
|
| 1171 |
+
If you'd prefer not to call the template :file:`registration/login.html`,
|
| 1172 |
+
you can pass the ``template_name`` parameter via the extra arguments to
|
| 1173 |
+
the ``as_view`` method in your URLconf. For example, this URLconf line would
|
| 1174 |
+
use :file:`myapp/login.html` instead::
|
| 1175 |
+
|
| 1176 |
+
path("accounts/login/", auth_views.LoginView.as_view(template_name="myapp/login.html")),
|
| 1177 |
+
|
| 1178 |
+
You can also specify the name of the ``GET`` field which contains the URL
|
| 1179 |
+
to redirect to after login using ``redirect_field_name``. By default, the
|
| 1180 |
+
field is called ``next``.
|
| 1181 |
+
|
| 1182 |
+
Here's a sample :file:`registration/login.html` template you can use as a
|
| 1183 |
+
starting point. It assumes you have a :file:`base.html` template that
|
| 1184 |
+
defines a ``content`` block:
|
| 1185 |
+
|
| 1186 |
+
.. code-block:: html+django
|
| 1187 |
+
|
| 1188 |
+
{% extends "base.html" %}
|
| 1189 |
+
|
| 1190 |
+
{% block content %}
|
| 1191 |
+
|
| 1192 |
+
{% if form.errors %}
|
| 1193 |
+
<p>Your username and password didn't match. Please try again.</p>
|
| 1194 |
+
{% endif %}
|
| 1195 |
+
|
| 1196 |
+
{% if next %}
|
| 1197 |
+
{% if user.is_authenticated %}
|
| 1198 |
+
<p>Your account doesn't have access to this page. To proceed,
|
| 1199 |
+
please login with an account that has access.</p>
|
| 1200 |
+
{% else %}
|
| 1201 |
+
<p>Please login to see this page.</p>
|
| 1202 |
+
{% endif %}
|
| 1203 |
+
{% endif %}
|
| 1204 |
+
|
| 1205 |
+
<form method="post" action="{% url 'login' %}">
|
| 1206 |
+
{% csrf_token %}
|
| 1207 |
+
<table>
|
| 1208 |
+
<tr>
|
| 1209 |
+
<td>{{ form.username.label_tag }}</td>
|
| 1210 |
+
<td>{{ form.username }}</td>
|
| 1211 |
+
</tr>
|
| 1212 |
+
<tr>
|
| 1213 |
+
<td>{{ form.password.label_tag }}</td>
|
| 1214 |
+
<td>{{ form.password }}</td>
|
| 1215 |
+
</tr>
|
| 1216 |
+
</table>
|
| 1217 |
+
|
| 1218 |
+
<input type="submit" value="login">
|
| 1219 |
+
<input type="hidden" name="next" value="{{ next }}">
|
| 1220 |
+
</form>
|
| 1221 |
+
|
| 1222 |
+
{# Assumes you set up the password_reset view in your URLconf #}
|
| 1223 |
+
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
|
| 1224 |
+
|
| 1225 |
+
{% endblock %}
|
| 1226 |
+
|
| 1227 |
+
If you have customized authentication (see :doc:`Customizing Authentication
|
| 1228 |
+
</topics/auth/customizing>`) you can use a custom authentication form by
|
| 1229 |
+
setting the ``authentication_form`` attribute. This form must accept a
|
| 1230 |
+
``request`` keyword argument in its ``__init__()`` method and provide a
|
| 1231 |
+
``get_user()`` method which returns the authenticated user object (this
|
| 1232 |
+
method is only ever called after successful form validation).
|
| 1233 |
+
|
| 1234 |
+
.. class:: LogoutView
|
| 1235 |
+
|
| 1236 |
+
Logs a user out on ``POST`` requests.
|
| 1237 |
+
|
| 1238 |
+
**URL name:** ``logout``
|
| 1239 |
+
|
| 1240 |
+
**Attributes:**
|
| 1241 |
+
|
| 1242 |
+
.. attribute:: next_page
|
| 1243 |
+
|
| 1244 |
+
The URL to redirect to after logout. Defaults to
|
| 1245 |
+
:setting:`LOGOUT_REDIRECT_URL`.
|
| 1246 |
+
|
| 1247 |
+
.. attribute:: template_name
|
| 1248 |
+
|
| 1249 |
+
The full name of a template to display after logging the user out.
|
| 1250 |
+
Defaults to :file:`registration/logged_out.html`.
|
| 1251 |
+
|
| 1252 |
+
.. attribute:: redirect_field_name
|
| 1253 |
+
|
| 1254 |
+
The name of a ``GET`` field containing the URL to redirect to after log
|
| 1255 |
+
out. Defaults to ``'next'``. Overrides the
|
| 1256 |
+
:attr:`next_page` URL if the given ``GET`` parameter is
|
| 1257 |
+
passed.
|
| 1258 |
+
|
| 1259 |
+
.. attribute:: extra_context
|
| 1260 |
+
|
| 1261 |
+
A dictionary of context data that will be added to the default context
|
| 1262 |
+
data passed to the template.
|
| 1263 |
+
|
| 1264 |
+
.. attribute:: success_url_allowed_hosts
|
| 1265 |
+
|
| 1266 |
+
A :class:`set` of hosts, in addition to :meth:`request.get_host()
|
| 1267 |
+
<django.http.HttpRequest.get_host>`, that are safe for redirecting
|
| 1268 |
+
after logout. Defaults to an empty :class:`set`.
|
| 1269 |
+
|
| 1270 |
+
**Template context:**
|
| 1271 |
+
|
| 1272 |
+
* ``title``: The string "Logged out", localized.
|
| 1273 |
+
|
| 1274 |
+
* ``site``: The current :class:`~django.contrib.sites.models.Site`,
|
| 1275 |
+
according to the :setting:`SITE_ID` setting. If you don't have the
|
| 1276 |
+
site framework installed, this will be set to an instance of
|
| 1277 |
+
:class:`~django.contrib.sites.requests.RequestSite`, which derives the
|
| 1278 |
+
site name and domain from the current
|
| 1279 |
+
:class:`~django.http.HttpRequest`.
|
| 1280 |
+
|
| 1281 |
+
* ``site_name``: An alias for ``site.name``. If you don't have the site
|
| 1282 |
+
framework installed, this will be set to the value of
|
| 1283 |
+
:attr:`request.META['SERVER_NAME'] <django.http.HttpRequest.META>`.
|
| 1284 |
+
For more on sites, see :doc:`/ref/contrib/sites`.
|
| 1285 |
+
|
| 1286 |
+
.. function:: logout_then_login(request, login_url=None)
|
| 1287 |
+
|
| 1288 |
+
Logs a user out on ``POST`` requests, then redirects to the login page.
|
| 1289 |
+
|
| 1290 |
+
**URL name:** No default URL provided
|
| 1291 |
+
|
| 1292 |
+
**Optional arguments:**
|
| 1293 |
+
|
| 1294 |
+
* ``login_url``: The URL of the login page to redirect to.
|
| 1295 |
+
Defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>` if not supplied.
|
| 1296 |
+
|
| 1297 |
+
.. deprecated:: 4.1
|
| 1298 |
+
|
| 1299 |
+
Support for logging out on ``GET`` requests is deprecated and will be
|
| 1300 |
+
removed in Django 5.0.
|
| 1301 |
+
|
| 1302 |
+
.. class:: PasswordChangeView
|
| 1303 |
+
|
| 1304 |
+
**URL name:** ``password_change``
|
| 1305 |
+
|
| 1306 |
+
Allows a user to change their password.
|
| 1307 |
+
|
| 1308 |
+
**Attributes:**
|
| 1309 |
+
|
| 1310 |
+
.. attribute:: template_name
|
| 1311 |
+
|
| 1312 |
+
The full name of a template to use for displaying the password change
|
| 1313 |
+
form. Defaults to :file:`registration/password_change_form.html` if not
|
| 1314 |
+
supplied.
|
| 1315 |
+
|
| 1316 |
+
.. attribute:: success_url
|
| 1317 |
+
|
| 1318 |
+
The URL to redirect to after a successful password change. Defaults to
|
| 1319 |
+
``'password_change_done'``.
|
| 1320 |
+
|
| 1321 |
+
.. attribute:: form_class
|
| 1322 |
+
|
| 1323 |
+
A custom "change password" form which must accept a ``user`` keyword
|
| 1324 |
+
argument. The form is responsible for actually changing the user's
|
| 1325 |
+
password. Defaults to
|
| 1326 |
+
:class:`~django.contrib.auth.forms.PasswordChangeForm`.
|
| 1327 |
+
|
| 1328 |
+
.. attribute:: extra_context
|
| 1329 |
+
|
| 1330 |
+
A dictionary of context data that will be added to the default context
|
| 1331 |
+
data passed to the template.
|
| 1332 |
+
|
| 1333 |
+
**Template context:**
|
| 1334 |
+
|
| 1335 |
+
* ``form``: The password change form (see ``form_class`` above).
|
| 1336 |
+
|
| 1337 |
+
.. class:: PasswordChangeDoneView
|
| 1338 |
+
|
| 1339 |
+
**URL name:** ``password_change_done``
|
| 1340 |
+
|
| 1341 |
+
The page shown after a user has changed their password.
|
| 1342 |
+
|
| 1343 |
+
**Attributes:**
|
| 1344 |
+
|
| 1345 |
+
.. attribute:: template_name
|
| 1346 |
+
|
| 1347 |
+
The full name of a template to use. Defaults to
|
| 1348 |
+
:file:`registration/password_change_done.html` if not supplied.
|
| 1349 |
+
|
| 1350 |
+
.. attribute:: extra_context
|
| 1351 |
+
|
| 1352 |
+
A dictionary of context data that will be added to the default context
|
| 1353 |
+
data passed to the template.
|
| 1354 |
+
|
| 1355 |
+
.. class:: PasswordResetView
|
| 1356 |
+
|
| 1357 |
+
**URL name:** ``password_reset``
|
| 1358 |
+
|
| 1359 |
+
Allows a user to reset their password by generating a one-time use link
|
| 1360 |
+
that can be used to reset the password, and sending that link to the
|
| 1361 |
+
user's registered email address.
|
| 1362 |
+
|
| 1363 |
+
This view will send an email if the following conditions are met:
|
| 1364 |
+
|
| 1365 |
+
* The email address provided exists in the system.
|
| 1366 |
+
* The requested user is active (``User.is_active`` is ``True``).
|
| 1367 |
+
* The requested user has a usable password. Users flagged with an unusable
|
| 1368 |
+
password (see
|
| 1369 |
+
:meth:`~django.contrib.auth.models.User.set_unusable_password`) aren't
|
| 1370 |
+
allowed to request a password reset to prevent misuse when using an
|
| 1371 |
+
external authentication source like LDAP.
|
| 1372 |
+
|
| 1373 |
+
If any of these conditions are *not* met, no email will be sent, but the
|
| 1374 |
+
user won't receive any error message either. This prevents information
|
| 1375 |
+
leaking to potential attackers. If you want to provide an error message in
|
| 1376 |
+
this case, you can subclass
|
| 1377 |
+
:class:`~django.contrib.auth.forms.PasswordResetForm` and use the
|
| 1378 |
+
``form_class`` attribute.
|
| 1379 |
+
|
| 1380 |
+
.. note::
|
| 1381 |
+
|
| 1382 |
+
Be aware that sending an email costs extra time, hence you may be
|
| 1383 |
+
vulnerable to an email address enumeration timing attack due to a
|
| 1384 |
+
difference between the duration of a reset request for an existing
|
| 1385 |
+
email address and the duration of a reset request for a nonexistent
|
| 1386 |
+
email address. To reduce the overhead, you can use a 3rd party package
|
| 1387 |
+
that allows to send emails asynchronously, e.g. :pypi:`django-mailer`.
|
| 1388 |
+
|
| 1389 |
+
**Attributes:**
|
| 1390 |
+
|
| 1391 |
+
.. attribute:: template_name
|
| 1392 |
+
|
| 1393 |
+
The full name of a template to use for displaying the password reset
|
| 1394 |
+
form. Defaults to :file:`registration/password_reset_form.html` if not
|
| 1395 |
+
supplied.
|
| 1396 |
+
|
| 1397 |
+
.. attribute:: form_class
|
| 1398 |
+
|
| 1399 |
+
Form that will be used to get the email of the user to reset the
|
| 1400 |
+
password for. Defaults to
|
| 1401 |
+
:class:`~django.contrib.auth.forms.PasswordResetForm`.
|
| 1402 |
+
|
| 1403 |
+
.. attribute:: email_template_name
|
| 1404 |
+
|
| 1405 |
+
The full name of a template to use for generating the email with the
|
| 1406 |
+
reset password link. Defaults to
|
| 1407 |
+
:file:`registration/password_reset_email.html` if not supplied.
|
| 1408 |
+
|
| 1409 |
+
.. attribute:: subject_template_name
|
| 1410 |
+
|
| 1411 |
+
The full name of a template to use for the subject of the email with
|
| 1412 |
+
the reset password link. Defaults to
|
| 1413 |
+
:file:`registration/password_reset_subject.txt` if not supplied.
|
| 1414 |
+
|
| 1415 |
+
.. attribute:: token_generator
|
| 1416 |
+
|
| 1417 |
+
Instance of the class to check the one time link. This will default to
|
| 1418 |
+
``default_token_generator``, it's an instance of
|
| 1419 |
+
``django.contrib.auth.tokens.PasswordResetTokenGenerator``.
|
| 1420 |
+
|
| 1421 |
+
.. attribute:: success_url
|
| 1422 |
+
|
| 1423 |
+
The URL to redirect to after a successful password reset request.
|
| 1424 |
+
Defaults to ``'password_reset_done'``.
|
| 1425 |
+
|
| 1426 |
+
.. attribute:: from_email
|
| 1427 |
+
|
| 1428 |
+
A valid email address. By default Django uses the
|
| 1429 |
+
:setting:`DEFAULT_FROM_EMAIL`.
|
| 1430 |
+
|
| 1431 |
+
.. attribute:: extra_context
|
| 1432 |
+
|
| 1433 |
+
A dictionary of context data that will be added to the default context
|
| 1434 |
+
data passed to the template.
|
| 1435 |
+
|
| 1436 |
+
.. attribute:: html_email_template_name
|
| 1437 |
+
|
| 1438 |
+
The full name of a template to use for generating a
|
| 1439 |
+
:mimetype:`text/html` multipart email with the password reset link. By
|
| 1440 |
+
default, HTML email is not sent.
|
| 1441 |
+
|
| 1442 |
+
.. attribute:: extra_email_context
|
| 1443 |
+
|
| 1444 |
+
A dictionary of context data that will be available in the email
|
| 1445 |
+
template. It can be used to override default template context values
|
| 1446 |
+
listed below e.g. ``domain``.
|
| 1447 |
+
|
| 1448 |
+
**Template context:**
|
| 1449 |
+
|
| 1450 |
+
* ``form``: The form (see ``form_class`` above) for resetting the user's
|
| 1451 |
+
password.
|
| 1452 |
+
|
| 1453 |
+
**Email template context:**
|
| 1454 |
+
|
| 1455 |
+
* ``email``: An alias for ``user.email``
|
| 1456 |
+
|
| 1457 |
+
* ``user``: The current :class:`~django.contrib.auth.models.User`,
|
| 1458 |
+
according to the ``email`` form field. Only active users are able to
|
| 1459 |
+
reset their passwords (``User.is_active is True``).
|
| 1460 |
+
|
| 1461 |
+
* ``site_name``: An alias for ``site.name``. If you don't have the site
|
| 1462 |
+
framework installed, this will be set to the value of
|
| 1463 |
+
:attr:`request.META['SERVER_NAME'] <django.http.HttpRequest.META>`.
|
| 1464 |
+
For more on sites, see :doc:`/ref/contrib/sites`.
|
| 1465 |
+
|
| 1466 |
+
* ``domain``: An alias for ``site.domain``. If you don't have the site
|
| 1467 |
+
framework installed, this will be set to the value of
|
| 1468 |
+
``request.get_host()``.
|
| 1469 |
+
|
| 1470 |
+
* ``protocol``: http or https
|
| 1471 |
+
|
| 1472 |
+
* ``uid``: The user's primary key encoded in base 64.
|
| 1473 |
+
|
| 1474 |
+
* ``token``: Token to check that the reset link is valid.
|
| 1475 |
+
|
| 1476 |
+
Sample ``registration/password_reset_email.html`` (email body template):
|
| 1477 |
+
|
| 1478 |
+
.. code-block:: html+django
|
| 1479 |
+
|
| 1480 |
+
Someone asked for password reset for email {{ email }}. Follow the link below:
|
| 1481 |
+
{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
|
| 1482 |
+
|
| 1483 |
+
The same template context is used for subject template. Subject must be
|
| 1484 |
+
single line plain text string.
|
| 1485 |
+
|
| 1486 |
+
.. class:: PasswordResetDoneView
|
| 1487 |
+
|
| 1488 |
+
**URL name:** ``password_reset_done``
|
| 1489 |
+
|
| 1490 |
+
The page shown after a user has been emailed a link to reset their
|
| 1491 |
+
password. This view is called by default if the :class:`PasswordResetView`
|
| 1492 |
+
doesn't have an explicit ``success_url`` URL set.
|
| 1493 |
+
|
| 1494 |
+
.. note::
|
| 1495 |
+
|
| 1496 |
+
If the email address provided does not exist in the system, the user is
|
| 1497 |
+
inactive, or has an unusable password, the user will still be
|
| 1498 |
+
redirected to this view but no email will be sent.
|
| 1499 |
+
|
| 1500 |
+
**Attributes:**
|
| 1501 |
+
|
| 1502 |
+
.. attribute:: template_name
|
| 1503 |
+
|
| 1504 |
+
The full name of a template to use. Defaults to
|
| 1505 |
+
:file:`registration/password_reset_done.html` if not supplied.
|
| 1506 |
+
|
| 1507 |
+
.. attribute:: extra_context
|
| 1508 |
+
|
| 1509 |
+
A dictionary of context data that will be added to the default context
|
| 1510 |
+
data passed to the template.
|
| 1511 |
+
|
| 1512 |
+
.. class:: PasswordResetConfirmView
|
| 1513 |
+
|
| 1514 |
+
**URL name:** ``password_reset_confirm``
|
| 1515 |
+
|
| 1516 |
+
Presents a form for entering a new password.
|
| 1517 |
+
|
| 1518 |
+
**Keyword arguments from the URL:**
|
| 1519 |
+
|
| 1520 |
+
* ``uidb64``: The user's id encoded in base 64.
|
| 1521 |
+
|
| 1522 |
+
* ``token``: Token to check that the password is valid.
|
| 1523 |
+
|
| 1524 |
+
**Attributes:**
|
| 1525 |
+
|
| 1526 |
+
.. attribute:: template_name
|
| 1527 |
+
|
| 1528 |
+
The full name of a template to display the confirm password view.
|
| 1529 |
+
Default value is :file:`registration/password_reset_confirm.html`.
|
| 1530 |
+
|
| 1531 |
+
.. attribute:: token_generator
|
| 1532 |
+
|
| 1533 |
+
Instance of the class to check the password. This will default to
|
| 1534 |
+
``default_token_generator``, it's an instance of
|
| 1535 |
+
``django.contrib.auth.tokens.PasswordResetTokenGenerator``.
|
| 1536 |
+
|
| 1537 |
+
.. attribute:: post_reset_login
|
| 1538 |
+
|
| 1539 |
+
A boolean indicating if the user should be automatically authenticated
|
| 1540 |
+
after a successful password reset. Defaults to ``False``.
|
| 1541 |
+
|
| 1542 |
+
.. attribute:: post_reset_login_backend
|
| 1543 |
+
|
| 1544 |
+
A dotted path to the authentication backend to use when authenticating
|
| 1545 |
+
a user if ``post_reset_login`` is ``True``. Required only if you have
|
| 1546 |
+
multiple :setting:`AUTHENTICATION_BACKENDS` configured. Defaults to
|
| 1547 |
+
``None``.
|
| 1548 |
+
|
| 1549 |
+
.. attribute:: form_class
|
| 1550 |
+
|
| 1551 |
+
Form that will be used to set the password. Defaults to
|
| 1552 |
+
:class:`~django.contrib.auth.forms.SetPasswordForm`.
|
| 1553 |
+
|
| 1554 |
+
.. attribute:: success_url
|
| 1555 |
+
|
| 1556 |
+
URL to redirect after the password reset done. Defaults to
|
| 1557 |
+
``'password_reset_complete'``.
|
| 1558 |
+
|
| 1559 |
+
.. attribute:: extra_context
|
| 1560 |
+
|
| 1561 |
+
A dictionary of context data that will be added to the default context
|
| 1562 |
+
data passed to the template.
|
| 1563 |
+
|
| 1564 |
+
.. attribute:: reset_url_token
|
| 1565 |
+
|
| 1566 |
+
Token parameter displayed as a component of password reset URLs.
|
| 1567 |
+
Defaults to ``'set-password'``.
|
| 1568 |
+
|
| 1569 |
+
**Template context:**
|
| 1570 |
+
|
| 1571 |
+
* ``form``: The form (see ``form_class`` above) for setting the new user's
|
| 1572 |
+
password.
|
| 1573 |
+
|
| 1574 |
+
* ``validlink``: Boolean, True if the link (combination of ``uidb64`` and
|
| 1575 |
+
``token``) is valid or unused yet.
|
| 1576 |
+
|
| 1577 |
+
.. class:: PasswordResetCompleteView
|
| 1578 |
+
|
| 1579 |
+
**URL name:** ``password_reset_complete``
|
| 1580 |
+
|
| 1581 |
+
Presents a view which informs the user that the password has been
|
| 1582 |
+
successfully changed.
|
| 1583 |
+
|
| 1584 |
+
**Attributes:**
|
| 1585 |
+
|
| 1586 |
+
.. attribute:: template_name
|
| 1587 |
+
|
| 1588 |
+
The full name of a template to display the view. Defaults to
|
| 1589 |
+
:file:`registration/password_reset_complete.html`.
|
| 1590 |
+
|
| 1591 |
+
.. attribute:: extra_context
|
| 1592 |
+
|
| 1593 |
+
A dictionary of context data that will be added to the default context
|
| 1594 |
+
data passed to the template.
|
| 1595 |
+
|
| 1596 |
+
Helper functions
|
| 1597 |
+
----------------
|
| 1598 |
+
|
| 1599 |
+
.. currentmodule:: django.contrib.auth.views
|
| 1600 |
+
|
| 1601 |
+
.. function:: redirect_to_login(next, login_url=None, redirect_field_name='next')
|
| 1602 |
+
|
| 1603 |
+
Redirects to the login page, and then back to another URL after a
|
| 1604 |
+
successful login.
|
| 1605 |
+
|
| 1606 |
+
**Required arguments:**
|
| 1607 |
+
|
| 1608 |
+
* ``next``: The URL to redirect to after a successful login.
|
| 1609 |
+
|
| 1610 |
+
**Optional arguments:**
|
| 1611 |
+
|
| 1612 |
+
* ``login_url``: The URL of the login page to redirect to.
|
| 1613 |
+
Defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>` if not supplied.
|
| 1614 |
+
|
| 1615 |
+
* ``redirect_field_name``: The name of a ``GET`` field containing the
|
| 1616 |
+
URL to redirect to after log out. Overrides ``next`` if the given
|
| 1617 |
+
``GET`` parameter is passed.
|
| 1618 |
+
|
| 1619 |
+
.. _built-in-auth-forms:
|
| 1620 |
+
|
| 1621 |
+
Built-in forms
|
| 1622 |
+
--------------
|
| 1623 |
+
|
| 1624 |
+
.. module:: django.contrib.auth.forms
|
| 1625 |
+
|
| 1626 |
+
If you don't want to use the built-in views, but want the convenience of not
|
| 1627 |
+
having to write forms for this functionality, the authentication system
|
| 1628 |
+
provides several built-in forms located in :mod:`django.contrib.auth.forms`:
|
| 1629 |
+
|
| 1630 |
+
.. note::
|
| 1631 |
+
The built-in authentication forms make certain assumptions about the user
|
| 1632 |
+
model that they are working with. If you're using a :ref:`custom user model
|
| 1633 |
+
<auth-custom-user>`, it may be necessary to define your own forms for the
|
| 1634 |
+
authentication system. For more information, refer to the documentation
|
| 1635 |
+
about :ref:`using the built-in authentication forms with custom user models
|
| 1636 |
+
<custom-users-and-the-built-in-auth-forms>`.
|
| 1637 |
+
|
| 1638 |
+
.. class:: AdminPasswordChangeForm
|
| 1639 |
+
|
| 1640 |
+
A form used in the admin interface to change a user's password.
|
| 1641 |
+
|
| 1642 |
+
Takes the ``user`` as the first positional argument.
|
| 1643 |
+
|
| 1644 |
+
.. class:: AuthenticationForm
|
| 1645 |
+
|
| 1646 |
+
A form for logging a user in.
|
| 1647 |
+
|
| 1648 |
+
Takes ``request`` as its first positional argument, which is stored on the
|
| 1649 |
+
form instance for use by sub-classes.
|
| 1650 |
+
|
| 1651 |
+
.. method:: confirm_login_allowed(user)
|
| 1652 |
+
|
| 1653 |
+
By default, ``AuthenticationForm`` rejects users whose ``is_active``
|
| 1654 |
+
flag is set to ``False``. You may override this behavior with a custom
|
| 1655 |
+
policy to determine which users can log in. Do this with a custom form
|
| 1656 |
+
that subclasses ``AuthenticationForm`` and overrides the
|
| 1657 |
+
``confirm_login_allowed()`` method. This method should raise a
|
| 1658 |
+
:exc:`~django.core.exceptions.ValidationError` if the given user may
|
| 1659 |
+
not log in.
|
| 1660 |
+
|
| 1661 |
+
For example, to allow all users to log in regardless of "active"
|
| 1662 |
+
status::
|
| 1663 |
+
|
| 1664 |
+
from django.contrib.auth.forms import AuthenticationForm
|
| 1665 |
+
|
| 1666 |
+
|
| 1667 |
+
class AuthenticationFormWithInactiveUsersOkay(AuthenticationForm):
|
| 1668 |
+
def confirm_login_allowed(self, user):
|
| 1669 |
+
pass
|
| 1670 |
+
|
| 1671 |
+
(In this case, you'll also need to use an authentication backend that
|
| 1672 |
+
allows inactive users, such as
|
| 1673 |
+
:class:`~django.contrib.auth.backends.AllowAllUsersModelBackend`.)
|
| 1674 |
+
|
| 1675 |
+
Or to allow only some active users to log in::
|
| 1676 |
+
|
| 1677 |
+
class PickyAuthenticationForm(AuthenticationForm):
|
| 1678 |
+
def confirm_login_allowed(self, user):
|
| 1679 |
+
if not user.is_active:
|
| 1680 |
+
raise ValidationError(
|
| 1681 |
+
_("This account is inactive."),
|
| 1682 |
+
code="inactive",
|
| 1683 |
+
)
|
| 1684 |
+
if user.username.startswith("b"):
|
| 1685 |
+
raise ValidationError(
|
| 1686 |
+
_("Sorry, accounts starting with 'b' aren't welcome here."),
|
| 1687 |
+
code="no_b_users",
|
| 1688 |
+
)
|
| 1689 |
+
|
| 1690 |
+
.. class:: PasswordChangeForm
|
| 1691 |
+
|
| 1692 |
+
A form for allowing a user to change their password.
|
| 1693 |
+
|
| 1694 |
+
.. class:: PasswordResetForm
|
| 1695 |
+
|
| 1696 |
+
A form for generating and emailing a one-time use link to reset a
|
| 1697 |
+
user's password.
|
| 1698 |
+
|
| 1699 |
+
.. method:: send_mail(subject_template_name, email_template_name, context, from_email, to_email, html_email_template_name=None)
|
| 1700 |
+
|
| 1701 |
+
Uses the arguments to send an ``EmailMultiAlternatives``.
|
| 1702 |
+
Can be overridden to customize how the email is sent to the user.
|
| 1703 |
+
|
| 1704 |
+
:param subject_template_name: the template for the subject.
|
| 1705 |
+
:param email_template_name: the template for the email body.
|
| 1706 |
+
:param context: context passed to the ``subject_template``,
|
| 1707 |
+
``email_template``, and ``html_email_template`` (if it is not
|
| 1708 |
+
``None``).
|
| 1709 |
+
:param from_email: the sender's email.
|
| 1710 |
+
:param to_email: the email of the requester.
|
| 1711 |
+
:param html_email_template_name: the template for the HTML body;
|
| 1712 |
+
defaults to ``None``, in which case a plain text email is sent.
|
| 1713 |
+
|
| 1714 |
+
By default, ``save()`` populates the ``context`` with the
|
| 1715 |
+
same variables that
|
| 1716 |
+
:class:`~django.contrib.auth.views.PasswordResetView` passes to its
|
| 1717 |
+
email context.
|
| 1718 |
+
|
| 1719 |
+
.. class:: SetPasswordForm
|
| 1720 |
+
|
| 1721 |
+
A form that lets a user change their password without entering the old
|
| 1722 |
+
password.
|
| 1723 |
+
|
| 1724 |
+
.. class:: UserChangeForm
|
| 1725 |
+
|
| 1726 |
+
A form used in the admin interface to change a user's information and
|
| 1727 |
+
permissions.
|
| 1728 |
+
|
| 1729 |
+
.. class:: BaseUserCreationForm
|
| 1730 |
+
|
| 1731 |
+
.. versionadded:: 4.2
|
| 1732 |
+
|
| 1733 |
+
A :class:`~django.forms.ModelForm` for creating a new user. This is the
|
| 1734 |
+
recommended base class if you need to customize the user creation form.
|
| 1735 |
+
|
| 1736 |
+
It has three fields: ``username`` (from the user model), ``password1``,
|
| 1737 |
+
and ``password2``. It verifies that ``password1`` and ``password2`` match,
|
| 1738 |
+
validates the password using
|
| 1739 |
+
:func:`~django.contrib.auth.password_validation.validate_password`, and
|
| 1740 |
+
sets the user's password using
|
| 1741 |
+
:meth:`~django.contrib.auth.models.User.set_password()`.
|
| 1742 |
+
|
| 1743 |
+
.. class:: UserCreationForm
|
| 1744 |
+
|
| 1745 |
+
Inherits from :class:`BaseUserCreationForm`. To help prevent confusion with
|
| 1746 |
+
similar usernames, the form doesn't allow usernames that differ only in
|
| 1747 |
+
case.
|
| 1748 |
+
|
| 1749 |
+
.. versionchanged:: 4.2
|
| 1750 |
+
|
| 1751 |
+
In older versions, :class:`UserCreationForm` didn't save many-to-many
|
| 1752 |
+
form fields for a custom user model.
|
| 1753 |
+
|
| 1754 |
+
In older versions, usernames that differ only in case are allowed.
|
| 1755 |
+
|
| 1756 |
+
.. currentmodule:: django.contrib.auth
|
| 1757 |
+
|
| 1758 |
+
Authentication data in templates
|
| 1759 |
+
--------------------------------
|
| 1760 |
+
|
| 1761 |
+
The currently logged-in user and their permissions are made available in the
|
| 1762 |
+
:doc:`template context </ref/templates/api>` when you use
|
| 1763 |
+
:class:`~django.template.RequestContext`.
|
| 1764 |
+
|
| 1765 |
+
.. admonition:: Technicality
|
| 1766 |
+
|
| 1767 |
+
Technically, these variables are only made available in the template
|
| 1768 |
+
context if you use :class:`~django.template.RequestContext` and the
|
| 1769 |
+
``'django.contrib.auth.context_processors.auth'`` context processor is
|
| 1770 |
+
enabled. It is in the default generated settings file. For more, see the
|
| 1771 |
+
:ref:`RequestContext docs <subclassing-context-requestcontext>`.
|
| 1772 |
+
|
| 1773 |
+
Users
|
| 1774 |
+
~~~~~
|
| 1775 |
+
|
| 1776 |
+
When rendering a template :class:`~django.template.RequestContext`, the
|
| 1777 |
+
currently logged-in user, either a :class:`~django.contrib.auth.models.User`
|
| 1778 |
+
instance or an :class:`~django.contrib.auth.models.AnonymousUser` instance, is
|
| 1779 |
+
stored in the template variable ``{{ user }}``:
|
| 1780 |
+
|
| 1781 |
+
.. code-block:: html+django
|
| 1782 |
+
|
| 1783 |
+
{% if user.is_authenticated %}
|
| 1784 |
+
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
|
| 1785 |
+
{% else %}
|
| 1786 |
+
<p>Welcome, new user. Please log in.</p>
|
| 1787 |
+
{% endif %}
|
| 1788 |
+
|
| 1789 |
+
This template context variable is not available if a ``RequestContext`` is not
|
| 1790 |
+
being used.
|
| 1791 |
+
|
| 1792 |
+
Permissions
|
| 1793 |
+
~~~~~~~~~~~
|
| 1794 |
+
|
| 1795 |
+
The currently logged-in user's permissions are stored in the template variable
|
| 1796 |
+
``{{ perms }}``. This is an instance of
|
| 1797 |
+
``django.contrib.auth.context_processors.PermWrapper``, which is a
|
| 1798 |
+
template-friendly proxy of permissions.
|
| 1799 |
+
|
| 1800 |
+
Evaluating a single-attribute lookup of ``{{ perms }}`` as a boolean is a proxy
|
| 1801 |
+
to :meth:`User.has_module_perms()
|
| 1802 |
+
<django.contrib.auth.models.User.has_module_perms>`. For example, to check if
|
| 1803 |
+
the logged-in user has any permissions in the ``foo`` app:
|
| 1804 |
+
|
| 1805 |
+
.. code-block:: html+django
|
| 1806 |
+
|
| 1807 |
+
{% if perms.foo %}
|
| 1808 |
+
|
| 1809 |
+
Evaluating a two-level-attribute lookup as a boolean is a proxy to
|
| 1810 |
+
:meth:`User.has_perm() <django.contrib.auth.models.User.has_perm>`. For example,
|
| 1811 |
+
to check if the logged-in user has the permission ``foo.add_vote``:
|
| 1812 |
+
|
| 1813 |
+
.. code-block:: html+django
|
| 1814 |
+
|
| 1815 |
+
{% if perms.foo.add_vote %}
|
| 1816 |
+
|
| 1817 |
+
Here's a more complete example of checking permissions in a template:
|
| 1818 |
+
|
| 1819 |
+
.. code-block:: html+django
|
| 1820 |
+
|
| 1821 |
+
{% if perms.foo %}
|
| 1822 |
+
<p>You have permission to do something in the foo app.</p>
|
| 1823 |
+
{% if perms.foo.add_vote %}
|
| 1824 |
+
<p>You can vote!</p>
|
| 1825 |
+
{% endif %}
|
| 1826 |
+
{% if perms.foo.add_driving %}
|
| 1827 |
+
<p>You can drive!</p>
|
| 1828 |
+
{% endif %}
|
| 1829 |
+
{% else %}
|
| 1830 |
+
<p>You don't have permission to do anything in the foo app.</p>
|
| 1831 |
+
{% endif %}
|
| 1832 |
+
|
| 1833 |
+
It is possible to also look permissions up by ``{% if in %}`` statements.
|
| 1834 |
+
For example:
|
| 1835 |
+
|
| 1836 |
+
.. code-block:: html+django
|
| 1837 |
+
|
| 1838 |
+
{% if 'foo' in perms %}
|
| 1839 |
+
{% if 'foo.add_vote' in perms %}
|
| 1840 |
+
<p>In lookup works, too.</p>
|
| 1841 |
+
{% endif %}
|
| 1842 |
+
{% endif %}
|
| 1843 |
+
|
| 1844 |
+
.. _auth-admin:
|
| 1845 |
+
|
| 1846 |
+
Managing users in the admin
|
| 1847 |
+
===========================
|
| 1848 |
+
|
| 1849 |
+
When you have both ``django.contrib.admin`` and ``django.contrib.auth``
|
| 1850 |
+
installed, the admin provides a convenient way to view and manage users,
|
| 1851 |
+
groups, and permissions. Users can be created and deleted like any Django
|
| 1852 |
+
model. Groups can be created, and permissions can be assigned to users or
|
| 1853 |
+
groups. A log of user edits to models made within the admin is also stored and
|
| 1854 |
+
displayed.
|
| 1855 |
+
|
| 1856 |
+
Creating users
|
| 1857 |
+
--------------
|
| 1858 |
+
|
| 1859 |
+
You should see a link to "Users" in the "Auth"
|
| 1860 |
+
section of the main admin index page. The "Add user" admin page is different
|
| 1861 |
+
than standard admin pages in that it requires you to choose a username and
|
| 1862 |
+
password before allowing you to edit the rest of the user's fields.
|
| 1863 |
+
|
| 1864 |
+
Also note: if you want a user account to be able to create users using the
|
| 1865 |
+
Django admin site, you'll need to give them permission to add users *and*
|
| 1866 |
+
change users (i.e., the "Add user" and "Change user" permissions). If an
|
| 1867 |
+
account has permission to add users but not to change them, that account won't
|
| 1868 |
+
be able to add users. Why? Because if you have permission to add users, you
|
| 1869 |
+
have the power to create superusers, which can then, in turn, change other
|
| 1870 |
+
users. So Django requires add *and* change permissions as a slight security
|
| 1871 |
+
measure.
|
| 1872 |
+
|
| 1873 |
+
Be thoughtful about how you allow users to manage permissions. If you give a
|
| 1874 |
+
non-superuser the ability to edit users, this is ultimately the same as giving
|
| 1875 |
+
them superuser status because they will be able to elevate permissions of
|
| 1876 |
+
users including themselves!
|
| 1877 |
+
|
| 1878 |
+
Changing passwords
|
| 1879 |
+
------------------
|
| 1880 |
+
|
| 1881 |
+
User passwords are not displayed in the admin (nor stored in the database), but
|
| 1882 |
+
the :doc:`password storage details </topics/auth/passwords>` are displayed.
|
| 1883 |
+
Included in the display of this information is a link to
|
| 1884 |
+
a password change form that allows admins to change user passwords.
|
testbed/django__django/docs/topics/auth/index.txt
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
=============================
|
| 2 |
+
User authentication in Django
|
| 3 |
+
=============================
|
| 4 |
+
|
| 5 |
+
.. toctree::
|
| 6 |
+
:hidden:
|
| 7 |
+
|
| 8 |
+
default
|
| 9 |
+
passwords
|
| 10 |
+
customizing
|
| 11 |
+
|
| 12 |
+
.. module:: django.contrib.auth
|
| 13 |
+
:synopsis: Django's authentication framework.
|
| 14 |
+
|
| 15 |
+
Django comes with a user authentication system. It handles user accounts,
|
| 16 |
+
groups, permissions and cookie-based user sessions. This section of the
|
| 17 |
+
documentation explains how the default implementation works out of the box, as
|
| 18 |
+
well as how to :doc:`extend and customize </topics/auth/customizing>` it to
|
| 19 |
+
suit your project's needs.
|
| 20 |
+
|
| 21 |
+
Overview
|
| 22 |
+
========
|
| 23 |
+
|
| 24 |
+
The Django authentication system handles both authentication and authorization.
|
| 25 |
+
Briefly, authentication verifies a user is who they claim to be, and
|
| 26 |
+
authorization determines what an authenticated user is allowed to do. Here the
|
| 27 |
+
term authentication is used to refer to both tasks.
|
| 28 |
+
|
| 29 |
+
The auth system consists of:
|
| 30 |
+
|
| 31 |
+
* Users
|
| 32 |
+
* Permissions: Binary (yes/no) flags designating whether a user may perform
|
| 33 |
+
a certain task.
|
| 34 |
+
* Groups: A generic way of applying labels and permissions to more than one
|
| 35 |
+
user.
|
| 36 |
+
* A configurable password hashing system
|
| 37 |
+
* Forms and view tools for logging in users, or restricting content
|
| 38 |
+
* A pluggable backend system
|
| 39 |
+
|
| 40 |
+
The authentication system in Django aims to be very generic and doesn't provide
|
| 41 |
+
some features commonly found in web authentication systems. Solutions for some
|
| 42 |
+
of these common problems have been implemented in third-party packages:
|
| 43 |
+
|
| 44 |
+
* Password strength checking
|
| 45 |
+
* Throttling of login attempts
|
| 46 |
+
* Authentication against third-parties (OAuth, for example)
|
| 47 |
+
* Object-level permissions
|
| 48 |
+
|
| 49 |
+
Installation
|
| 50 |
+
============
|
| 51 |
+
|
| 52 |
+
Authentication support is bundled as a Django contrib module in
|
| 53 |
+
``django.contrib.auth``. By default, the required configuration is already
|
| 54 |
+
included in the :file:`settings.py` generated by :djadmin:`django-admin
|
| 55 |
+
startproject <startproject>`, these consist of two items listed in your
|
| 56 |
+
:setting:`INSTALLED_APPS` setting:
|
| 57 |
+
|
| 58 |
+
1. ``'django.contrib.auth'`` contains the core of the authentication framework,
|
| 59 |
+
and its default models.
|
| 60 |
+
2. ``'django.contrib.contenttypes'`` is the Django :doc:`content type system
|
| 61 |
+
</ref/contrib/contenttypes>`, which allows permissions to be associated with
|
| 62 |
+
models you create.
|
| 63 |
+
|
| 64 |
+
and these items in your :setting:`MIDDLEWARE` setting:
|
| 65 |
+
|
| 66 |
+
#. :class:`~django.contrib.sessions.middleware.SessionMiddleware` manages
|
| 67 |
+
:doc:`sessions </topics/http/sessions>` across requests.
|
| 68 |
+
#. :class:`~django.contrib.auth.middleware.AuthenticationMiddleware` associates
|
| 69 |
+
users with requests using sessions.
|
| 70 |
+
|
| 71 |
+
With these settings in place, running the command ``manage.py migrate`` creates
|
| 72 |
+
the necessary database tables for auth related models and permissions for any
|
| 73 |
+
models defined in your installed apps.
|
| 74 |
+
|
| 75 |
+
Usage
|
| 76 |
+
=====
|
| 77 |
+
|
| 78 |
+
:doc:`Using Django's default implementation <default>`
|
| 79 |
+
|
| 80 |
+
* :ref:`Working with User objects <user-objects>`
|
| 81 |
+
* :ref:`Permissions and authorization <topic-authorization>`
|
| 82 |
+
* :ref:`Authentication in web requests <auth-web-requests>`
|
| 83 |
+
* :ref:`Managing users in the admin <auth-admin>`
|
| 84 |
+
|
| 85 |
+
:doc:`API reference for the default implementation </ref/contrib/auth>`
|
| 86 |
+
|
| 87 |
+
:doc:`Customizing Users and authentication <customizing>`
|
| 88 |
+
|
| 89 |
+
:doc:`Password management in Django <passwords>`
|
testbed/django__django/docs/topics/auth/passwords.txt
ADDED
|
@@ -0,0 +1,755 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
=============================
|
| 2 |
+
Password management in Django
|
| 3 |
+
=============================
|
| 4 |
+
|
| 5 |
+
Password management is something that should generally not be reinvented
|
| 6 |
+
unnecessarily, and Django endeavors to provide a secure and flexible set of
|
| 7 |
+
tools for managing user passwords. This document describes how Django stores
|
| 8 |
+
passwords, how the storage hashing can be configured, and some utilities to
|
| 9 |
+
work with hashed passwords.
|
| 10 |
+
|
| 11 |
+
.. seealso::
|
| 12 |
+
|
| 13 |
+
Even though users may use strong passwords, attackers might be able to
|
| 14 |
+
eavesdrop on their connections. Use :ref:`HTTPS
|
| 15 |
+
<security-recommendation-ssl>` to avoid sending passwords (or any other
|
| 16 |
+
sensitive data) over plain HTTP connections because they will be vulnerable
|
| 17 |
+
to password sniffing.
|
| 18 |
+
|
| 19 |
+
.. _auth_password_storage:
|
| 20 |
+
|
| 21 |
+
How Django stores passwords
|
| 22 |
+
===========================
|
| 23 |
+
|
| 24 |
+
Django provides a flexible password storage system and uses PBKDF2 by default.
|
| 25 |
+
|
| 26 |
+
The :attr:`~django.contrib.auth.models.User.password` attribute of a
|
| 27 |
+
:class:`~django.contrib.auth.models.User` object is a string in this format:
|
| 28 |
+
|
| 29 |
+
.. code-block:: text
|
| 30 |
+
|
| 31 |
+
<algorithm>$<iterations>$<salt>$<hash>
|
| 32 |
+
|
| 33 |
+
Those are the components used for storing a User's password, separated by the
|
| 34 |
+
dollar-sign character and consist of: the hashing algorithm, the number of
|
| 35 |
+
algorithm iterations (work factor), the random salt, and the resulting password
|
| 36 |
+
hash. The algorithm is one of a number of one-way hashing or password storage
|
| 37 |
+
algorithms Django can use; see below. Iterations describe the number of times
|
| 38 |
+
the algorithm is run over the hash. Salt is the random seed used and the hash
|
| 39 |
+
is the result of the one-way function.
|
| 40 |
+
|
| 41 |
+
By default, Django uses the PBKDF2_ algorithm with a SHA256 hash, a
|
| 42 |
+
password stretching mechanism recommended by NIST_. This should be
|
| 43 |
+
sufficient for most users: it's quite secure, requiring massive
|
| 44 |
+
amounts of computing time to break.
|
| 45 |
+
|
| 46 |
+
However, depending on your requirements, you may choose a different
|
| 47 |
+
algorithm, or even use a custom algorithm to match your specific
|
| 48 |
+
security situation. Again, most users shouldn't need to do this -- if
|
| 49 |
+
you're not sure, you probably don't. If you do, please read on:
|
| 50 |
+
|
| 51 |
+
Django chooses the algorithm to use by consulting the
|
| 52 |
+
:setting:`PASSWORD_HASHERS` setting. This is a list of hashing algorithm
|
| 53 |
+
classes that this Django installation supports.
|
| 54 |
+
|
| 55 |
+
For storing passwords, Django will use the first hasher in
|
| 56 |
+
:setting:`PASSWORD_HASHERS`. To store new passwords with a different algorithm,
|
| 57 |
+
put your preferred algorithm first in :setting:`PASSWORD_HASHERS`.
|
| 58 |
+
|
| 59 |
+
For verifying passwords, Django will find the hasher in the list that matches
|
| 60 |
+
the algorithm name in the stored password. If a stored password names an
|
| 61 |
+
algorithm not found in :setting:`PASSWORD_HASHERS`, trying to verify it will
|
| 62 |
+
raise ``ValueError``.
|
| 63 |
+
|
| 64 |
+
The default for :setting:`PASSWORD_HASHERS` is::
|
| 65 |
+
|
| 66 |
+
PASSWORD_HASHERS = [
|
| 67 |
+
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
| 68 |
+
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
|
| 69 |
+
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
| 70 |
+
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
|
| 71 |
+
"django.contrib.auth.hashers.ScryptPasswordHasher",
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
This means that Django will use PBKDF2_ to store all passwords but will support
|
| 75 |
+
checking passwords stored with PBKDF2SHA1, argon2_, and bcrypt_.
|
| 76 |
+
|
| 77 |
+
The next few sections describe a couple of common ways advanced users may want
|
| 78 |
+
to modify this setting.
|
| 79 |
+
|
| 80 |
+
.. _argon2_usage:
|
| 81 |
+
|
| 82 |
+
Using Argon2 with Django
|
| 83 |
+
------------------------
|
| 84 |
+
|
| 85 |
+
Argon2_ is the winner of the 2015 `Password Hashing Competition`_, a community
|
| 86 |
+
organized open competition to select a next generation hashing algorithm. It's
|
| 87 |
+
designed not to be easier to compute on custom hardware than it is to compute
|
| 88 |
+
on an ordinary CPU. The default variant for the Argon2 password hasher is
|
| 89 |
+
Argon2id.
|
| 90 |
+
|
| 91 |
+
Argon2_ is not the default for Django because it requires a third-party
|
| 92 |
+
library. The Password Hashing Competition panel, however, recommends immediate
|
| 93 |
+
use of Argon2 rather than the other algorithms supported by Django.
|
| 94 |
+
|
| 95 |
+
To use Argon2id as your default storage algorithm, do the following:
|
| 96 |
+
|
| 97 |
+
#. Install the :pypi:`argon2-cffi` package. This can be done by running
|
| 98 |
+
``python -m pip install django[argon2]``, which is equivalent to
|
| 99 |
+
``python -m pip install argon2-cffi`` (along with any version requirement
|
| 100 |
+
from Django's ``setup.cfg``).
|
| 101 |
+
|
| 102 |
+
#. Modify :setting:`PASSWORD_HASHERS` to list ``Argon2PasswordHasher`` first.
|
| 103 |
+
That is, in your settings file, you'd put::
|
| 104 |
+
|
| 105 |
+
PASSWORD_HASHERS = [
|
| 106 |
+
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
| 107 |
+
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
| 108 |
+
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
|
| 109 |
+
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
|
| 110 |
+
"django.contrib.auth.hashers.ScryptPasswordHasher",
|
| 111 |
+
]
|
| 112 |
+
|
| 113 |
+
Keep and/or add any entries in this list if you need Django to :ref:`upgrade
|
| 114 |
+
passwords <password-upgrades>`.
|
| 115 |
+
|
| 116 |
+
.. _bcrypt_usage:
|
| 117 |
+
|
| 118 |
+
Using ``bcrypt`` with Django
|
| 119 |
+
----------------------------
|
| 120 |
+
|
| 121 |
+
Bcrypt_ is a popular password storage algorithm that's specifically designed
|
| 122 |
+
for long-term password storage. It's not the default used by Django since it
|
| 123 |
+
requires the use of third-party libraries, but since many people may want to
|
| 124 |
+
use it Django supports bcrypt with minimal effort.
|
| 125 |
+
|
| 126 |
+
To use Bcrypt as your default storage algorithm, do the following:
|
| 127 |
+
|
| 128 |
+
#. Install the :pypi:`bcrypt` package. This can be done by running
|
| 129 |
+
``python -m pip install django[bcrypt]``, which is equivalent to
|
| 130 |
+
``python -m pip install bcrypt`` (along with any version requirement from
|
| 131 |
+
Django's ``setup.cfg``).
|
| 132 |
+
|
| 133 |
+
#. Modify :setting:`PASSWORD_HASHERS` to list ``BCryptSHA256PasswordHasher``
|
| 134 |
+
first. That is, in your settings file, you'd put::
|
| 135 |
+
|
| 136 |
+
PASSWORD_HASHERS = [
|
| 137 |
+
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
|
| 138 |
+
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
| 139 |
+
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
|
| 140 |
+
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
| 141 |
+
"django.contrib.auth.hashers.ScryptPasswordHasher",
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
Keep and/or add any entries in this list if you need Django to :ref:`upgrade
|
| 145 |
+
passwords <password-upgrades>`.
|
| 146 |
+
|
| 147 |
+
That's it -- now your Django install will use Bcrypt as the default storage
|
| 148 |
+
algorithm.
|
| 149 |
+
|
| 150 |
+
.. _scrypt-usage:
|
| 151 |
+
|
| 152 |
+
Using ``scrypt`` with Django
|
| 153 |
+
----------------------------
|
| 154 |
+
|
| 155 |
+
scrypt_ is similar to PBKDF2 and bcrypt in utilizing a set number of iterations
|
| 156 |
+
to slow down brute-force attacks. However, because PBKDF2 and bcrypt do not
|
| 157 |
+
require a lot of memory, attackers with sufficient resources can launch
|
| 158 |
+
large-scale parallel attacks in order to speed up the attacking process.
|
| 159 |
+
scrypt_ is specifically designed to use more memory compared to other
|
| 160 |
+
password-based key derivation functions in order to limit the amount of
|
| 161 |
+
parallelism an attacker can use, see :rfc:`7914` for more details.
|
| 162 |
+
|
| 163 |
+
To use scrypt_ as your default storage algorithm, do the following:
|
| 164 |
+
|
| 165 |
+
#. Modify :setting:`PASSWORD_HASHERS` to list ``ScryptPasswordHasher`` first.
|
| 166 |
+
That is, in your settings file::
|
| 167 |
+
|
| 168 |
+
PASSWORD_HASHERS = [
|
| 169 |
+
"django.contrib.auth.hashers.ScryptPasswordHasher",
|
| 170 |
+
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
| 171 |
+
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
|
| 172 |
+
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
| 173 |
+
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
|
| 174 |
+
]
|
| 175 |
+
|
| 176 |
+
Keep and/or add any entries in this list if you need Django to :ref:`upgrade
|
| 177 |
+
passwords <password-upgrades>`.
|
| 178 |
+
|
| 179 |
+
.. note::
|
| 180 |
+
|
| 181 |
+
``scrypt`` requires OpenSSL 1.1+.
|
| 182 |
+
|
| 183 |
+
Increasing the salt entropy
|
| 184 |
+
---------------------------
|
| 185 |
+
|
| 186 |
+
Most password hashes include a salt along with their password hash in order to
|
| 187 |
+
protect against rainbow table attacks. The salt itself is a random value which
|
| 188 |
+
increases the size and thus the cost of the rainbow table and is currently set
|
| 189 |
+
at 128 bits with the ``salt_entropy`` value in the ``BasePasswordHasher``. As
|
| 190 |
+
computing and storage costs decrease this value should be raised. When
|
| 191 |
+
implementing your own password hasher you are free to override this value in
|
| 192 |
+
order to use a desired entropy level for your password hashes. ``salt_entropy``
|
| 193 |
+
is measured in bits.
|
| 194 |
+
|
| 195 |
+
.. admonition:: Implementation detail
|
| 196 |
+
|
| 197 |
+
Due to the method in which salt values are stored the ``salt_entropy``
|
| 198 |
+
value is effectively a minimum value. For instance a value of 128 would
|
| 199 |
+
provide a salt which would actually contain 131 bits of entropy.
|
| 200 |
+
|
| 201 |
+
.. _increasing-password-algorithm-work-factor:
|
| 202 |
+
|
| 203 |
+
Increasing the work factor
|
| 204 |
+
--------------------------
|
| 205 |
+
|
| 206 |
+
PBKDF2 and bcrypt
|
| 207 |
+
~~~~~~~~~~~~~~~~~
|
| 208 |
+
|
| 209 |
+
The PBKDF2 and bcrypt algorithms use a number of iterations or rounds of
|
| 210 |
+
hashing. This deliberately slows down attackers, making attacks against hashed
|
| 211 |
+
passwords harder. However, as computing power increases, the number of
|
| 212 |
+
iterations needs to be increased. We've chosen a reasonable default (and will
|
| 213 |
+
increase it with each release of Django), but you may wish to tune it up or
|
| 214 |
+
down, depending on your security needs and available processing power. To do so,
|
| 215 |
+
you'll subclass the appropriate algorithm and override the ``iterations``
|
| 216 |
+
parameter (use the ``rounds`` parameter when subclassing a bcrypt hasher). For
|
| 217 |
+
example, to increase the number of iterations used by the default PBKDF2
|
| 218 |
+
algorithm:
|
| 219 |
+
|
| 220 |
+
#. Create a subclass of ``django.contrib.auth.hashers.PBKDF2PasswordHasher``
|
| 221 |
+
::
|
| 222 |
+
|
| 223 |
+
from django.contrib.auth.hashers import PBKDF2PasswordHasher
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
class MyPBKDF2PasswordHasher(PBKDF2PasswordHasher):
|
| 227 |
+
"""
|
| 228 |
+
A subclass of PBKDF2PasswordHasher that uses 100 times more iterations.
|
| 229 |
+
"""
|
| 230 |
+
|
| 231 |
+
iterations = PBKDF2PasswordHasher.iterations * 100
|
| 232 |
+
|
| 233 |
+
Save this somewhere in your project. For example, you might put this in
|
| 234 |
+
a file like ``myproject/hashers.py``.
|
| 235 |
+
|
| 236 |
+
#. Add your new hasher as the first entry in :setting:`PASSWORD_HASHERS`::
|
| 237 |
+
|
| 238 |
+
PASSWORD_HASHERS = [
|
| 239 |
+
"myproject.hashers.MyPBKDF2PasswordHasher",
|
| 240 |
+
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
| 241 |
+
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
|
| 242 |
+
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
| 243 |
+
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
|
| 244 |
+
"django.contrib.auth.hashers.ScryptPasswordHasher",
|
| 245 |
+
]
|
| 246 |
+
|
| 247 |
+
That's it -- now your Django install will use more iterations when it
|
| 248 |
+
stores passwords using PBKDF2.
|
| 249 |
+
|
| 250 |
+
.. note::
|
| 251 |
+
|
| 252 |
+
bcrypt ``rounds`` is a logarithmic work factor, e.g. 12 rounds means
|
| 253 |
+
``2 ** 12`` iterations.
|
| 254 |
+
|
| 255 |
+
Argon2
|
| 256 |
+
~~~~~~
|
| 257 |
+
|
| 258 |
+
Argon2 has the following attributes that can be customized:
|
| 259 |
+
|
| 260 |
+
#. ``time_cost`` controls the number of iterations within the hash.
|
| 261 |
+
#. ``memory_cost`` controls the size of memory that must be used during the
|
| 262 |
+
computation of the hash.
|
| 263 |
+
#. ``parallelism`` controls how many CPUs the computation of the hash can be
|
| 264 |
+
parallelized on.
|
| 265 |
+
|
| 266 |
+
The default values of these attributes are probably fine for you. If you
|
| 267 |
+
determine that the password hash is too fast or too slow, you can tweak it as
|
| 268 |
+
follows:
|
| 269 |
+
|
| 270 |
+
#. Choose ``parallelism`` to be the number of threads you can
|
| 271 |
+
spare computing the hash.
|
| 272 |
+
#. Choose ``memory_cost`` to be the KiB of memory you can spare.
|
| 273 |
+
#. Adjust ``time_cost`` and measure the time hashing a password takes.
|
| 274 |
+
Pick a ``time_cost`` that takes an acceptable time for you.
|
| 275 |
+
If ``time_cost`` set to 1 is unacceptably slow, lower ``memory_cost``.
|
| 276 |
+
|
| 277 |
+
.. admonition:: ``memory_cost`` interpretation
|
| 278 |
+
|
| 279 |
+
The argon2 command-line utility and some other libraries interpret the
|
| 280 |
+
``memory_cost`` parameter differently from the value that Django uses. The
|
| 281 |
+
conversion is given by ``memory_cost == 2 ** memory_cost_commandline``.
|
| 282 |
+
|
| 283 |
+
``scrypt``
|
| 284 |
+
~~~~~~~~~~
|
| 285 |
+
|
| 286 |
+
scrypt_ has the following attributes that can be customized:
|
| 287 |
+
|
| 288 |
+
#. ``work_factor`` controls the number of iterations within the hash.
|
| 289 |
+
#. ``block_size``
|
| 290 |
+
#. ``parallelism`` controls how many threads will run in parallel.
|
| 291 |
+
#. ``maxmem`` limits the maximum size of memory that can be used during the
|
| 292 |
+
computation of the hash. Defaults to ``0``, which means the default
|
| 293 |
+
limitation from the OpenSSL library.
|
| 294 |
+
|
| 295 |
+
We've chosen reasonable defaults, but you may wish to tune it up or down,
|
| 296 |
+
depending on your security needs and available processing power.
|
| 297 |
+
|
| 298 |
+
.. admonition:: Estimating memory usage
|
| 299 |
+
|
| 300 |
+
The minimum memory requirement of scrypt_ is::
|
| 301 |
+
|
| 302 |
+
work_factor * 2 * block_size * 64
|
| 303 |
+
|
| 304 |
+
so you may need to tweak ``maxmem`` when changing the ``work_factor`` or
|
| 305 |
+
``block_size`` values.
|
| 306 |
+
|
| 307 |
+
.. _password-upgrades:
|
| 308 |
+
|
| 309 |
+
Password upgrading
|
| 310 |
+
------------------
|
| 311 |
+
|
| 312 |
+
When users log in, if their passwords are stored with anything other than
|
| 313 |
+
the preferred algorithm, Django will automatically upgrade the algorithm
|
| 314 |
+
to the preferred one. This means that old installs of Django will get
|
| 315 |
+
automatically more secure as users log in, and it also means that you
|
| 316 |
+
can switch to new (and better) storage algorithms as they get invented.
|
| 317 |
+
|
| 318 |
+
However, Django can only upgrade passwords that use algorithms mentioned in
|
| 319 |
+
:setting:`PASSWORD_HASHERS`, so as you upgrade to new systems you should make
|
| 320 |
+
sure never to *remove* entries from this list. If you do, users using
|
| 321 |
+
unmentioned algorithms won't be able to upgrade. Hashed passwords will be
|
| 322 |
+
updated when increasing (or decreasing) the number of PBKDF2 iterations, bcrypt
|
| 323 |
+
rounds, or argon2 attributes.
|
| 324 |
+
|
| 325 |
+
Be aware that if all the passwords in your database aren't encoded in the
|
| 326 |
+
default hasher's algorithm, you may be vulnerable to a user enumeration timing
|
| 327 |
+
attack due to a difference between the duration of a login request for a user
|
| 328 |
+
with a password encoded in a non-default algorithm and the duration of a login
|
| 329 |
+
request for a nonexistent user (which runs the default hasher). You may be able
|
| 330 |
+
to mitigate this by :ref:`upgrading older password hashes
|
| 331 |
+
<wrapping-password-hashers>`.
|
| 332 |
+
|
| 333 |
+
.. _wrapping-password-hashers:
|
| 334 |
+
|
| 335 |
+
Password upgrading without requiring a login
|
| 336 |
+
--------------------------------------------
|
| 337 |
+
|
| 338 |
+
If you have an existing database with an older, weak hash such as MD5, you
|
| 339 |
+
might want to upgrade those hashes yourself instead of waiting for the upgrade
|
| 340 |
+
to happen when a user logs in (which may never happen if a user doesn't return
|
| 341 |
+
to your site). In this case, you can use a "wrapped" password hasher.
|
| 342 |
+
|
| 343 |
+
For this example, we'll migrate a collection of MD5 hashes to use
|
| 344 |
+
PBKDF2(MD5(password)) and add the corresponding password hasher for checking
|
| 345 |
+
if a user entered the correct password on login. We assume we're using the
|
| 346 |
+
built-in ``User`` model and that our project has an ``accounts`` app. You can
|
| 347 |
+
modify the pattern to work with any algorithm or with a custom user model.
|
| 348 |
+
|
| 349 |
+
First, we'll add the custom hasher:
|
| 350 |
+
|
| 351 |
+
.. code-block:: python
|
| 352 |
+
:caption: ``accounts/hashers.py``
|
| 353 |
+
|
| 354 |
+
from django.contrib.auth.hashers import (
|
| 355 |
+
PBKDF2PasswordHasher,
|
| 356 |
+
MD5PasswordHasher,
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
class PBKDF2WrappedMD5PasswordHasher(PBKDF2PasswordHasher):
|
| 361 |
+
algorithm = "pbkdf2_wrapped_md5"
|
| 362 |
+
|
| 363 |
+
def encode_md5_hash(self, md5_hash, salt, iterations=None):
|
| 364 |
+
return super().encode(md5_hash, salt, iterations)
|
| 365 |
+
|
| 366 |
+
def encode(self, password, salt, iterations=None):
|
| 367 |
+
_, _, md5_hash = MD5PasswordHasher().encode(password, salt).split("$", 2)
|
| 368 |
+
return self.encode_md5_hash(md5_hash, salt, iterations)
|
| 369 |
+
|
| 370 |
+
The data migration might look something like:
|
| 371 |
+
|
| 372 |
+
.. code-block:: python
|
| 373 |
+
:caption: ``accounts/migrations/0002_migrate_md5_passwords.py``
|
| 374 |
+
|
| 375 |
+
from django.db import migrations
|
| 376 |
+
|
| 377 |
+
from ..hashers import PBKDF2WrappedMD5PasswordHasher
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def forwards_func(apps, schema_editor):
|
| 381 |
+
User = apps.get_model("auth", "User")
|
| 382 |
+
users = User.objects.filter(password__startswith="md5$")
|
| 383 |
+
hasher = PBKDF2WrappedMD5PasswordHasher()
|
| 384 |
+
for user in users:
|
| 385 |
+
algorithm, salt, md5_hash = user.password.split("$", 2)
|
| 386 |
+
user.password = hasher.encode_md5_hash(md5_hash, salt)
|
| 387 |
+
user.save(update_fields=["password"])
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
class Migration(migrations.Migration):
|
| 391 |
+
dependencies = [
|
| 392 |
+
("accounts", "0001_initial"),
|
| 393 |
+
# replace this with the latest migration in contrib.auth
|
| 394 |
+
("auth", "####_migration_name"),
|
| 395 |
+
]
|
| 396 |
+
|
| 397 |
+
operations = [
|
| 398 |
+
migrations.RunPython(forwards_func),
|
| 399 |
+
]
|
| 400 |
+
|
| 401 |
+
Be aware that this migration will take on the order of several minutes for
|
| 402 |
+
several thousand users, depending on the speed of your hardware.
|
| 403 |
+
|
| 404 |
+
Finally, we'll add a :setting:`PASSWORD_HASHERS` setting:
|
| 405 |
+
|
| 406 |
+
.. code-block:: python
|
| 407 |
+
:caption: ``mysite/settings.py``
|
| 408 |
+
|
| 409 |
+
PASSWORD_HASHERS = [
|
| 410 |
+
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
| 411 |
+
"accounts.hashers.PBKDF2WrappedMD5PasswordHasher",
|
| 412 |
+
]
|
| 413 |
+
|
| 414 |
+
Include any other hashers that your site uses in this list.
|
| 415 |
+
|
| 416 |
+
.. _pbkdf2: https://en.wikipedia.org/wiki/PBKDF2
|
| 417 |
+
.. _nist: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
|
| 418 |
+
.. _bcrypt: https://en.wikipedia.org/wiki/Bcrypt
|
| 419 |
+
.. _argon2: https://en.wikipedia.org/wiki/Argon2
|
| 420 |
+
.. _scrypt: https://en.wikipedia.org/wiki/Scrypt
|
| 421 |
+
.. _`Password Hashing Competition`: https://www.password-hashing.net/
|
| 422 |
+
|
| 423 |
+
.. _auth-included-hashers:
|
| 424 |
+
|
| 425 |
+
Included hashers
|
| 426 |
+
----------------
|
| 427 |
+
|
| 428 |
+
The full list of hashers included in Django is::
|
| 429 |
+
|
| 430 |
+
[
|
| 431 |
+
"django.contrib.auth.hashers.PBKDF2PasswordHasher",
|
| 432 |
+
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher",
|
| 433 |
+
"django.contrib.auth.hashers.Argon2PasswordHasher",
|
| 434 |
+
"django.contrib.auth.hashers.BCryptSHA256PasswordHasher",
|
| 435 |
+
"django.contrib.auth.hashers.BCryptPasswordHasher",
|
| 436 |
+
"django.contrib.auth.hashers.ScryptPasswordHasher",
|
| 437 |
+
"django.contrib.auth.hashers.MD5PasswordHasher",
|
| 438 |
+
]
|
| 439 |
+
|
| 440 |
+
The corresponding algorithm names are:
|
| 441 |
+
|
| 442 |
+
* ``pbkdf2_sha256``
|
| 443 |
+
* ``pbkdf2_sha1``
|
| 444 |
+
* ``argon2``
|
| 445 |
+
* ``bcrypt_sha256``
|
| 446 |
+
* ``bcrypt``
|
| 447 |
+
* ``scrypt``
|
| 448 |
+
* ``md5``
|
| 449 |
+
|
| 450 |
+
.. _write-your-own-password-hasher:
|
| 451 |
+
|
| 452 |
+
Writing your own hasher
|
| 453 |
+
-----------------------
|
| 454 |
+
|
| 455 |
+
If you write your own password hasher that contains a work factor such as a
|
| 456 |
+
number of iterations, you should implement a
|
| 457 |
+
``harden_runtime(self, password, encoded)`` method to bridge the runtime gap
|
| 458 |
+
between the work factor supplied in the ``encoded`` password and the default
|
| 459 |
+
work factor of the hasher. This prevents a user enumeration timing attack due
|
| 460 |
+
to difference between a login request for a user with a password encoded in an
|
| 461 |
+
older number of iterations and a nonexistent user (which runs the default
|
| 462 |
+
hasher's default number of iterations).
|
| 463 |
+
|
| 464 |
+
Taking PBKDF2 as example, if ``encoded`` contains 20,000 iterations and the
|
| 465 |
+
hasher's default ``iterations`` is 30,000, the method should run ``password``
|
| 466 |
+
through another 10,000 iterations of PBKDF2.
|
| 467 |
+
|
| 468 |
+
If your hasher doesn't have a work factor, implement the method as a no-op
|
| 469 |
+
(``pass``).
|
| 470 |
+
|
| 471 |
+
Manually managing a user's password
|
| 472 |
+
===================================
|
| 473 |
+
|
| 474 |
+
.. module:: django.contrib.auth.hashers
|
| 475 |
+
|
| 476 |
+
The :mod:`django.contrib.auth.hashers` module provides a set of functions
|
| 477 |
+
to create and validate hashed passwords. You can use them independently
|
| 478 |
+
from the ``User`` model.
|
| 479 |
+
|
| 480 |
+
.. function:: check_password(password, encoded, setter=None, preferred="default")
|
| 481 |
+
.. function:: acheck_password(password, encoded, asetter=None, preferred="default")
|
| 482 |
+
|
| 483 |
+
*Asynchronous version*: ``acheck_password()``
|
| 484 |
+
|
| 485 |
+
If you'd like to manually authenticate a user by comparing a plain-text
|
| 486 |
+
password to the hashed password in the database, use the convenience
|
| 487 |
+
function :func:`check_password`. It takes two mandatory arguments: the
|
| 488 |
+
plain-text password to check, and the full value of a user's ``password``
|
| 489 |
+
field in the database to check against. It returns ``True`` if they match,
|
| 490 |
+
``False`` otherwise. Optionally, you can pass a callable ``setter`` that
|
| 491 |
+
takes the password and will be called when you need to regenerate it. You
|
| 492 |
+
can also pass ``preferred`` to change a hashing algorithm if you don't want
|
| 493 |
+
to use the default (first entry of ``PASSWORD_HASHERS`` setting). See
|
| 494 |
+
:ref:`auth-included-hashers` for the algorithm name of each hasher.
|
| 495 |
+
|
| 496 |
+
.. versionchanged:: 5.0
|
| 497 |
+
|
| 498 |
+
``acheck_password()`` method was added.
|
| 499 |
+
|
| 500 |
+
.. function:: make_password(password, salt=None, hasher='default')
|
| 501 |
+
|
| 502 |
+
Creates a hashed password in the format used by this application. It takes
|
| 503 |
+
one mandatory argument: the password in plain-text (string or bytes).
|
| 504 |
+
Optionally, you can provide a salt and a hashing algorithm to use, if you
|
| 505 |
+
don't want to use the defaults (first entry of ``PASSWORD_HASHERS``
|
| 506 |
+
setting). See :ref:`auth-included-hashers` for the algorithm name of each
|
| 507 |
+
hasher. If the password argument is ``None``, an unusable password is
|
| 508 |
+
returned (one that will never be accepted by :func:`check_password`).
|
| 509 |
+
|
| 510 |
+
.. function:: is_password_usable(encoded_password)
|
| 511 |
+
|
| 512 |
+
Returns ``False`` if the password is a result of
|
| 513 |
+
:meth:`.User.set_unusable_password`.
|
| 514 |
+
|
| 515 |
+
.. _password-validation:
|
| 516 |
+
|
| 517 |
+
Password validation
|
| 518 |
+
===================
|
| 519 |
+
|
| 520 |
+
.. module:: django.contrib.auth.password_validation
|
| 521 |
+
|
| 522 |
+
Users often choose poor passwords. To help mitigate this problem, Django
|
| 523 |
+
offers pluggable password validation. You can configure multiple password
|
| 524 |
+
validators at the same time. A few validators are included in Django, but you
|
| 525 |
+
can write your own as well.
|
| 526 |
+
|
| 527 |
+
Each password validator must provide a help text to explain the requirements to
|
| 528 |
+
the user, validate a given password and return an error message if it does not
|
| 529 |
+
meet the requirements, and optionally receive passwords that have been set.
|
| 530 |
+
Validators can also have optional settings to fine tune their behavior.
|
| 531 |
+
|
| 532 |
+
Validation is controlled by the :setting:`AUTH_PASSWORD_VALIDATORS` setting.
|
| 533 |
+
The default for the setting is an empty list, which means no validators are
|
| 534 |
+
applied. In new projects created with the default :djadmin:`startproject`
|
| 535 |
+
template, a set of validators is enabled by default.
|
| 536 |
+
|
| 537 |
+
By default, validators are used in the forms to reset or change passwords and
|
| 538 |
+
in the :djadmin:`createsuperuser` and :djadmin:`changepassword` management
|
| 539 |
+
commands. Validators aren't applied at the model level, for example in
|
| 540 |
+
``User.objects.create_user()`` and ``create_superuser()``, because we assume
|
| 541 |
+
that developers, not users, interact with Django at that level and also because
|
| 542 |
+
model validation doesn't automatically run as part of creating models.
|
| 543 |
+
|
| 544 |
+
.. note::
|
| 545 |
+
|
| 546 |
+
Password validation can prevent the use of many types of weak passwords.
|
| 547 |
+
However, the fact that a password passes all the validators doesn't
|
| 548 |
+
guarantee that it is a strong password. There are many factors that can
|
| 549 |
+
weaken a password that are not detectable by even the most advanced
|
| 550 |
+
password validators.
|
| 551 |
+
|
| 552 |
+
Enabling password validation
|
| 553 |
+
----------------------------
|
| 554 |
+
|
| 555 |
+
Password validation is configured in the
|
| 556 |
+
:setting:`AUTH_PASSWORD_VALIDATORS` setting::
|
| 557 |
+
|
| 558 |
+
AUTH_PASSWORD_VALIDATORS = [
|
| 559 |
+
{
|
| 560 |
+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
| 561 |
+
},
|
| 562 |
+
{
|
| 563 |
+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
| 564 |
+
"OPTIONS": {
|
| 565 |
+
"min_length": 9,
|
| 566 |
+
},
|
| 567 |
+
},
|
| 568 |
+
{
|
| 569 |
+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
| 570 |
+
},
|
| 571 |
+
{
|
| 572 |
+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
| 573 |
+
},
|
| 574 |
+
]
|
| 575 |
+
|
| 576 |
+
This example enables all four included validators:
|
| 577 |
+
|
| 578 |
+
* ``UserAttributeSimilarityValidator``, which checks the similarity between
|
| 579 |
+
the password and a set of attributes of the user.
|
| 580 |
+
* ``MinimumLengthValidator``, which checks whether the password meets a minimum
|
| 581 |
+
length. This validator is configured with a custom option: it now requires
|
| 582 |
+
the minimum length to be nine characters, instead of the default eight.
|
| 583 |
+
* ``CommonPasswordValidator``, which checks whether the password occurs in a
|
| 584 |
+
list of common passwords. By default, it compares to an included list of
|
| 585 |
+
20,000 common passwords.
|
| 586 |
+
* ``NumericPasswordValidator``, which checks whether the password isn't
|
| 587 |
+
entirely numeric.
|
| 588 |
+
|
| 589 |
+
For ``UserAttributeSimilarityValidator`` and ``CommonPasswordValidator``,
|
| 590 |
+
we're using the default settings in this example. ``NumericPasswordValidator``
|
| 591 |
+
has no settings.
|
| 592 |
+
|
| 593 |
+
The help texts and any errors from password validators are always returned in
|
| 594 |
+
the order they are listed in :setting:`AUTH_PASSWORD_VALIDATORS`.
|
| 595 |
+
|
| 596 |
+
Included validators
|
| 597 |
+
-------------------
|
| 598 |
+
|
| 599 |
+
Django includes four validators:
|
| 600 |
+
|
| 601 |
+
.. class:: MinimumLengthValidator(min_length=8)
|
| 602 |
+
|
| 603 |
+
Validates that the password is of a minimum length.
|
| 604 |
+
The minimum length can be customized with the ``min_length`` parameter.
|
| 605 |
+
|
| 606 |
+
.. class:: UserAttributeSimilarityValidator(user_attributes=DEFAULT_USER_ATTRIBUTES, max_similarity=0.7)
|
| 607 |
+
|
| 608 |
+
Validates that the password is sufficiently different from certain
|
| 609 |
+
attributes of the user.
|
| 610 |
+
|
| 611 |
+
The ``user_attributes`` parameter should be an iterable of names of user
|
| 612 |
+
attributes to compare to. If this argument is not provided, the default
|
| 613 |
+
is used: ``'username', 'first_name', 'last_name', 'email'``.
|
| 614 |
+
Attributes that don't exist are ignored.
|
| 615 |
+
|
| 616 |
+
The maximum allowed similarity of passwords can be set on a scale of 0.1
|
| 617 |
+
to 1.0 with the ``max_similarity`` parameter. This is compared to the
|
| 618 |
+
result of :meth:`difflib.SequenceMatcher.quick_ratio`. A value of 0.1
|
| 619 |
+
rejects passwords unless they are substantially different from the
|
| 620 |
+
``user_attributes``, whereas a value of 1.0 rejects only passwords that are
|
| 621 |
+
identical to an attribute's value.
|
| 622 |
+
|
| 623 |
+
.. versionchanged:: 2.2.26
|
| 624 |
+
|
| 625 |
+
The ``max_similarity`` parameter was limited to a minimum value of 0.1.
|
| 626 |
+
|
| 627 |
+
.. class:: CommonPasswordValidator(password_list_path=DEFAULT_PASSWORD_LIST_PATH)
|
| 628 |
+
|
| 629 |
+
Validates that the password is not a common password. This converts the
|
| 630 |
+
password to lowercase (to do a case-insensitive comparison) and checks it
|
| 631 |
+
against a list of 20,000 common password created by `Royce Williams
|
| 632 |
+
<https://gist.github.com/roycewilliams/226886fd01572964e1431ac8afc999ce>`_.
|
| 633 |
+
|
| 634 |
+
The ``password_list_path`` can be set to the path of a custom file of
|
| 635 |
+
common passwords. This file should contain one lowercase password per line
|
| 636 |
+
and may be plain text or gzipped.
|
| 637 |
+
|
| 638 |
+
.. versionchanged:: 4.2
|
| 639 |
+
|
| 640 |
+
The list of 20,000 common passwords was updated to the most recent
|
| 641 |
+
version.
|
| 642 |
+
|
| 643 |
+
.. class:: NumericPasswordValidator()
|
| 644 |
+
|
| 645 |
+
Validate that the password is not entirely numeric.
|
| 646 |
+
|
| 647 |
+
Integrating validation
|
| 648 |
+
----------------------
|
| 649 |
+
|
| 650 |
+
There are a few functions in ``django.contrib.auth.password_validation`` that
|
| 651 |
+
you can call from your own forms or other code to integrate password
|
| 652 |
+
validation. This can be useful if you use custom forms for password setting,
|
| 653 |
+
or if you have API calls that allow passwords to be set, for example.
|
| 654 |
+
|
| 655 |
+
.. function:: validate_password(password, user=None, password_validators=None)
|
| 656 |
+
|
| 657 |
+
Validates a password. If all validators find the password valid, returns
|
| 658 |
+
``None``. If one or more validators reject the password, raises a
|
| 659 |
+
:exc:`~django.core.exceptions.ValidationError` with all the error messages
|
| 660 |
+
from the validators.
|
| 661 |
+
|
| 662 |
+
The ``user`` object is optional: if it's not provided, some validators may
|
| 663 |
+
not be able to perform any validation and will accept any password.
|
| 664 |
+
|
| 665 |
+
.. function:: password_changed(password, user=None, password_validators=None)
|
| 666 |
+
|
| 667 |
+
Informs all validators that the password has been changed. This can be used
|
| 668 |
+
by validators such as one that prevents password reuse. This should be
|
| 669 |
+
called once the password has been successfully changed.
|
| 670 |
+
|
| 671 |
+
For subclasses of :class:`~django.contrib.auth.models.AbstractBaseUser`,
|
| 672 |
+
the password field will be marked as "dirty" when calling
|
| 673 |
+
:meth:`~django.contrib.auth.models.AbstractBaseUser.set_password` which
|
| 674 |
+
triggers a call to ``password_changed()`` after the user is saved.
|
| 675 |
+
|
| 676 |
+
.. function:: password_validators_help_texts(password_validators=None)
|
| 677 |
+
|
| 678 |
+
Returns a list of the help texts of all validators. These explain the
|
| 679 |
+
password requirements to the user.
|
| 680 |
+
|
| 681 |
+
.. function:: password_validators_help_text_html(password_validators=None)
|
| 682 |
+
|
| 683 |
+
Returns an HTML string with all help texts in an ``<ul>``. This is
|
| 684 |
+
helpful when adding password validation to forms, as you can pass the
|
| 685 |
+
output directly to the ``help_text`` parameter of a form field.
|
| 686 |
+
|
| 687 |
+
.. function:: get_password_validators(validator_config)
|
| 688 |
+
|
| 689 |
+
Returns a set of validator objects based on the ``validator_config``
|
| 690 |
+
parameter. By default, all functions use the validators defined in
|
| 691 |
+
:setting:`AUTH_PASSWORD_VALIDATORS`, but by calling this function with an
|
| 692 |
+
alternate set of validators and then passing the result into the
|
| 693 |
+
``password_validators`` parameter of the other functions, your custom set
|
| 694 |
+
of validators will be used instead. This is useful when you have a typical
|
| 695 |
+
set of validators to use for most scenarios, but also have a special
|
| 696 |
+
situation that requires a custom set. If you always use the same set
|
| 697 |
+
of validators, there is no need to use this function, as the configuration
|
| 698 |
+
from :setting:`AUTH_PASSWORD_VALIDATORS` is used by default.
|
| 699 |
+
|
| 700 |
+
The structure of ``validator_config`` is identical to the
|
| 701 |
+
structure of :setting:`AUTH_PASSWORD_VALIDATORS`. The return value of
|
| 702 |
+
this function can be passed into the ``password_validators`` parameter
|
| 703 |
+
of the functions listed above.
|
| 704 |
+
|
| 705 |
+
Note that where the password is passed to one of these functions, this should
|
| 706 |
+
always be the clear text password - not a hashed password.
|
| 707 |
+
|
| 708 |
+
Writing your own validator
|
| 709 |
+
--------------------------
|
| 710 |
+
|
| 711 |
+
If Django's built-in validators are not sufficient, you can write your own
|
| 712 |
+
password validators. Validators have a fairly small interface. They must
|
| 713 |
+
implement two methods:
|
| 714 |
+
|
| 715 |
+
* ``validate(self, password, user=None)``: validate a password. Return
|
| 716 |
+
``None`` if the password is valid, or raise a
|
| 717 |
+
:exc:`~django.core.exceptions.ValidationError` with an error message if the
|
| 718 |
+
password is not valid. You must be able to deal with ``user`` being
|
| 719 |
+
``None`` - if that means your validator can't run, return ``None`` for no
|
| 720 |
+
error.
|
| 721 |
+
* ``get_help_text()``: provide a help text to explain the requirements to
|
| 722 |
+
the user.
|
| 723 |
+
|
| 724 |
+
Any items in the ``OPTIONS`` in :setting:`AUTH_PASSWORD_VALIDATORS` for your
|
| 725 |
+
validator will be passed to the constructor. All constructor arguments should
|
| 726 |
+
have a default value.
|
| 727 |
+
|
| 728 |
+
Here's a basic example of a validator, with one optional setting::
|
| 729 |
+
|
| 730 |
+
from django.core.exceptions import ValidationError
|
| 731 |
+
from django.utils.translation import gettext as _
|
| 732 |
+
|
| 733 |
+
|
| 734 |
+
class MinimumLengthValidator:
|
| 735 |
+
def __init__(self, min_length=8):
|
| 736 |
+
self.min_length = min_length
|
| 737 |
+
|
| 738 |
+
def validate(self, password, user=None):
|
| 739 |
+
if len(password) < self.min_length:
|
| 740 |
+
raise ValidationError(
|
| 741 |
+
_("This password must contain at least %(min_length)d characters."),
|
| 742 |
+
code="password_too_short",
|
| 743 |
+
params={"min_length": self.min_length},
|
| 744 |
+
)
|
| 745 |
+
|
| 746 |
+
def get_help_text(self):
|
| 747 |
+
return _(
|
| 748 |
+
"Your password must contain at least %(min_length)d characters."
|
| 749 |
+
% {"min_length": self.min_length}
|
| 750 |
+
)
|
| 751 |
+
|
| 752 |
+
You can also implement ``password_changed(password, user=None``), which will
|
| 753 |
+
be called after a successful password change. That can be used to prevent
|
| 754 |
+
password reuse, for example. However, if you decide to store a user's previous
|
| 755 |
+
passwords, you should never do so in clear text.
|
testbed/django__django/docs/topics/cache.txt
ADDED
|
@@ -0,0 +1,1514 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
========================
|
| 2 |
+
Django's cache framework
|
| 3 |
+
========================
|
| 4 |
+
|
| 5 |
+
A fundamental trade-off in dynamic websites is, well, they're dynamic. Each
|
| 6 |
+
time a user requests a page, the web server makes all sorts of calculations --
|
| 7 |
+
from database queries to template rendering to business logic -- to create the
|
| 8 |
+
page that your site's visitor sees. This is a lot more expensive, from a
|
| 9 |
+
processing-overhead perspective, than your standard
|
| 10 |
+
read-a-file-off-the-filesystem server arrangement.
|
| 11 |
+
|
| 12 |
+
For most web applications, this overhead isn't a big deal. Most web
|
| 13 |
+
applications aren't ``washingtonpost.com`` or ``slashdot.org``; they're small-
|
| 14 |
+
to medium-sized sites with so-so traffic. But for medium- to high-traffic
|
| 15 |
+
sites, it's essential to cut as much overhead as possible.
|
| 16 |
+
|
| 17 |
+
That's where caching comes in.
|
| 18 |
+
|
| 19 |
+
To cache something is to save the result of an expensive calculation so that
|
| 20 |
+
you don't have to perform the calculation next time. Here's some pseudocode
|
| 21 |
+
explaining how this would work for a dynamically generated web page:
|
| 22 |
+
|
| 23 |
+
.. code-block:: text
|
| 24 |
+
|
| 25 |
+
given a URL, try finding that page in the cache
|
| 26 |
+
if the page is in the cache:
|
| 27 |
+
return the cached page
|
| 28 |
+
else:
|
| 29 |
+
generate the page
|
| 30 |
+
save the generated page in the cache (for next time)
|
| 31 |
+
return the generated page
|
| 32 |
+
|
| 33 |
+
Django comes with a robust cache system that lets you save dynamic pages so
|
| 34 |
+
they don't have to be calculated for each request. For convenience, Django
|
| 35 |
+
offers different levels of cache granularity: You can cache the output of
|
| 36 |
+
specific views, you can cache only the pieces that are difficult to produce,
|
| 37 |
+
or you can cache your entire site.
|
| 38 |
+
|
| 39 |
+
Django also works well with "downstream" caches, such as `Squid
|
| 40 |
+
<http://www.squid-cache.org/>`_ and browser-based caches. These are the types
|
| 41 |
+
of caches that you don't directly control but to which you can provide hints
|
| 42 |
+
(via HTTP headers) about which parts of your site should be cached, and how.
|
| 43 |
+
|
| 44 |
+
.. seealso::
|
| 45 |
+
The :ref:`Cache Framework design philosophy <cache-design-philosophy>`
|
| 46 |
+
explains a few of the design decisions of the framework.
|
| 47 |
+
|
| 48 |
+
.. _setting-up-the-cache:
|
| 49 |
+
|
| 50 |
+
Setting up the cache
|
| 51 |
+
====================
|
| 52 |
+
|
| 53 |
+
The cache system requires a small amount of setup. Namely, you have to tell it
|
| 54 |
+
where your cached data should live -- whether in a database, on the filesystem
|
| 55 |
+
or directly in memory. This is an important decision that affects your cache's
|
| 56 |
+
performance; yes, some cache types are faster than others.
|
| 57 |
+
|
| 58 |
+
Your cache preference goes in the :setting:`CACHES` setting in your
|
| 59 |
+
settings file. Here's an explanation of all available values for
|
| 60 |
+
:setting:`CACHES`.
|
| 61 |
+
|
| 62 |
+
.. _memcached:
|
| 63 |
+
|
| 64 |
+
Memcached
|
| 65 |
+
---------
|
| 66 |
+
|
| 67 |
+
Memcached__ is an entirely memory-based cache server, originally developed
|
| 68 |
+
to handle high loads at LiveJournal.com and subsequently open-sourced by
|
| 69 |
+
Danga Interactive. It is used by sites such as Facebook and Wikipedia to
|
| 70 |
+
reduce database access and dramatically increase site performance.
|
| 71 |
+
|
| 72 |
+
__ https://memcached.org/
|
| 73 |
+
|
| 74 |
+
Memcached runs as a daemon and is allotted a specified amount of RAM. All it
|
| 75 |
+
does is provide a fast interface for adding, retrieving and deleting data in
|
| 76 |
+
the cache. All data is stored directly in memory, so there's no overhead of
|
| 77 |
+
database or filesystem usage.
|
| 78 |
+
|
| 79 |
+
After installing Memcached itself, you'll need to install a Memcached
|
| 80 |
+
binding. There are several Python Memcached bindings available; the
|
| 81 |
+
two supported by Django are :pypi:`pylibmc` and :pypi:`pymemcache`.
|
| 82 |
+
|
| 83 |
+
To use Memcached with Django:
|
| 84 |
+
|
| 85 |
+
* Set :setting:`BACKEND <CACHES-BACKEND>` to
|
| 86 |
+
``django.core.cache.backends.memcached.PyMemcacheCache`` or
|
| 87 |
+
``django.core.cache.backends.memcached.PyLibMCCache`` (depending on your
|
| 88 |
+
chosen memcached binding)
|
| 89 |
+
|
| 90 |
+
* Set :setting:`LOCATION <CACHES-LOCATION>` to ``ip:port`` values,
|
| 91 |
+
where ``ip`` is the IP address of the Memcached daemon and ``port`` is the
|
| 92 |
+
port on which Memcached is running, or to a ``unix:path`` value, where
|
| 93 |
+
``path`` is the path to a Memcached Unix socket file.
|
| 94 |
+
|
| 95 |
+
In this example, Memcached is running on localhost (127.0.0.1) port 11211, using
|
| 96 |
+
the ``pymemcache`` binding::
|
| 97 |
+
|
| 98 |
+
CACHES = {
|
| 99 |
+
"default": {
|
| 100 |
+
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
|
| 101 |
+
"LOCATION": "127.0.0.1:11211",
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
In this example, Memcached is available through a local Unix socket file
|
| 106 |
+
:file:`/tmp/memcached.sock` using the ``pymemcache`` binding::
|
| 107 |
+
|
| 108 |
+
CACHES = {
|
| 109 |
+
"default": {
|
| 110 |
+
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
|
| 111 |
+
"LOCATION": "unix:/tmp/memcached.sock",
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
One excellent feature of Memcached is its ability to share a cache over
|
| 116 |
+
multiple servers. This means you can run Memcached daemons on multiple
|
| 117 |
+
machines, and the program will treat the group of machines as a *single*
|
| 118 |
+
cache, without the need to duplicate cache values on each machine. To take
|
| 119 |
+
advantage of this feature, include all server addresses in
|
| 120 |
+
:setting:`LOCATION <CACHES-LOCATION>`, either as a semicolon or comma
|
| 121 |
+
delimited string, or as a list.
|
| 122 |
+
|
| 123 |
+
In this example, the cache is shared over Memcached instances running on IP
|
| 124 |
+
address 172.19.26.240 and 172.19.26.242, both on port 11211::
|
| 125 |
+
|
| 126 |
+
CACHES = {
|
| 127 |
+
"default": {
|
| 128 |
+
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
|
| 129 |
+
"LOCATION": [
|
| 130 |
+
"172.19.26.240:11211",
|
| 131 |
+
"172.19.26.242:11211",
|
| 132 |
+
],
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
In the following example, the cache is shared over Memcached instances running
|
| 137 |
+
on the IP addresses 172.19.26.240 (port 11211), 172.19.26.242 (port 11212), and
|
| 138 |
+
172.19.26.244 (port 11213)::
|
| 139 |
+
|
| 140 |
+
CACHES = {
|
| 141 |
+
"default": {
|
| 142 |
+
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
|
| 143 |
+
"LOCATION": [
|
| 144 |
+
"172.19.26.240:11211",
|
| 145 |
+
"172.19.26.242:11212",
|
| 146 |
+
"172.19.26.244:11213",
|
| 147 |
+
],
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
By default, the ``PyMemcacheCache`` backend sets the following options (you can
|
| 152 |
+
override them in your :setting:`OPTIONS <CACHES-OPTIONS>`)::
|
| 153 |
+
|
| 154 |
+
"OPTIONS": {
|
| 155 |
+
"allow_unicode_keys": True,
|
| 156 |
+
"default_noreply": False,
|
| 157 |
+
"serde": pymemcache.serde.pickle_serde,
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
A final point about Memcached is that memory-based caching has a
|
| 161 |
+
disadvantage: because the cached data is stored in memory, the data will be
|
| 162 |
+
lost if your server crashes. Clearly, memory isn't intended for permanent data
|
| 163 |
+
storage, so don't rely on memory-based caching as your only data storage.
|
| 164 |
+
Without a doubt, *none* of the Django caching backends should be used for
|
| 165 |
+
permanent storage -- they're all intended to be solutions for caching, not
|
| 166 |
+
storage -- but we point this out here because memory-based caching is
|
| 167 |
+
particularly temporary.
|
| 168 |
+
|
| 169 |
+
.. _redis:
|
| 170 |
+
|
| 171 |
+
Redis
|
| 172 |
+
-----
|
| 173 |
+
|
| 174 |
+
Redis__ is an in-memory database that can be used for caching. To begin you'll
|
| 175 |
+
need a Redis server running either locally or on a remote machine.
|
| 176 |
+
|
| 177 |
+
__ https://redis.io/
|
| 178 |
+
|
| 179 |
+
After setting up the Redis server, you'll need to install Python bindings for
|
| 180 |
+
Redis. :pypi:`redis-py <redis>` is the binding supported natively by Django.
|
| 181 |
+
Installing the :pypi:`hiredis-py <hiredis>` package is also recommended.
|
| 182 |
+
|
| 183 |
+
To use Redis as your cache backend with Django:
|
| 184 |
+
|
| 185 |
+
* Set :setting:`BACKEND <CACHES-BACKEND>` to
|
| 186 |
+
``django.core.cache.backends.redis.RedisCache``.
|
| 187 |
+
|
| 188 |
+
* Set :setting:`LOCATION <CACHES-LOCATION>` to the URL pointing to your Redis
|
| 189 |
+
instance, using the appropriate scheme. See the ``redis-py`` docs for
|
| 190 |
+
`details on the available schemes
|
| 191 |
+
<https://redis-py.readthedocs.io/en/stable/connections.html#redis.connection.ConnectionPool.from_url>`_.
|
| 192 |
+
|
| 193 |
+
For example, if Redis is running on localhost (127.0.0.1) port 6379::
|
| 194 |
+
|
| 195 |
+
CACHES = {
|
| 196 |
+
"default": {
|
| 197 |
+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
| 198 |
+
"LOCATION": "redis://127.0.0.1:6379",
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
Often Redis servers are protected with authentication. In order to supply a
|
| 203 |
+
username and password, add them in the ``LOCATION`` along with the URL::
|
| 204 |
+
|
| 205 |
+
CACHES = {
|
| 206 |
+
"default": {
|
| 207 |
+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
| 208 |
+
"LOCATION": "redis://username:password@127.0.0.1:6379",
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
If you have multiple Redis servers set up in the replication mode, you can
|
| 213 |
+
specify the servers either as a semicolon or comma delimited string, or as a
|
| 214 |
+
list. While using multiple servers, write operations are performed on the first
|
| 215 |
+
server (leader). Read operations are performed on the other servers (replicas)
|
| 216 |
+
chosen at random::
|
| 217 |
+
|
| 218 |
+
CACHES = {
|
| 219 |
+
"default": {
|
| 220 |
+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
| 221 |
+
"LOCATION": [
|
| 222 |
+
"redis://127.0.0.1:6379", # leader
|
| 223 |
+
"redis://127.0.0.1:6378", # read-replica 1
|
| 224 |
+
"redis://127.0.0.1:6377", # read-replica 2
|
| 225 |
+
],
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
.. _database-caching:
|
| 230 |
+
|
| 231 |
+
Database caching
|
| 232 |
+
----------------
|
| 233 |
+
|
| 234 |
+
Django can store its cached data in your database. This works best if you've
|
| 235 |
+
got a fast, well-indexed database server.
|
| 236 |
+
|
| 237 |
+
To use a database table as your cache backend:
|
| 238 |
+
|
| 239 |
+
* Set :setting:`BACKEND <CACHES-BACKEND>` to
|
| 240 |
+
``django.core.cache.backends.db.DatabaseCache``
|
| 241 |
+
|
| 242 |
+
* Set :setting:`LOCATION <CACHES-LOCATION>` to ``tablename``, the name of the
|
| 243 |
+
database table. This name can be whatever you want, as long as it's a valid
|
| 244 |
+
table name that's not already being used in your database.
|
| 245 |
+
|
| 246 |
+
In this example, the cache table's name is ``my_cache_table``::
|
| 247 |
+
|
| 248 |
+
CACHES = {
|
| 249 |
+
"default": {
|
| 250 |
+
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
|
| 251 |
+
"LOCATION": "my_cache_table",
|
| 252 |
+
}
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
Unlike other cache backends, the database cache does not support automatic
|
| 256 |
+
culling of expired entries at the database level. Instead, expired cache
|
| 257 |
+
entries are culled each time ``add()``, ``set()``, or ``touch()`` is called.
|
| 258 |
+
|
| 259 |
+
Creating the cache table
|
| 260 |
+
~~~~~~~~~~~~~~~~~~~~~~~~
|
| 261 |
+
|
| 262 |
+
Before using the database cache, you must create the cache table with this
|
| 263 |
+
command:
|
| 264 |
+
|
| 265 |
+
.. code-block:: shell
|
| 266 |
+
|
| 267 |
+
python manage.py createcachetable
|
| 268 |
+
|
| 269 |
+
This creates a table in your database that is in the proper format that
|
| 270 |
+
Django's database-cache system expects. The name of the table is taken from
|
| 271 |
+
:setting:`LOCATION <CACHES-LOCATION>`.
|
| 272 |
+
|
| 273 |
+
If you are using multiple database caches, :djadmin:`createcachetable` creates
|
| 274 |
+
one table for each cache.
|
| 275 |
+
|
| 276 |
+
If you are using multiple databases, :djadmin:`createcachetable` observes the
|
| 277 |
+
``allow_migrate()`` method of your database routers (see below).
|
| 278 |
+
|
| 279 |
+
Like :djadmin:`migrate`, :djadmin:`createcachetable` won't touch an existing
|
| 280 |
+
table. It will only create missing tables.
|
| 281 |
+
|
| 282 |
+
To print the SQL that would be run, rather than run it, use the
|
| 283 |
+
:option:`createcachetable --dry-run` option.
|
| 284 |
+
|
| 285 |
+
Multiple databases
|
| 286 |
+
~~~~~~~~~~~~~~~~~~
|
| 287 |
+
|
| 288 |
+
If you use database caching with multiple databases, you'll also need
|
| 289 |
+
to set up routing instructions for your database cache table. For the
|
| 290 |
+
purposes of routing, the database cache table appears as a model named
|
| 291 |
+
``CacheEntry``, in an application named ``django_cache``. This model
|
| 292 |
+
won't appear in the models cache, but the model details can be used
|
| 293 |
+
for routing purposes.
|
| 294 |
+
|
| 295 |
+
For example, the following router would direct all cache read
|
| 296 |
+
operations to ``cache_replica``, and all write operations to
|
| 297 |
+
``cache_primary``. The cache table will only be synchronized onto
|
| 298 |
+
``cache_primary``::
|
| 299 |
+
|
| 300 |
+
class CacheRouter:
|
| 301 |
+
"""A router to control all database cache operations"""
|
| 302 |
+
|
| 303 |
+
def db_for_read(self, model, **hints):
|
| 304 |
+
"All cache read operations go to the replica"
|
| 305 |
+
if model._meta.app_label == "django_cache":
|
| 306 |
+
return "cache_replica"
|
| 307 |
+
return None
|
| 308 |
+
|
| 309 |
+
def db_for_write(self, model, **hints):
|
| 310 |
+
"All cache write operations go to primary"
|
| 311 |
+
if model._meta.app_label == "django_cache":
|
| 312 |
+
return "cache_primary"
|
| 313 |
+
return None
|
| 314 |
+
|
| 315 |
+
def allow_migrate(self, db, app_label, model_name=None, **hints):
|
| 316 |
+
"Only install the cache model on primary"
|
| 317 |
+
if app_label == "django_cache":
|
| 318 |
+
return db == "cache_primary"
|
| 319 |
+
return None
|
| 320 |
+
|
| 321 |
+
If you don't specify routing directions for the database cache model,
|
| 322 |
+
the cache backend will use the ``default`` database.
|
| 323 |
+
|
| 324 |
+
And if you don't use the database cache backend, you don't need to worry about
|
| 325 |
+
providing routing instructions for the database cache model.
|
| 326 |
+
|
| 327 |
+
Filesystem caching
|
| 328 |
+
------------------
|
| 329 |
+
|
| 330 |
+
The file-based backend serializes and stores each cache value as a separate
|
| 331 |
+
file. To use this backend set :setting:`BACKEND <CACHES-BACKEND>` to
|
| 332 |
+
``"django.core.cache.backends.filebased.FileBasedCache"`` and
|
| 333 |
+
:setting:`LOCATION <CACHES-LOCATION>` to a suitable directory. For example,
|
| 334 |
+
to store cached data in ``/var/tmp/django_cache``, use this setting::
|
| 335 |
+
|
| 336 |
+
CACHES = {
|
| 337 |
+
"default": {
|
| 338 |
+
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
|
| 339 |
+
"LOCATION": "/var/tmp/django_cache",
|
| 340 |
+
}
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
If you're on Windows, put the drive letter at the beginning of the path,
|
| 344 |
+
like this::
|
| 345 |
+
|
| 346 |
+
CACHES = {
|
| 347 |
+
"default": {
|
| 348 |
+
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
|
| 349 |
+
"LOCATION": "c:/foo/bar",
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
|
| 353 |
+
The directory path should be absolute -- that is, it should start at the root
|
| 354 |
+
of your filesystem. It doesn't matter whether you put a slash at the end of the
|
| 355 |
+
setting.
|
| 356 |
+
|
| 357 |
+
Make sure the directory pointed-to by this setting either exists and is
|
| 358 |
+
readable and writable, or that it can be created by the system user under which
|
| 359 |
+
your web server runs. Continuing the above example, if your server runs as the
|
| 360 |
+
user ``apache``, make sure the directory ``/var/tmp/django_cache`` exists and
|
| 361 |
+
is readable and writable by the user ``apache``, or that it can be created by
|
| 362 |
+
the user ``apache``.
|
| 363 |
+
|
| 364 |
+
.. warning::
|
| 365 |
+
|
| 366 |
+
When the cache :setting:`LOCATION <CACHES-LOCATION>` is contained within
|
| 367 |
+
:setting:`MEDIA_ROOT`, :setting:`STATIC_ROOT`, or
|
| 368 |
+
:setting:`STATICFILES_FINDERS`, sensitive data may be exposed.
|
| 369 |
+
|
| 370 |
+
An attacker who gains access to the cache file can not only falsify HTML
|
| 371 |
+
content, which your site will trust, but also remotely execute arbitrary
|
| 372 |
+
code, as the data is serialized using :mod:`pickle`.
|
| 373 |
+
|
| 374 |
+
.. warning::
|
| 375 |
+
|
| 376 |
+
Filesystem caching may become slow when storing a large number of files. If
|
| 377 |
+
you run into this problem, consider using a different caching mechanism.
|
| 378 |
+
You can also subclass
|
| 379 |
+
:source:`FileBasedCache <django/core/cache/backends/filebased.py>` and
|
| 380 |
+
improve the culling strategy.
|
| 381 |
+
|
| 382 |
+
.. _local-memory-caching:
|
| 383 |
+
|
| 384 |
+
Local-memory caching
|
| 385 |
+
--------------------
|
| 386 |
+
|
| 387 |
+
This is the default cache if another is not specified in your settings file. If
|
| 388 |
+
you want the speed advantages of in-memory caching but don't have the capability
|
| 389 |
+
of running Memcached, consider the local-memory cache backend. This cache is
|
| 390 |
+
per-process (see below) and thread-safe. To use it, set :setting:`BACKEND
|
| 391 |
+
<CACHES-BACKEND>` to ``"django.core.cache.backends.locmem.LocMemCache"``. For
|
| 392 |
+
example::
|
| 393 |
+
|
| 394 |
+
CACHES = {
|
| 395 |
+
"default": {
|
| 396 |
+
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
|
| 397 |
+
"LOCATION": "unique-snowflake",
|
| 398 |
+
}
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
The cache :setting:`LOCATION <CACHES-LOCATION>` is used to identify individual
|
| 402 |
+
memory stores. If you only have one ``locmem`` cache, you can omit the
|
| 403 |
+
:setting:`LOCATION <CACHES-LOCATION>`; however, if you have more than one local
|
| 404 |
+
memory cache, you will need to assign a name to at least one of them in
|
| 405 |
+
order to keep them separate.
|
| 406 |
+
|
| 407 |
+
The cache uses a least-recently-used (LRU) culling strategy.
|
| 408 |
+
|
| 409 |
+
Note that each process will have its own private cache instance, which means no
|
| 410 |
+
cross-process caching is possible. This also means the local memory cache isn't
|
| 411 |
+
particularly memory-efficient, so it's probably not a good choice for
|
| 412 |
+
production environments. It's nice for development.
|
| 413 |
+
|
| 414 |
+
Dummy caching (for development)
|
| 415 |
+
-------------------------------
|
| 416 |
+
|
| 417 |
+
Finally, Django comes with a "dummy" cache that doesn't actually cache -- it
|
| 418 |
+
just implements the cache interface without doing anything.
|
| 419 |
+
|
| 420 |
+
This is useful if you have a production site that uses heavy-duty caching in
|
| 421 |
+
various places but a development/test environment where you don't want to cache
|
| 422 |
+
and don't want to have to change your code to special-case the latter. To
|
| 423 |
+
activate dummy caching, set :setting:`BACKEND <CACHES-BACKEND>` like so::
|
| 424 |
+
|
| 425 |
+
CACHES = {
|
| 426 |
+
"default": {
|
| 427 |
+
"BACKEND": "django.core.cache.backends.dummy.DummyCache",
|
| 428 |
+
}
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
Using a custom cache backend
|
| 432 |
+
----------------------------
|
| 433 |
+
|
| 434 |
+
While Django includes support for a number of cache backends out-of-the-box,
|
| 435 |
+
sometimes you might want to use a customized cache backend. To use an external
|
| 436 |
+
cache backend with Django, use the Python import path as the
|
| 437 |
+
:setting:`BACKEND <CACHES-BACKEND>` of the :setting:`CACHES` setting, like so::
|
| 438 |
+
|
| 439 |
+
CACHES = {
|
| 440 |
+
"default": {
|
| 441 |
+
"BACKEND": "path.to.backend",
|
| 442 |
+
}
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
If you're building your own backend, you can use the standard cache backends
|
| 446 |
+
as reference implementations. You'll find the code in the
|
| 447 |
+
:source:`django/core/cache/backends/` directory of the Django source.
|
| 448 |
+
|
| 449 |
+
Note: Without a really compelling reason, such as a host that doesn't support
|
| 450 |
+
them, you should stick to the cache backends included with Django. They've
|
| 451 |
+
been well-tested and are well-documented.
|
| 452 |
+
|
| 453 |
+
.. _cache_arguments:
|
| 454 |
+
|
| 455 |
+
Cache arguments
|
| 456 |
+
---------------
|
| 457 |
+
|
| 458 |
+
Each cache backend can be given additional arguments to control caching
|
| 459 |
+
behavior. These arguments are provided as additional keys in the
|
| 460 |
+
:setting:`CACHES` setting. Valid arguments are as follows:
|
| 461 |
+
|
| 462 |
+
* :setting:`TIMEOUT <CACHES-TIMEOUT>`: The default timeout, in
|
| 463 |
+
seconds, to use for the cache. This argument defaults to ``300`` seconds (5 minutes).
|
| 464 |
+
You can set ``TIMEOUT`` to ``None`` so that, by default, cache keys never
|
| 465 |
+
expire. A value of ``0`` causes keys to immediately expire (effectively
|
| 466 |
+
"don't cache").
|
| 467 |
+
|
| 468 |
+
* :setting:`OPTIONS <CACHES-OPTIONS>`: Any options that should be
|
| 469 |
+
passed to the cache backend. The list of valid options will vary
|
| 470 |
+
with each backend, and cache backends backed by a third-party library
|
| 471 |
+
will pass their options directly to the underlying cache library.
|
| 472 |
+
|
| 473 |
+
Cache backends that implement their own culling strategy (i.e.,
|
| 474 |
+
the ``locmem``, ``filesystem`` and ``database`` backends) will
|
| 475 |
+
honor the following options:
|
| 476 |
+
|
| 477 |
+
* ``MAX_ENTRIES``: The maximum number of entries allowed in
|
| 478 |
+
the cache before old values are deleted. This argument
|
| 479 |
+
defaults to ``300``.
|
| 480 |
+
|
| 481 |
+
* ``CULL_FREQUENCY``: The fraction of entries that are culled
|
| 482 |
+
when ``MAX_ENTRIES`` is reached. The actual ratio is
|
| 483 |
+
``1 / CULL_FREQUENCY``, so set ``CULL_FREQUENCY`` to ``2`` to
|
| 484 |
+
cull half the entries when ``MAX_ENTRIES`` is reached. This argument
|
| 485 |
+
should be an integer and defaults to ``3``.
|
| 486 |
+
|
| 487 |
+
A value of ``0`` for ``CULL_FREQUENCY`` means that the
|
| 488 |
+
entire cache will be dumped when ``MAX_ENTRIES`` is reached.
|
| 489 |
+
On some backends (``database`` in particular) this makes culling *much*
|
| 490 |
+
faster at the expense of more cache misses.
|
| 491 |
+
|
| 492 |
+
The Memcached and Redis backends pass the contents of :setting:`OPTIONS
|
| 493 |
+
<CACHES-OPTIONS>` as keyword arguments to the client constructors, allowing
|
| 494 |
+
for more advanced control of client behavior. For example usage, see below.
|
| 495 |
+
|
| 496 |
+
* :setting:`KEY_PREFIX <CACHES-KEY_PREFIX>`: A string that will be
|
| 497 |
+
automatically included (prepended by default) to all cache keys
|
| 498 |
+
used by the Django server.
|
| 499 |
+
|
| 500 |
+
See the :ref:`cache documentation <cache_key_prefixing>` for
|
| 501 |
+
more information.
|
| 502 |
+
|
| 503 |
+
* :setting:`VERSION <CACHES-VERSION>`: The default version number
|
| 504 |
+
for cache keys generated by the Django server.
|
| 505 |
+
|
| 506 |
+
See the :ref:`cache documentation <cache_versioning>` for more
|
| 507 |
+
information.
|
| 508 |
+
|
| 509 |
+
* :setting:`KEY_FUNCTION <CACHES-KEY_FUNCTION>`
|
| 510 |
+
A string containing a dotted path to a function that defines how
|
| 511 |
+
to compose a prefix, version and key into a final cache key.
|
| 512 |
+
|
| 513 |
+
See the :ref:`cache documentation <cache_key_transformation>`
|
| 514 |
+
for more information.
|
| 515 |
+
|
| 516 |
+
In this example, a filesystem backend is being configured with a timeout
|
| 517 |
+
of 60 seconds, and a maximum capacity of 1000 items::
|
| 518 |
+
|
| 519 |
+
CACHES = {
|
| 520 |
+
"default": {
|
| 521 |
+
"BACKEND": "django.core.cache.backends.filebased.FileBasedCache",
|
| 522 |
+
"LOCATION": "/var/tmp/django_cache",
|
| 523 |
+
"TIMEOUT": 60,
|
| 524 |
+
"OPTIONS": {"MAX_ENTRIES": 1000},
|
| 525 |
+
}
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
Here's an example configuration for a ``pylibmc`` based backend that enables
|
| 529 |
+
the binary protocol, SASL authentication, and the ``ketama`` behavior mode::
|
| 530 |
+
|
| 531 |
+
CACHES = {
|
| 532 |
+
"default": {
|
| 533 |
+
"BACKEND": "django.core.cache.backends.memcached.PyLibMCCache",
|
| 534 |
+
"LOCATION": "127.0.0.1:11211",
|
| 535 |
+
"OPTIONS": {
|
| 536 |
+
"binary": True,
|
| 537 |
+
"username": "user",
|
| 538 |
+
"password": "pass",
|
| 539 |
+
"behaviors": {
|
| 540 |
+
"ketama": True,
|
| 541 |
+
},
|
| 542 |
+
},
|
| 543 |
+
}
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
Here's an example configuration for a ``pymemcache`` based backend that enables
|
| 547 |
+
client pooling (which may improve performance by keeping clients connected),
|
| 548 |
+
treats memcache/network errors as cache misses, and sets the ``TCP_NODELAY``
|
| 549 |
+
flag on the connection's socket::
|
| 550 |
+
|
| 551 |
+
CACHES = {
|
| 552 |
+
"default": {
|
| 553 |
+
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
|
| 554 |
+
"LOCATION": "127.0.0.1:11211",
|
| 555 |
+
"OPTIONS": {
|
| 556 |
+
"no_delay": True,
|
| 557 |
+
"ignore_exc": True,
|
| 558 |
+
"max_pool_size": 4,
|
| 559 |
+
"use_pooling": True,
|
| 560 |
+
},
|
| 561 |
+
}
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
Here's an example configuration for a ``redis`` based backend that selects
|
| 565 |
+
database ``10`` (by default Redis ships with 16 logical databases), specifies a
|
| 566 |
+
`parser class`_ (``redis.connection.HiredisParser`` will be used by default if
|
| 567 |
+
the ``hiredis-py`` package is installed), and sets a custom `connection pool
|
| 568 |
+
class`_ (``redis.ConnectionPool`` is used by default)::
|
| 569 |
+
|
| 570 |
+
CACHES = {
|
| 571 |
+
"default": {
|
| 572 |
+
"BACKEND": "django.core.cache.backends.redis.RedisCache",
|
| 573 |
+
"LOCATION": "redis://127.0.0.1:6379",
|
| 574 |
+
"OPTIONS": {
|
| 575 |
+
"db": "10",
|
| 576 |
+
"parser_class": "redis.connection.PythonParser",
|
| 577 |
+
"pool_class": "redis.BlockingConnectionPool",
|
| 578 |
+
},
|
| 579 |
+
}
|
| 580 |
+
}
|
| 581 |
+
|
| 582 |
+
.. _`parser class`: https://github.com/redis/redis-py#parsers
|
| 583 |
+
.. _`connection pool class`: https://github.com/redis/redis-py#connection-pools
|
| 584 |
+
|
| 585 |
+
.. _the-per-site-cache:
|
| 586 |
+
|
| 587 |
+
The per-site cache
|
| 588 |
+
==================
|
| 589 |
+
|
| 590 |
+
Once the cache is set up, the simplest way to use caching is to cache your
|
| 591 |
+
entire site. You'll need to add
|
| 592 |
+
``'django.middleware.cache.UpdateCacheMiddleware'`` and
|
| 593 |
+
``'django.middleware.cache.FetchFromCacheMiddleware'`` to your
|
| 594 |
+
:setting:`MIDDLEWARE` setting, as in this example::
|
| 595 |
+
|
| 596 |
+
MIDDLEWARE = [
|
| 597 |
+
"django.middleware.cache.UpdateCacheMiddleware",
|
| 598 |
+
"django.middleware.common.CommonMiddleware",
|
| 599 |
+
"django.middleware.cache.FetchFromCacheMiddleware",
|
| 600 |
+
]
|
| 601 |
+
|
| 602 |
+
.. note::
|
| 603 |
+
|
| 604 |
+
No, that's not a typo: the "update" middleware must be first in the list,
|
| 605 |
+
and the "fetch" middleware must be last. The details are a bit obscure, but
|
| 606 |
+
see `Order of MIDDLEWARE`_ below if you'd like the full story.
|
| 607 |
+
|
| 608 |
+
Then, add the following required settings to your Django settings file:
|
| 609 |
+
|
| 610 |
+
* :setting:`CACHE_MIDDLEWARE_ALIAS` -- The cache alias to use for storage.
|
| 611 |
+
* :setting:`CACHE_MIDDLEWARE_SECONDS` -- The number of seconds each page should
|
| 612 |
+
be cached.
|
| 613 |
+
* :setting:`CACHE_MIDDLEWARE_KEY_PREFIX` -- If the cache is shared across
|
| 614 |
+
multiple sites using the same Django installation, set this to the name of
|
| 615 |
+
the site, or some other string that is unique to this Django instance, to
|
| 616 |
+
prevent key collisions. Use an empty string if you don't care.
|
| 617 |
+
|
| 618 |
+
``FetchFromCacheMiddleware`` caches GET and HEAD responses with status 200,
|
| 619 |
+
where the request and response headers allow. Responses to requests for the same
|
| 620 |
+
URL with different query parameters are considered to be unique pages and are
|
| 621 |
+
cached separately. This middleware expects that a HEAD request is answered with
|
| 622 |
+
the same response headers as the corresponding GET request; in which case it can
|
| 623 |
+
return a cached GET response for HEAD request.
|
| 624 |
+
|
| 625 |
+
Additionally, ``UpdateCacheMiddleware`` automatically sets a few headers in
|
| 626 |
+
each :class:`~django.http.HttpResponse` which affect :ref:`downstream caches
|
| 627 |
+
<downstream-caches>`:
|
| 628 |
+
|
| 629 |
+
* Sets the ``Expires`` header to the current date/time plus the defined
|
| 630 |
+
:setting:`CACHE_MIDDLEWARE_SECONDS`.
|
| 631 |
+
|
| 632 |
+
* Sets the ``Cache-Control`` header to give a max age for the page --
|
| 633 |
+
again, from the :setting:`CACHE_MIDDLEWARE_SECONDS` setting.
|
| 634 |
+
|
| 635 |
+
See :doc:`/topics/http/middleware` for more on middleware.
|
| 636 |
+
|
| 637 |
+
If a view sets its own cache expiry time (i.e. it has a ``max-age`` section in
|
| 638 |
+
its ``Cache-Control`` header) then the page will be cached until the expiry
|
| 639 |
+
time, rather than :setting:`CACHE_MIDDLEWARE_SECONDS`. Using the decorators in
|
| 640 |
+
``django.views.decorators.cache`` you can easily set a view's expiry time
|
| 641 |
+
(using the :func:`~django.views.decorators.cache.cache_control` decorator) or
|
| 642 |
+
disable caching for a view (using the
|
| 643 |
+
:func:`~django.views.decorators.cache.never_cache` decorator). See the
|
| 644 |
+
`using other headers`__ section for more on these decorators.
|
| 645 |
+
|
| 646 |
+
.. _i18n-cache-key:
|
| 647 |
+
|
| 648 |
+
If :setting:`USE_I18N` is set to ``True`` then the generated cache key will
|
| 649 |
+
include the name of the active :term:`language<language code>` -- see also
|
| 650 |
+
:ref:`how-django-discovers-language-preference`). This allows you to easily
|
| 651 |
+
cache multilingual sites without having to create the cache key yourself.
|
| 652 |
+
|
| 653 |
+
Cache keys also include the :ref:`current time zone
|
| 654 |
+
<default-current-time-zone>` when :setting:`USE_TZ` is set to ``True``.
|
| 655 |
+
|
| 656 |
+
__ `Controlling cache: Using other headers`_
|
| 657 |
+
|
| 658 |
+
The per-view cache
|
| 659 |
+
==================
|
| 660 |
+
|
| 661 |
+
.. function:: django.views.decorators.cache.cache_page(timeout, *, cache=None, key_prefix=None)
|
| 662 |
+
|
| 663 |
+
A more granular way to use the caching framework is by caching the output of
|
| 664 |
+
individual views. ``django.views.decorators.cache`` defines a ``cache_page``
|
| 665 |
+
decorator that will automatically cache the view's response for you::
|
| 666 |
+
|
| 667 |
+
from django.views.decorators.cache import cache_page
|
| 668 |
+
|
| 669 |
+
|
| 670 |
+
@cache_page(60 * 15)
|
| 671 |
+
def my_view(request):
|
| 672 |
+
...
|
| 673 |
+
|
| 674 |
+
``cache_page`` takes a single argument: the cache timeout, in seconds. In the
|
| 675 |
+
above example, the result of the ``my_view()`` view will be cached for 15
|
| 676 |
+
minutes. (Note that we've written it as ``60 * 15`` for the purpose of
|
| 677 |
+
readability. ``60 * 15`` will be evaluated to ``900`` -- that is, 15 minutes
|
| 678 |
+
multiplied by 60 seconds per minute.)
|
| 679 |
+
|
| 680 |
+
The cache timeout set by ``cache_page`` takes precedence over the ``max-age``
|
| 681 |
+
directive from the ``Cache-Control`` header.
|
| 682 |
+
|
| 683 |
+
The per-view cache, like the per-site cache, is keyed off of the URL. If
|
| 684 |
+
multiple URLs point at the same view, each URL will be cached separately.
|
| 685 |
+
Continuing the ``my_view`` example, if your URLconf looks like this::
|
| 686 |
+
|
| 687 |
+
urlpatterns = [
|
| 688 |
+
path("foo/<int:code>/", my_view),
|
| 689 |
+
]
|
| 690 |
+
|
| 691 |
+
then requests to ``/foo/1/`` and ``/foo/23/`` will be cached separately, as
|
| 692 |
+
you may expect. But once a particular URL (e.g., ``/foo/23/``) has been
|
| 693 |
+
requested, subsequent requests to that URL will use the cache.
|
| 694 |
+
|
| 695 |
+
``cache_page`` can also take an optional keyword argument, ``cache``,
|
| 696 |
+
which directs the decorator to use a specific cache (from your
|
| 697 |
+
:setting:`CACHES` setting) when caching view results. By default, the
|
| 698 |
+
``default`` cache will be used, but you can specify any cache you
|
| 699 |
+
want::
|
| 700 |
+
|
| 701 |
+
@cache_page(60 * 15, cache="special_cache")
|
| 702 |
+
def my_view(request):
|
| 703 |
+
...
|
| 704 |
+
|
| 705 |
+
You can also override the cache prefix on a per-view basis. ``cache_page``
|
| 706 |
+
takes an optional keyword argument, ``key_prefix``,
|
| 707 |
+
which works in the same way as the :setting:`CACHE_MIDDLEWARE_KEY_PREFIX`
|
| 708 |
+
setting for the middleware. It can be used like this::
|
| 709 |
+
|
| 710 |
+
@cache_page(60 * 15, key_prefix="site1")
|
| 711 |
+
def my_view(request):
|
| 712 |
+
...
|
| 713 |
+
|
| 714 |
+
The ``key_prefix`` and ``cache`` arguments may be specified together. The
|
| 715 |
+
``key_prefix`` argument and the :setting:`KEY_PREFIX <CACHES-KEY_PREFIX>`
|
| 716 |
+
specified under :setting:`CACHES` will be concatenated.
|
| 717 |
+
|
| 718 |
+
Additionally, ``cache_page`` automatically sets ``Cache-Control`` and
|
| 719 |
+
``Expires`` headers in the response which affect :ref:`downstream caches
|
| 720 |
+
<downstream-caches>`.
|
| 721 |
+
|
| 722 |
+
Specifying per-view cache in the URLconf
|
| 723 |
+
----------------------------------------
|
| 724 |
+
|
| 725 |
+
The examples in the previous section have hard-coded the fact that the view is
|
| 726 |
+
cached, because ``cache_page`` alters the ``my_view`` function in place. This
|
| 727 |
+
approach couples your view to the cache system, which is not ideal for several
|
| 728 |
+
reasons. For instance, you might want to reuse the view functions on another,
|
| 729 |
+
cache-less site, or you might want to distribute the views to people who might
|
| 730 |
+
want to use them without being cached. The solution to these problems is to
|
| 731 |
+
specify the per-view cache in the URLconf rather than next to the view functions
|
| 732 |
+
themselves.
|
| 733 |
+
|
| 734 |
+
You can do so by wrapping the view function with ``cache_page`` when you refer
|
| 735 |
+
to it in the URLconf. Here's the old URLconf from earlier::
|
| 736 |
+
|
| 737 |
+
urlpatterns = [
|
| 738 |
+
path("foo/<int:code>/", my_view),
|
| 739 |
+
]
|
| 740 |
+
|
| 741 |
+
Here's the same thing, with ``my_view`` wrapped in ``cache_page``::
|
| 742 |
+
|
| 743 |
+
from django.views.decorators.cache import cache_page
|
| 744 |
+
|
| 745 |
+
urlpatterns = [
|
| 746 |
+
path("foo/<int:code>/", cache_page(60 * 15)(my_view)),
|
| 747 |
+
]
|
| 748 |
+
|
| 749 |
+
.. templatetag:: cache
|
| 750 |
+
|
| 751 |
+
Template fragment caching
|
| 752 |
+
=========================
|
| 753 |
+
|
| 754 |
+
If you're after even more control, you can also cache template fragments using
|
| 755 |
+
the ``cache`` template tag. To give your template access to this tag, put
|
| 756 |
+
``{% load cache %}`` near the top of your template.
|
| 757 |
+
|
| 758 |
+
The ``{% cache %}`` template tag caches the contents of the block for a given
|
| 759 |
+
amount of time. It takes at least two arguments: the cache timeout, in seconds,
|
| 760 |
+
and the name to give the cache fragment. The fragment is cached forever if
|
| 761 |
+
timeout is ``None``. The name will be taken as is, do not use a variable. For
|
| 762 |
+
example:
|
| 763 |
+
|
| 764 |
+
.. code-block:: html+django
|
| 765 |
+
|
| 766 |
+
{% load cache %}
|
| 767 |
+
{% cache 500 sidebar %}
|
| 768 |
+
.. sidebar ..
|
| 769 |
+
{% endcache %}
|
| 770 |
+
|
| 771 |
+
Sometimes you might want to cache multiple copies of a fragment depending on
|
| 772 |
+
some dynamic data that appears inside the fragment. For example, you might want a
|
| 773 |
+
separate cached copy of the sidebar used in the previous example for every user
|
| 774 |
+
of your site. Do this by passing one or more additional arguments, which may be
|
| 775 |
+
variables with or without filters, to the ``{% cache %}`` template tag to
|
| 776 |
+
uniquely identify the cache fragment:
|
| 777 |
+
|
| 778 |
+
.. code-block:: html+django
|
| 779 |
+
|
| 780 |
+
{% load cache %}
|
| 781 |
+
{% cache 500 sidebar request.user.username %}
|
| 782 |
+
.. sidebar for logged in user ..
|
| 783 |
+
{% endcache %}
|
| 784 |
+
|
| 785 |
+
If :setting:`USE_I18N` is set to ``True`` the per-site middleware cache will
|
| 786 |
+
:ref:`respect the active language<i18n-cache-key>`. For the ``cache`` template
|
| 787 |
+
tag you could use one of the
|
| 788 |
+
:ref:`translation-specific variables<template-translation-vars>` available in
|
| 789 |
+
templates to achieve the same result:
|
| 790 |
+
|
| 791 |
+
.. code-block:: html+django
|
| 792 |
+
|
| 793 |
+
{% load i18n %}
|
| 794 |
+
{% load cache %}
|
| 795 |
+
|
| 796 |
+
{% get_current_language as LANGUAGE_CODE %}
|
| 797 |
+
|
| 798 |
+
{% cache 600 welcome LANGUAGE_CODE %}
|
| 799 |
+
{% translate "Welcome to example.com" %}
|
| 800 |
+
{% endcache %}
|
| 801 |
+
|
| 802 |
+
The cache timeout can be a template variable, as long as the template variable
|
| 803 |
+
resolves to an integer value. For example, if the template variable
|
| 804 |
+
``my_timeout`` is set to the value ``600``, then the following two examples are
|
| 805 |
+
equivalent:
|
| 806 |
+
|
| 807 |
+
.. code-block:: html+django
|
| 808 |
+
|
| 809 |
+
{% cache 600 sidebar %} ... {% endcache %}
|
| 810 |
+
{% cache my_timeout sidebar %} ... {% endcache %}
|
| 811 |
+
|
| 812 |
+
This feature is useful in avoiding repetition in templates. You can set the
|
| 813 |
+
timeout in a variable, in one place, and reuse that value.
|
| 814 |
+
|
| 815 |
+
By default, the cache tag will try to use the cache called "template_fragments".
|
| 816 |
+
If no such cache exists, it will fall back to using the default cache. You may
|
| 817 |
+
select an alternate cache backend to use with the ``using`` keyword argument,
|
| 818 |
+
which must be the last argument to the tag.
|
| 819 |
+
|
| 820 |
+
.. code-block:: html+django
|
| 821 |
+
|
| 822 |
+
{% cache 300 local-thing ... using="localcache" %}
|
| 823 |
+
|
| 824 |
+
It is considered an error to specify a cache name that is not configured.
|
| 825 |
+
|
| 826 |
+
.. function:: django.core.cache.utils.make_template_fragment_key(fragment_name, vary_on=None)
|
| 827 |
+
|
| 828 |
+
If you want to obtain the cache key used for a cached fragment, you can use
|
| 829 |
+
``make_template_fragment_key``. ``fragment_name`` is the same as second argument
|
| 830 |
+
to the ``cache`` template tag; ``vary_on`` is a list of all additional arguments
|
| 831 |
+
passed to the tag. This function can be useful for invalidating or overwriting
|
| 832 |
+
a cached item, for example:
|
| 833 |
+
|
| 834 |
+
.. code-block:: pycon
|
| 835 |
+
|
| 836 |
+
>>> from django.core.cache import cache
|
| 837 |
+
>>> from django.core.cache.utils import make_template_fragment_key
|
| 838 |
+
# cache key for {% cache 500 sidebar username %}
|
| 839 |
+
>>> key = make_template_fragment_key("sidebar", [username])
|
| 840 |
+
>>> cache.delete(key) # invalidates cached template fragment
|
| 841 |
+
True
|
| 842 |
+
|
| 843 |
+
.. _low-level-cache-api:
|
| 844 |
+
|
| 845 |
+
The low-level cache API
|
| 846 |
+
=======================
|
| 847 |
+
|
| 848 |
+
Sometimes, caching an entire rendered page doesn't gain you very much and is,
|
| 849 |
+
in fact, inconvenient overkill.
|
| 850 |
+
|
| 851 |
+
Perhaps, for instance, your site includes a view whose results depend on
|
| 852 |
+
several expensive queries, the results of which change at different intervals.
|
| 853 |
+
In this case, it would not be ideal to use the full-page caching that the
|
| 854 |
+
per-site or per-view cache strategies offer, because you wouldn't want to
|
| 855 |
+
cache the entire result (since some of the data changes often), but you'd still
|
| 856 |
+
want to cache the results that rarely change.
|
| 857 |
+
|
| 858 |
+
For cases like this, Django exposes a low-level cache API. You can use this API
|
| 859 |
+
to store objects in the cache with any level of granularity you like. You can
|
| 860 |
+
cache any Python object that can be pickled safely: strings, dictionaries,
|
| 861 |
+
lists of model objects, and so forth. (Most common Python objects can be
|
| 862 |
+
pickled; refer to the Python documentation for more information about
|
| 863 |
+
pickling.)
|
| 864 |
+
|
| 865 |
+
Accessing the cache
|
| 866 |
+
-------------------
|
| 867 |
+
|
| 868 |
+
.. data:: django.core.cache.caches
|
| 869 |
+
|
| 870 |
+
You can access the caches configured in the :setting:`CACHES` setting
|
| 871 |
+
through a dict-like object: ``django.core.cache.caches``. Repeated
|
| 872 |
+
requests for the same alias in the same thread will return the same
|
| 873 |
+
object.
|
| 874 |
+
|
| 875 |
+
>>> from django.core.cache import caches
|
| 876 |
+
>>> cache1 = caches['myalias']
|
| 877 |
+
>>> cache2 = caches['myalias']
|
| 878 |
+
>>> cache1 is cache2
|
| 879 |
+
True
|
| 880 |
+
|
| 881 |
+
If the named key does not exist, ``InvalidCacheBackendError`` will be
|
| 882 |
+
raised.
|
| 883 |
+
|
| 884 |
+
To provide thread-safety, a different instance of the cache backend will
|
| 885 |
+
be returned for each thread.
|
| 886 |
+
|
| 887 |
+
.. data:: django.core.cache.cache
|
| 888 |
+
|
| 889 |
+
As a shortcut, the default cache is available as
|
| 890 |
+
``django.core.cache.cache``:
|
| 891 |
+
|
| 892 |
+
.. code-block:: pycon
|
| 893 |
+
|
| 894 |
+
>>> from django.core.cache import cache
|
| 895 |
+
|
| 896 |
+
This object is equivalent to ``caches['default']``.
|
| 897 |
+
|
| 898 |
+
.. _cache-basic-interface:
|
| 899 |
+
|
| 900 |
+
Basic usage
|
| 901 |
+
-----------
|
| 902 |
+
|
| 903 |
+
.. currentmodule:: django.core.cache
|
| 904 |
+
|
| 905 |
+
The basic interface is:
|
| 906 |
+
|
| 907 |
+
.. method:: cache.set(key, value, timeout=DEFAULT_TIMEOUT, version=None)
|
| 908 |
+
|
| 909 |
+
>>> cache.set('my_key', 'hello, world!', 30)
|
| 910 |
+
|
| 911 |
+
.. method:: cache.get(key, default=None, version=None)
|
| 912 |
+
|
| 913 |
+
>>> cache.get('my_key')
|
| 914 |
+
'hello, world!'
|
| 915 |
+
|
| 916 |
+
``key`` should be a ``str``, and ``value`` can be any picklable Python object.
|
| 917 |
+
|
| 918 |
+
The ``timeout`` argument is optional and defaults to the ``timeout`` argument
|
| 919 |
+
of the appropriate backend in the :setting:`CACHES` setting (explained above).
|
| 920 |
+
It's the number of seconds the value should be stored in the cache. Passing in
|
| 921 |
+
``None`` for ``timeout`` will cache the value forever. A ``timeout`` of ``0``
|
| 922 |
+
won't cache the value.
|
| 923 |
+
|
| 924 |
+
If the object doesn't exist in the cache, ``cache.get()`` returns ``None``:
|
| 925 |
+
|
| 926 |
+
.. code-block:: pycon
|
| 927 |
+
|
| 928 |
+
>>> # Wait 30 seconds for 'my_key' to expire...
|
| 929 |
+
>>> cache.get("my_key")
|
| 930 |
+
None
|
| 931 |
+
|
| 932 |
+
If you need to determine whether the object exists in the cache and you have
|
| 933 |
+
stored a literal value ``None``, use a sentinel object as the default:
|
| 934 |
+
|
| 935 |
+
.. code-block:: pycon
|
| 936 |
+
|
| 937 |
+
>>> sentinel = object()
|
| 938 |
+
>>> cache.get("my_key", sentinel) is sentinel
|
| 939 |
+
False
|
| 940 |
+
>>> # Wait 30 seconds for 'my_key' to expire...
|
| 941 |
+
>>> cache.get("my_key", sentinel) is sentinel
|
| 942 |
+
True
|
| 943 |
+
|
| 944 |
+
``cache.get()`` can take a ``default`` argument. This specifies which value to
|
| 945 |
+
return if the object doesn't exist in the cache:
|
| 946 |
+
|
| 947 |
+
.. code-block:: pycon
|
| 948 |
+
|
| 949 |
+
>>> cache.get("my_key", "has expired")
|
| 950 |
+
'has expired'
|
| 951 |
+
|
| 952 |
+
.. method:: cache.add(key, value, timeout=DEFAULT_TIMEOUT, version=None)
|
| 953 |
+
|
| 954 |
+
To add a key only if it doesn't already exist, use the ``add()`` method.
|
| 955 |
+
It takes the same parameters as ``set()``, but it will not attempt to
|
| 956 |
+
update the cache if the key specified is already present:
|
| 957 |
+
|
| 958 |
+
.. code-block:: pycon
|
| 959 |
+
|
| 960 |
+
>>> cache.set("add_key", "Initial value")
|
| 961 |
+
>>> cache.add("add_key", "New value")
|
| 962 |
+
>>> cache.get("add_key")
|
| 963 |
+
'Initial value'
|
| 964 |
+
|
| 965 |
+
If you need to know whether ``add()`` stored a value in the cache, you can
|
| 966 |
+
check the return value. It will return ``True`` if the value was stored,
|
| 967 |
+
``False`` otherwise.
|
| 968 |
+
|
| 969 |
+
.. method:: cache.get_or_set(key, default, timeout=DEFAULT_TIMEOUT, version=None)
|
| 970 |
+
|
| 971 |
+
If you want to get a key's value or set a value if the key isn't in the cache,
|
| 972 |
+
there is the ``get_or_set()`` method. It takes the same parameters as ``get()``
|
| 973 |
+
but the default is set as the new cache value for that key, rather than
|
| 974 |
+
returned:
|
| 975 |
+
|
| 976 |
+
.. code-block:: pycon
|
| 977 |
+
|
| 978 |
+
>>> cache.get("my_new_key") # returns None
|
| 979 |
+
>>> cache.get_or_set("my_new_key", "my new value", 100)
|
| 980 |
+
'my new value'
|
| 981 |
+
|
| 982 |
+
You can also pass any callable as a *default* value:
|
| 983 |
+
|
| 984 |
+
.. code-block:: pycon
|
| 985 |
+
|
| 986 |
+
>>> import datetime
|
| 987 |
+
>>> cache.get_or_set("some-timestamp-key", datetime.datetime.now)
|
| 988 |
+
datetime.datetime(2014, 12, 11, 0, 15, 49, 457920)
|
| 989 |
+
|
| 990 |
+
.. method:: cache.get_many(keys, version=None)
|
| 991 |
+
|
| 992 |
+
There's also a ``get_many()`` interface that only hits the cache once.
|
| 993 |
+
``get_many()`` returns a dictionary with all the keys you asked for that
|
| 994 |
+
actually exist in the cache (and haven't expired):
|
| 995 |
+
|
| 996 |
+
.. code-block:: pycon
|
| 997 |
+
|
| 998 |
+
>>> cache.set("a", 1)
|
| 999 |
+
>>> cache.set("b", 2)
|
| 1000 |
+
>>> cache.set("c", 3)
|
| 1001 |
+
>>> cache.get_many(["a", "b", "c"])
|
| 1002 |
+
{'a': 1, 'b': 2, 'c': 3}
|
| 1003 |
+
|
| 1004 |
+
.. method:: cache.set_many(dict, timeout)
|
| 1005 |
+
|
| 1006 |
+
To set multiple values more efficiently, use ``set_many()`` to pass a dictionary
|
| 1007 |
+
of key-value pairs:
|
| 1008 |
+
|
| 1009 |
+
.. code-block:: pycon
|
| 1010 |
+
|
| 1011 |
+
>>> cache.set_many({"a": 1, "b": 2, "c": 3})
|
| 1012 |
+
>>> cache.get_many(["a", "b", "c"])
|
| 1013 |
+
{'a': 1, 'b': 2, 'c': 3}
|
| 1014 |
+
|
| 1015 |
+
Like ``cache.set()``, ``set_many()`` takes an optional ``timeout`` parameter.
|
| 1016 |
+
|
| 1017 |
+
On supported backends (memcached), ``set_many()`` returns a list of keys that
|
| 1018 |
+
failed to be inserted.
|
| 1019 |
+
|
| 1020 |
+
.. method:: cache.delete(key, version=None)
|
| 1021 |
+
|
| 1022 |
+
You can delete keys explicitly with ``delete()`` to clear the cache for a
|
| 1023 |
+
particular object:
|
| 1024 |
+
|
| 1025 |
+
.. code-block:: pycon
|
| 1026 |
+
|
| 1027 |
+
>>> cache.delete("a")
|
| 1028 |
+
True
|
| 1029 |
+
|
| 1030 |
+
``delete()`` returns ``True`` if the key was successfully deleted, ``False``
|
| 1031 |
+
otherwise.
|
| 1032 |
+
|
| 1033 |
+
.. method:: cache.delete_many(keys, version=None)
|
| 1034 |
+
|
| 1035 |
+
If you want to clear a bunch of keys at once, ``delete_many()`` can take a list
|
| 1036 |
+
of keys to be cleared:
|
| 1037 |
+
|
| 1038 |
+
.. code-block:: pycon
|
| 1039 |
+
|
| 1040 |
+
>>> cache.delete_many(["a", "b", "c"])
|
| 1041 |
+
|
| 1042 |
+
.. method:: cache.clear()
|
| 1043 |
+
|
| 1044 |
+
Finally, if you want to delete all the keys in the cache, use
|
| 1045 |
+
``cache.clear()``. Be careful with this; ``clear()`` will remove *everything*
|
| 1046 |
+
from the cache, not just the keys set by your application. :
|
| 1047 |
+
|
| 1048 |
+
.. code-block:: pycon
|
| 1049 |
+
|
| 1050 |
+
>>> cache.clear()
|
| 1051 |
+
|
| 1052 |
+
.. method:: cache.touch(key, timeout=DEFAULT_TIMEOUT, version=None)
|
| 1053 |
+
|
| 1054 |
+
``cache.touch()`` sets a new expiration for a key. For example, to update a key
|
| 1055 |
+
to expire 10 seconds from now:
|
| 1056 |
+
|
| 1057 |
+
.. code-block:: pycon
|
| 1058 |
+
|
| 1059 |
+
>>> cache.touch("a", 10)
|
| 1060 |
+
True
|
| 1061 |
+
|
| 1062 |
+
Like other methods, the ``timeout`` argument is optional and defaults to the
|
| 1063 |
+
``TIMEOUT`` option of the appropriate backend in the :setting:`CACHES` setting.
|
| 1064 |
+
|
| 1065 |
+
``touch()`` returns ``True`` if the key was successfully touched, ``False``
|
| 1066 |
+
otherwise.
|
| 1067 |
+
|
| 1068 |
+
.. method:: cache.incr(key, delta=1, version=None)
|
| 1069 |
+
.. method:: cache.decr(key, delta=1, version=None)
|
| 1070 |
+
|
| 1071 |
+
You can also increment or decrement a key that already exists using the
|
| 1072 |
+
``incr()`` or ``decr()`` methods, respectively. By default, the existing cache
|
| 1073 |
+
value will be incremented or decremented by 1. Other increment/decrement values
|
| 1074 |
+
can be specified by providing an argument to the increment/decrement call. A
|
| 1075 |
+
ValueError will be raised if you attempt to increment or decrement a
|
| 1076 |
+
nonexistent cache key:
|
| 1077 |
+
|
| 1078 |
+
.. code-block:: pycon
|
| 1079 |
+
|
| 1080 |
+
>>> cache.set("num", 1)
|
| 1081 |
+
>>> cache.incr("num")
|
| 1082 |
+
2
|
| 1083 |
+
>>> cache.incr("num", 10)
|
| 1084 |
+
12
|
| 1085 |
+
>>> cache.decr("num")
|
| 1086 |
+
11
|
| 1087 |
+
>>> cache.decr("num", 5)
|
| 1088 |
+
6
|
| 1089 |
+
|
| 1090 |
+
.. note::
|
| 1091 |
+
|
| 1092 |
+
``incr()``/``decr()`` methods are not guaranteed to be atomic. On those
|
| 1093 |
+
backends that support atomic increment/decrement (most notably, the
|
| 1094 |
+
memcached backend), increment and decrement operations will be atomic.
|
| 1095 |
+
However, if the backend doesn't natively provide an increment/decrement
|
| 1096 |
+
operation, it will be implemented using a two-step retrieve/update.
|
| 1097 |
+
|
| 1098 |
+
.. method:: cache.close()
|
| 1099 |
+
|
| 1100 |
+
You can close the connection to your cache with ``close()`` if implemented by
|
| 1101 |
+
the cache backend.
|
| 1102 |
+
|
| 1103 |
+
>>> cache.close()
|
| 1104 |
+
|
| 1105 |
+
.. note::
|
| 1106 |
+
|
| 1107 |
+
For caches that don't implement ``close`` methods it is a no-op.
|
| 1108 |
+
|
| 1109 |
+
.. note::
|
| 1110 |
+
|
| 1111 |
+
The async variants of base methods are prefixed with ``a``, e.g.
|
| 1112 |
+
``cache.aadd()`` or ``cache.adelete_many()``. See `Asynchronous support`_
|
| 1113 |
+
for more details.
|
| 1114 |
+
|
| 1115 |
+
.. _cache_key_prefixing:
|
| 1116 |
+
|
| 1117 |
+
Cache key prefixing
|
| 1118 |
+
-------------------
|
| 1119 |
+
|
| 1120 |
+
If you are sharing a cache instance between servers, or between your
|
| 1121 |
+
production and development environments, it's possible for data cached
|
| 1122 |
+
by one server to be used by another server. If the format of cached
|
| 1123 |
+
data is different between servers, this can lead to some very hard to
|
| 1124 |
+
diagnose problems.
|
| 1125 |
+
|
| 1126 |
+
To prevent this, Django provides the ability to prefix all cache keys
|
| 1127 |
+
used by a server. When a particular cache key is saved or retrieved,
|
| 1128 |
+
Django will automatically prefix the cache key with the value of the
|
| 1129 |
+
:setting:`KEY_PREFIX <CACHES-KEY_PREFIX>` cache setting.
|
| 1130 |
+
|
| 1131 |
+
By ensuring each Django instance has a different
|
| 1132 |
+
:setting:`KEY_PREFIX <CACHES-KEY_PREFIX>`, you can ensure that there will be no
|
| 1133 |
+
collisions in cache values.
|
| 1134 |
+
|
| 1135 |
+
.. _cache_versioning:
|
| 1136 |
+
|
| 1137 |
+
Cache versioning
|
| 1138 |
+
----------------
|
| 1139 |
+
|
| 1140 |
+
When you change running code that uses cached values, you may need to
|
| 1141 |
+
purge any existing cached values. The easiest way to do this is to
|
| 1142 |
+
flush the entire cache, but this can lead to the loss of cache values
|
| 1143 |
+
that are still valid and useful.
|
| 1144 |
+
|
| 1145 |
+
Django provides a better way to target individual cache values.
|
| 1146 |
+
Django's cache framework has a system-wide version identifier,
|
| 1147 |
+
specified using the :setting:`VERSION <CACHES-VERSION>` cache setting.
|
| 1148 |
+
The value of this setting is automatically combined with the cache
|
| 1149 |
+
prefix and the user-provided cache key to obtain the final cache key.
|
| 1150 |
+
|
| 1151 |
+
By default, any key request will automatically include the site
|
| 1152 |
+
default cache key version. However, the primitive cache functions all
|
| 1153 |
+
include a ``version`` argument, so you can specify a particular cache
|
| 1154 |
+
key version to set or get. For example:
|
| 1155 |
+
|
| 1156 |
+
.. code-block:: pycon
|
| 1157 |
+
|
| 1158 |
+
>>> # Set version 2 of a cache key
|
| 1159 |
+
>>> cache.set("my_key", "hello world!", version=2)
|
| 1160 |
+
>>> # Get the default version (assuming version=1)
|
| 1161 |
+
>>> cache.get("my_key")
|
| 1162 |
+
None
|
| 1163 |
+
>>> # Get version 2 of the same key
|
| 1164 |
+
>>> cache.get("my_key", version=2)
|
| 1165 |
+
'hello world!'
|
| 1166 |
+
|
| 1167 |
+
The version of a specific key can be incremented and decremented using
|
| 1168 |
+
the ``incr_version()`` and ``decr_version()`` methods. This
|
| 1169 |
+
enables specific keys to be bumped to a new version, leaving other
|
| 1170 |
+
keys unaffected. Continuing our previous example:
|
| 1171 |
+
|
| 1172 |
+
.. code-block:: pycon
|
| 1173 |
+
|
| 1174 |
+
>>> # Increment the version of 'my_key'
|
| 1175 |
+
>>> cache.incr_version("my_key")
|
| 1176 |
+
>>> # The default version still isn't available
|
| 1177 |
+
>>> cache.get("my_key")
|
| 1178 |
+
None
|
| 1179 |
+
# Version 2 isn't available, either
|
| 1180 |
+
>>> cache.get("my_key", version=2)
|
| 1181 |
+
None
|
| 1182 |
+
>>> # But version 3 *is* available
|
| 1183 |
+
>>> cache.get("my_key", version=3)
|
| 1184 |
+
'hello world!'
|
| 1185 |
+
|
| 1186 |
+
.. _cache_key_transformation:
|
| 1187 |
+
|
| 1188 |
+
Cache key transformation
|
| 1189 |
+
------------------------
|
| 1190 |
+
|
| 1191 |
+
As described in the previous two sections, the cache key provided by a
|
| 1192 |
+
user is not used verbatim -- it is combined with the cache prefix and
|
| 1193 |
+
key version to provide a final cache key. By default, the three parts
|
| 1194 |
+
are joined using colons to produce a final string::
|
| 1195 |
+
|
| 1196 |
+
def make_key(key, key_prefix, version):
|
| 1197 |
+
return "%s:%s:%s" % (key_prefix, version, key)
|
| 1198 |
+
|
| 1199 |
+
If you want to combine the parts in different ways, or apply other
|
| 1200 |
+
processing to the final key (e.g., taking a hash digest of the key
|
| 1201 |
+
parts), you can provide a custom key function.
|
| 1202 |
+
|
| 1203 |
+
The :setting:`KEY_FUNCTION <CACHES-KEY_FUNCTION>` cache setting
|
| 1204 |
+
specifies a dotted-path to a function matching the prototype of
|
| 1205 |
+
``make_key()`` above. If provided, this custom key function will
|
| 1206 |
+
be used instead of the default key combining function.
|
| 1207 |
+
|
| 1208 |
+
Cache key warnings
|
| 1209 |
+
------------------
|
| 1210 |
+
|
| 1211 |
+
Memcached, the most commonly-used production cache backend, does not allow
|
| 1212 |
+
cache keys longer than 250 characters or containing whitespace or control
|
| 1213 |
+
characters, and using such keys will cause an exception. To encourage
|
| 1214 |
+
cache-portable code and minimize unpleasant surprises, the other built-in cache
|
| 1215 |
+
backends issue a warning (``django.core.cache.backends.base.CacheKeyWarning``)
|
| 1216 |
+
if a key is used that would cause an error on memcached.
|
| 1217 |
+
|
| 1218 |
+
If you are using a production backend that can accept a wider range of keys (a
|
| 1219 |
+
custom backend, or one of the non-memcached built-in backends), and want to use
|
| 1220 |
+
this wider range without warnings, you can silence ``CacheKeyWarning`` with
|
| 1221 |
+
this code in the ``management`` module of one of your
|
| 1222 |
+
:setting:`INSTALLED_APPS`::
|
| 1223 |
+
|
| 1224 |
+
import warnings
|
| 1225 |
+
|
| 1226 |
+
from django.core.cache import CacheKeyWarning
|
| 1227 |
+
|
| 1228 |
+
warnings.simplefilter("ignore", CacheKeyWarning)
|
| 1229 |
+
|
| 1230 |
+
If you want to instead provide custom key validation logic for one of the
|
| 1231 |
+
built-in backends, you can subclass it, override just the ``validate_key``
|
| 1232 |
+
method, and follow the instructions for `using a custom cache backend`_. For
|
| 1233 |
+
instance, to do this for the ``locmem`` backend, put this code in a module::
|
| 1234 |
+
|
| 1235 |
+
from django.core.cache.backends.locmem import LocMemCache
|
| 1236 |
+
|
| 1237 |
+
|
| 1238 |
+
class CustomLocMemCache(LocMemCache):
|
| 1239 |
+
def validate_key(self, key):
|
| 1240 |
+
"""Custom validation, raising exceptions or warnings as needed."""
|
| 1241 |
+
...
|
| 1242 |
+
|
| 1243 |
+
...and use the dotted Python path to this class in the
|
| 1244 |
+
:setting:`BACKEND <CACHES-BACKEND>` portion of your :setting:`CACHES` setting.
|
| 1245 |
+
|
| 1246 |
+
.. _asynchronous_support:
|
| 1247 |
+
|
| 1248 |
+
Asynchronous support
|
| 1249 |
+
====================
|
| 1250 |
+
|
| 1251 |
+
Django has developing support for asynchronous cache backends, but does not
|
| 1252 |
+
yet support asynchronous caching. It will be coming in a future release.
|
| 1253 |
+
|
| 1254 |
+
``django.core.cache.backends.base.BaseCache`` has async variants of :ref:`all
|
| 1255 |
+
base methods <cache-basic-interface>`. By convention, the asynchronous versions
|
| 1256 |
+
of all methods are prefixed with ``a``. By default, the arguments for both
|
| 1257 |
+
variants are the same:
|
| 1258 |
+
|
| 1259 |
+
.. code-block:: pycon
|
| 1260 |
+
|
| 1261 |
+
>>> await cache.aset("num", 1)
|
| 1262 |
+
>>> await cache.ahas_key("num")
|
| 1263 |
+
True
|
| 1264 |
+
|
| 1265 |
+
.. _downstream-caches:
|
| 1266 |
+
|
| 1267 |
+
Downstream caches
|
| 1268 |
+
=================
|
| 1269 |
+
|
| 1270 |
+
So far, this document has focused on caching your *own* data. But another type
|
| 1271 |
+
of caching is relevant to web development, too: caching performed by
|
| 1272 |
+
"downstream" caches. These are systems that cache pages for users even before
|
| 1273 |
+
the request reaches your website.
|
| 1274 |
+
|
| 1275 |
+
Here are a few examples of downstream caches:
|
| 1276 |
+
|
| 1277 |
+
* When using HTTP, your :abbr:`ISP (Internet Service Provider)` may cache
|
| 1278 |
+
certain pages, so if you requested a page from ``http://example.com/``, your
|
| 1279 |
+
ISP would send you the page without having to access example.com directly.
|
| 1280 |
+
The maintainers of example.com have no knowledge of this caching; the ISP
|
| 1281 |
+
sits between example.com and your web browser, handling all of the caching
|
| 1282 |
+
transparently. Such caching is not possible under HTTPS as it would
|
| 1283 |
+
constitute a man-in-the-middle attack.
|
| 1284 |
+
|
| 1285 |
+
* Your Django website may sit behind a *proxy cache*, such as Squid Web
|
| 1286 |
+
Proxy Cache (http://www.squid-cache.org/), that caches pages for
|
| 1287 |
+
performance. In this case, each request first would be handled by the
|
| 1288 |
+
proxy, and it would be passed to your application only if needed.
|
| 1289 |
+
|
| 1290 |
+
* Your web browser caches pages, too. If a web page sends out the
|
| 1291 |
+
appropriate headers, your browser will use the local cached copy for
|
| 1292 |
+
subsequent requests to that page, without even contacting the web page
|
| 1293 |
+
again to see whether it has changed.
|
| 1294 |
+
|
| 1295 |
+
Downstream caching is a nice efficiency boost, but there's a danger to it:
|
| 1296 |
+
Many web pages' contents differ based on authentication and a host of other
|
| 1297 |
+
variables, and cache systems that blindly save pages based purely on URLs could
|
| 1298 |
+
expose incorrect or sensitive data to subsequent visitors to those pages.
|
| 1299 |
+
|
| 1300 |
+
For example, if you operate a web email system, then the contents of the
|
| 1301 |
+
"inbox" page depend on which user is logged in. If an ISP blindly cached your
|
| 1302 |
+
site, then the first user who logged in through that ISP would have their
|
| 1303 |
+
user-specific inbox page cached for subsequent visitors to the site. That's
|
| 1304 |
+
not cool.
|
| 1305 |
+
|
| 1306 |
+
Fortunately, HTTP provides a solution to this problem. A number of HTTP headers
|
| 1307 |
+
exist to instruct downstream caches to differ their cache contents depending on
|
| 1308 |
+
designated variables, and to tell caching mechanisms not to cache particular
|
| 1309 |
+
pages. We'll look at some of these headers in the sections that follow.
|
| 1310 |
+
|
| 1311 |
+
.. _using-vary-headers:
|
| 1312 |
+
|
| 1313 |
+
Using ``Vary`` headers
|
| 1314 |
+
======================
|
| 1315 |
+
|
| 1316 |
+
The ``Vary`` header defines which request headers a cache
|
| 1317 |
+
mechanism should take into account when building its cache key. For example, if
|
| 1318 |
+
the contents of a web page depend on a user's language preference, the page is
|
| 1319 |
+
said to "vary on language."
|
| 1320 |
+
|
| 1321 |
+
By default, Django's cache system creates its cache keys using the requested
|
| 1322 |
+
fully-qualified URL -- e.g.,
|
| 1323 |
+
``"https://www.example.com/stories/2005/?order_by=author"``. This means every
|
| 1324 |
+
request to that URL will use the same cached version, regardless of user-agent
|
| 1325 |
+
differences such as cookies or language preferences. However, if this page
|
| 1326 |
+
produces different content based on some difference in request headers -- such
|
| 1327 |
+
as a cookie, or a language, or a user-agent -- you'll need to use the ``Vary``
|
| 1328 |
+
header to tell caching mechanisms that the page output depends on those things.
|
| 1329 |
+
|
| 1330 |
+
To do this in Django, use the convenient
|
| 1331 |
+
:func:`django.views.decorators.vary.vary_on_headers` view decorator, like so::
|
| 1332 |
+
|
| 1333 |
+
from django.views.decorators.vary import vary_on_headers
|
| 1334 |
+
|
| 1335 |
+
|
| 1336 |
+
@vary_on_headers("User-Agent")
|
| 1337 |
+
def my_view(request):
|
| 1338 |
+
...
|
| 1339 |
+
|
| 1340 |
+
In this case, a caching mechanism (such as Django's own cache middleware) will
|
| 1341 |
+
cache a separate version of the page for each unique user-agent.
|
| 1342 |
+
|
| 1343 |
+
The advantage to using the ``vary_on_headers`` decorator rather than manually
|
| 1344 |
+
setting the ``Vary`` header (using something like ``response.headers['Vary'] =
|
| 1345 |
+
'user-agent'``) is that the decorator *adds* to the ``Vary`` header (which may
|
| 1346 |
+
already exist), rather than setting it from scratch and potentially overriding
|
| 1347 |
+
anything that was already in there.
|
| 1348 |
+
|
| 1349 |
+
You can pass multiple headers to ``vary_on_headers()``::
|
| 1350 |
+
|
| 1351 |
+
@vary_on_headers("User-Agent", "Cookie")
|
| 1352 |
+
def my_view(request):
|
| 1353 |
+
...
|
| 1354 |
+
|
| 1355 |
+
This tells downstream caches to vary on *both*, which means each combination of
|
| 1356 |
+
user-agent and cookie will get its own cache value. For example, a request with
|
| 1357 |
+
the user-agent ``Mozilla`` and the cookie value ``foo=bar`` will be considered
|
| 1358 |
+
different from a request with the user-agent ``Mozilla`` and the cookie value
|
| 1359 |
+
``foo=ham``.
|
| 1360 |
+
|
| 1361 |
+
Because varying on cookie is so common, there's a
|
| 1362 |
+
:func:`django.views.decorators.vary.vary_on_cookie` decorator. These two views
|
| 1363 |
+
are equivalent::
|
| 1364 |
+
|
| 1365 |
+
@vary_on_cookie
|
| 1366 |
+
def my_view(request):
|
| 1367 |
+
...
|
| 1368 |
+
|
| 1369 |
+
|
| 1370 |
+
@vary_on_headers("Cookie")
|
| 1371 |
+
def my_view(request):
|
| 1372 |
+
...
|
| 1373 |
+
|
| 1374 |
+
The headers you pass to ``vary_on_headers`` are not case sensitive;
|
| 1375 |
+
``"User-Agent"`` is the same thing as ``"user-agent"``.
|
| 1376 |
+
|
| 1377 |
+
You can also use a helper function, :func:`django.utils.cache.patch_vary_headers`,
|
| 1378 |
+
directly. This function sets, or adds to, the ``Vary header``. For example::
|
| 1379 |
+
|
| 1380 |
+
from django.shortcuts import render
|
| 1381 |
+
from django.utils.cache import patch_vary_headers
|
| 1382 |
+
|
| 1383 |
+
|
| 1384 |
+
def my_view(request):
|
| 1385 |
+
...
|
| 1386 |
+
response = render(request, "template_name", context)
|
| 1387 |
+
patch_vary_headers(response, ["Cookie"])
|
| 1388 |
+
return response
|
| 1389 |
+
|
| 1390 |
+
``patch_vary_headers`` takes an :class:`~django.http.HttpResponse` instance as
|
| 1391 |
+
its first argument and a list/tuple of case-insensitive header names as its
|
| 1392 |
+
second argument.
|
| 1393 |
+
|
| 1394 |
+
For more on Vary headers, see the :rfc:`official Vary spec
|
| 1395 |
+
<9110#section-12.5.5>`.
|
| 1396 |
+
|
| 1397 |
+
Controlling cache: Using other headers
|
| 1398 |
+
======================================
|
| 1399 |
+
|
| 1400 |
+
Other problems with caching are the privacy of data and the question of where
|
| 1401 |
+
data should be stored in a cascade of caches.
|
| 1402 |
+
|
| 1403 |
+
A user usually faces two kinds of caches: their own browser cache (a private
|
| 1404 |
+
cache) and their provider's cache (a public cache). A public cache is used by
|
| 1405 |
+
multiple users and controlled by someone else. This poses problems with
|
| 1406 |
+
sensitive data--you don't want, say, your bank account number stored in a
|
| 1407 |
+
public cache. So web applications need a way to tell caches which data is
|
| 1408 |
+
private and which is public.
|
| 1409 |
+
|
| 1410 |
+
The solution is to indicate a page's cache should be "private." To do this in
|
| 1411 |
+
Django, use the :func:`~django.views.decorators.cache.cache_control` view
|
| 1412 |
+
decorator. Example::
|
| 1413 |
+
|
| 1414 |
+
from django.views.decorators.cache import cache_control
|
| 1415 |
+
|
| 1416 |
+
|
| 1417 |
+
@cache_control(private=True)
|
| 1418 |
+
def my_view(request):
|
| 1419 |
+
...
|
| 1420 |
+
|
| 1421 |
+
This decorator takes care of sending out the appropriate HTTP header behind the
|
| 1422 |
+
scenes.
|
| 1423 |
+
|
| 1424 |
+
Note that the cache control settings "private" and "public" are mutually
|
| 1425 |
+
exclusive. The decorator ensures that the "public" directive is removed if
|
| 1426 |
+
"private" should be set (and vice versa). An example use of the two directives
|
| 1427 |
+
would be a blog site that offers both private and public entries. Public
|
| 1428 |
+
entries may be cached on any shared cache. The following code uses
|
| 1429 |
+
:func:`~django.utils.cache.patch_cache_control`, the manual way to modify the
|
| 1430 |
+
cache control header (it is internally called by the
|
| 1431 |
+
:func:`~django.views.decorators.cache.cache_control` decorator)::
|
| 1432 |
+
|
| 1433 |
+
from django.views.decorators.cache import patch_cache_control
|
| 1434 |
+
from django.views.decorators.vary import vary_on_cookie
|
| 1435 |
+
|
| 1436 |
+
|
| 1437 |
+
@vary_on_cookie
|
| 1438 |
+
def list_blog_entries_view(request):
|
| 1439 |
+
if request.user.is_anonymous:
|
| 1440 |
+
response = render_only_public_entries()
|
| 1441 |
+
patch_cache_control(response, public=True)
|
| 1442 |
+
else:
|
| 1443 |
+
response = render_private_and_public_entries(request.user)
|
| 1444 |
+
patch_cache_control(response, private=True)
|
| 1445 |
+
|
| 1446 |
+
return response
|
| 1447 |
+
|
| 1448 |
+
You can control downstream caches in other ways as well (see :rfc:`9111` for
|
| 1449 |
+
details on HTTP caching). For example, even if you don't use Django's
|
| 1450 |
+
server-side cache framework, you can still tell clients to cache a view for a
|
| 1451 |
+
certain amount of time with the :rfc:`max-age <9111#section-5.2.2.1>`
|
| 1452 |
+
directive::
|
| 1453 |
+
|
| 1454 |
+
from django.views.decorators.cache import cache_control
|
| 1455 |
+
|
| 1456 |
+
|
| 1457 |
+
@cache_control(max_age=3600)
|
| 1458 |
+
def my_view(request):
|
| 1459 |
+
...
|
| 1460 |
+
|
| 1461 |
+
(If you *do* use the caching middleware, it already sets the ``max-age`` with
|
| 1462 |
+
the value of the :setting:`CACHE_MIDDLEWARE_SECONDS` setting. In that case,
|
| 1463 |
+
the custom ``max_age`` from the
|
| 1464 |
+
:func:`~django.views.decorators.cache.cache_control` decorator will take
|
| 1465 |
+
precedence, and the header values will be merged correctly.)
|
| 1466 |
+
|
| 1467 |
+
Any valid ``Cache-Control`` response directive is valid in ``cache_control()``.
|
| 1468 |
+
Here are some more examples:
|
| 1469 |
+
|
| 1470 |
+
* ``no_transform=True``
|
| 1471 |
+
* ``must_revalidate=True``
|
| 1472 |
+
* ``stale_while_revalidate=num_seconds``
|
| 1473 |
+
* ``no_cache=True``
|
| 1474 |
+
|
| 1475 |
+
The full list of known directives can be found in the `IANA registry`_
|
| 1476 |
+
(note that not all of them apply to responses).
|
| 1477 |
+
|
| 1478 |
+
.. _IANA registry: https://www.iana.org/assignments/http-cache-directives/http-cache-directives.xhtml
|
| 1479 |
+
|
| 1480 |
+
If you want to use headers to disable caching altogether,
|
| 1481 |
+
:func:`~django.views.decorators.cache.never_cache` is a view decorator that
|
| 1482 |
+
adds headers to ensure the response won't be cached by browsers or other
|
| 1483 |
+
caches. Example::
|
| 1484 |
+
|
| 1485 |
+
from django.views.decorators.cache import never_cache
|
| 1486 |
+
|
| 1487 |
+
|
| 1488 |
+
@never_cache
|
| 1489 |
+
def myview(request):
|
| 1490 |
+
...
|
| 1491 |
+
|
| 1492 |
+
Order of ``MIDDLEWARE``
|
| 1493 |
+
=======================
|
| 1494 |
+
|
| 1495 |
+
If you use caching middleware, it's important to put each half in the right
|
| 1496 |
+
place within the :setting:`MIDDLEWARE` setting. That's because the cache
|
| 1497 |
+
middleware needs to know which headers by which to vary the cache storage.
|
| 1498 |
+
Middleware always adds something to the ``Vary`` response header when it can.
|
| 1499 |
+
|
| 1500 |
+
``UpdateCacheMiddleware`` runs during the response phase, where middleware is
|
| 1501 |
+
run in reverse order, so an item at the top of the list runs *last* during the
|
| 1502 |
+
response phase. Thus, you need to make sure that ``UpdateCacheMiddleware``
|
| 1503 |
+
appears *before* any other middleware that might add something to the ``Vary``
|
| 1504 |
+
header. The following middleware modules do so:
|
| 1505 |
+
|
| 1506 |
+
* ``SessionMiddleware`` adds ``Cookie``
|
| 1507 |
+
* ``GZipMiddleware`` adds ``Accept-Encoding``
|
| 1508 |
+
* ``LocaleMiddleware`` adds ``Accept-Language``
|
| 1509 |
+
|
| 1510 |
+
``FetchFromCacheMiddleware``, on the other hand, runs during the request phase,
|
| 1511 |
+
where middleware is applied first-to-last, so an item at the top of the list
|
| 1512 |
+
runs *first* during the request phase. The ``FetchFromCacheMiddleware`` also
|
| 1513 |
+
needs to run after other middleware updates the ``Vary`` header, so
|
| 1514 |
+
``FetchFromCacheMiddleware`` must be *after* any item that does so.
|
testbed/django__django/docs/topics/class-based-views/generic-display.txt
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
==================================
|
| 2 |
+
Built-in class-based generic views
|
| 3 |
+
==================================
|
| 4 |
+
|
| 5 |
+
Writing web applications can be monotonous, because we repeat certain patterns
|
| 6 |
+
again and again. Django tries to take away some of that monotony at the model
|
| 7 |
+
and template layers, but web developers also experience this boredom at the view
|
| 8 |
+
level.
|
| 9 |
+
|
| 10 |
+
Django's *generic views* were developed to ease that pain. They take certain
|
| 11 |
+
common idioms and patterns found in view development and abstract them so that
|
| 12 |
+
you can quickly write common views of data without having to write too much
|
| 13 |
+
code.
|
| 14 |
+
|
| 15 |
+
We can recognize certain common tasks, like displaying a list of objects, and
|
| 16 |
+
write code that displays a list of *any* object. Then the model in question can
|
| 17 |
+
be passed as an extra argument to the URLconf.
|
| 18 |
+
|
| 19 |
+
Django ships with generic views to do the following:
|
| 20 |
+
|
| 21 |
+
* Display list and detail pages for a single object. If we were creating an
|
| 22 |
+
application to manage conferences then a ``TalkListView`` and a
|
| 23 |
+
``RegisteredUserListView`` would be examples of list views. A single
|
| 24 |
+
talk page is an example of what we call a "detail" view.
|
| 25 |
+
|
| 26 |
+
* Present date-based objects in year/month/day archive pages,
|
| 27 |
+
associated detail, and "latest" pages.
|
| 28 |
+
|
| 29 |
+
* Allow users to create, update, and delete objects -- with or
|
| 30 |
+
without authorization.
|
| 31 |
+
|
| 32 |
+
Taken together, these views provide interfaces to perform the most common tasks
|
| 33 |
+
developers encounter.
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
Extending generic views
|
| 37 |
+
=======================
|
| 38 |
+
|
| 39 |
+
There's no question that using generic views can speed up development
|
| 40 |
+
substantially. In most projects, however, there comes a moment when the
|
| 41 |
+
generic views no longer suffice. Indeed, the most common question asked by new
|
| 42 |
+
Django developers is how to make generic views handle a wider array of
|
| 43 |
+
situations.
|
| 44 |
+
|
| 45 |
+
This is one of the reasons generic views were redesigned for the 1.3 release -
|
| 46 |
+
previously, they were view functions with a bewildering array of options; now,
|
| 47 |
+
rather than passing in a large amount of configuration in the URLconf, the
|
| 48 |
+
recommended way to extend generic views is to subclass them, and override their
|
| 49 |
+
attributes or methods.
|
| 50 |
+
|
| 51 |
+
That said, generic views will have a limit. If you find you're struggling to
|
| 52 |
+
implement your view as a subclass of a generic view, then you may find it more
|
| 53 |
+
effective to write just the code you need, using your own class-based or
|
| 54 |
+
functional views.
|
| 55 |
+
|
| 56 |
+
More examples of generic views are available in some third party applications,
|
| 57 |
+
or you could write your own as needed.
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
Generic views of objects
|
| 61 |
+
========================
|
| 62 |
+
|
| 63 |
+
:class:`~django.views.generic.base.TemplateView` certainly is useful, but
|
| 64 |
+
Django's generic views really shine when it comes to presenting views of your
|
| 65 |
+
database content. Because it's such a common task, Django comes with a handful
|
| 66 |
+
of built-in generic views to help generate list and detail views of objects.
|
| 67 |
+
|
| 68 |
+
Let's start by looking at some examples of showing a list of objects or an
|
| 69 |
+
individual object.
|
| 70 |
+
|
| 71 |
+
We'll be using these models::
|
| 72 |
+
|
| 73 |
+
# models.py
|
| 74 |
+
from django.db import models
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class Publisher(models.Model):
|
| 78 |
+
name = models.CharField(max_length=30)
|
| 79 |
+
address = models.CharField(max_length=50)
|
| 80 |
+
city = models.CharField(max_length=60)
|
| 81 |
+
state_province = models.CharField(max_length=30)
|
| 82 |
+
country = models.CharField(max_length=50)
|
| 83 |
+
website = models.URLField()
|
| 84 |
+
|
| 85 |
+
class Meta:
|
| 86 |
+
ordering = ["-name"]
|
| 87 |
+
|
| 88 |
+
def __str__(self):
|
| 89 |
+
return self.name
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class Author(models.Model):
|
| 93 |
+
salutation = models.CharField(max_length=10)
|
| 94 |
+
name = models.CharField(max_length=200)
|
| 95 |
+
email = models.EmailField()
|
| 96 |
+
headshot = models.ImageField(upload_to="author_headshots")
|
| 97 |
+
|
| 98 |
+
def __str__(self):
|
| 99 |
+
return self.name
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class Book(models.Model):
|
| 103 |
+
title = models.CharField(max_length=100)
|
| 104 |
+
authors = models.ManyToManyField("Author")
|
| 105 |
+
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
|
| 106 |
+
publication_date = models.DateField()
|
| 107 |
+
|
| 108 |
+
Now we need to define a view::
|
| 109 |
+
|
| 110 |
+
# views.py
|
| 111 |
+
from django.views.generic import ListView
|
| 112 |
+
from books.models import Publisher
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
class PublisherListView(ListView):
|
| 116 |
+
model = Publisher
|
| 117 |
+
|
| 118 |
+
Finally hook that view into your urls::
|
| 119 |
+
|
| 120 |
+
# urls.py
|
| 121 |
+
from django.urls import path
|
| 122 |
+
from books.views import PublisherListView
|
| 123 |
+
|
| 124 |
+
urlpatterns = [
|
| 125 |
+
path("publishers/", PublisherListView.as_view()),
|
| 126 |
+
]
|
| 127 |
+
|
| 128 |
+
That's all the Python code we need to write. We still need to write a template,
|
| 129 |
+
however. We could explicitly tell the view which template to use by adding a
|
| 130 |
+
``template_name`` attribute to the view, but in the absence of an explicit
|
| 131 |
+
template Django will infer one from the object's name. In this case, the
|
| 132 |
+
inferred template will be ``"books/publisher_list.html"`` -- the "books" part
|
| 133 |
+
comes from the name of the app that defines the model, while the "publisher"
|
| 134 |
+
bit is the lowercased version of the model's name.
|
| 135 |
+
|
| 136 |
+
.. note::
|
| 137 |
+
|
| 138 |
+
Thus, when (for example) the ``APP_DIRS`` option of a ``DjangoTemplates``
|
| 139 |
+
backend is set to True in :setting:`TEMPLATES`, a template location could
|
| 140 |
+
be: /path/to/project/books/templates/books/publisher_list.html
|
| 141 |
+
|
| 142 |
+
This template will be rendered against a context containing a variable called
|
| 143 |
+
``object_list`` that contains all the publisher objects. A template might look
|
| 144 |
+
like this:
|
| 145 |
+
|
| 146 |
+
.. code-block:: html+django
|
| 147 |
+
|
| 148 |
+
{% extends "base.html" %}
|
| 149 |
+
|
| 150 |
+
{% block content %}
|
| 151 |
+
<h2>Publishers</h2>
|
| 152 |
+
<ul>
|
| 153 |
+
{% for publisher in object_list %}
|
| 154 |
+
<li>{{ publisher.name }}</li>
|
| 155 |
+
{% endfor %}
|
| 156 |
+
</ul>
|
| 157 |
+
{% endblock %}
|
| 158 |
+
|
| 159 |
+
That's really all there is to it. All the cool features of generic views come
|
| 160 |
+
from changing the attributes set on the generic view. The
|
| 161 |
+
:doc:`generic views reference</ref/class-based-views/index>` documents all the
|
| 162 |
+
generic views and their options in detail; the rest of this document will
|
| 163 |
+
consider some of the common ways you might customize and extend generic views.
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
Making "friendly" template contexts
|
| 167 |
+
-----------------------------------
|
| 168 |
+
|
| 169 |
+
You might have noticed that our sample publisher list template stores all the
|
| 170 |
+
publishers in a variable named ``object_list``. While this works just fine, it
|
| 171 |
+
isn't all that "friendly" to template authors: they have to "just know" that
|
| 172 |
+
they're dealing with publishers here.
|
| 173 |
+
|
| 174 |
+
Well, if you're dealing with a model object, this is already done for you. When
|
| 175 |
+
you are dealing with an object or queryset, Django is able to populate the
|
| 176 |
+
context using the lowercased version of the model class' name. This is provided
|
| 177 |
+
in addition to the default ``object_list`` entry, but contains exactly the same
|
| 178 |
+
data, i.e. ``publisher_list``.
|
| 179 |
+
|
| 180 |
+
If this still isn't a good match, you can manually set the name of the
|
| 181 |
+
context variable. The ``context_object_name`` attribute on a generic view
|
| 182 |
+
specifies the context variable to use::
|
| 183 |
+
|
| 184 |
+
# views.py
|
| 185 |
+
from django.views.generic import ListView
|
| 186 |
+
from books.models import Publisher
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
class PublisherListView(ListView):
|
| 190 |
+
model = Publisher
|
| 191 |
+
context_object_name = "my_favorite_publishers"
|
| 192 |
+
|
| 193 |
+
Providing a useful ``context_object_name`` is always a good idea. Your
|
| 194 |
+
coworkers who design templates will thank you.
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
.. _adding-extra-context:
|
| 198 |
+
|
| 199 |
+
Adding extra context
|
| 200 |
+
--------------------
|
| 201 |
+
|
| 202 |
+
Often you need to present some extra information beyond that provided by the
|
| 203 |
+
generic view. For example, think of showing a list of all the books on each
|
| 204 |
+
publisher detail page. The :class:`~django.views.generic.detail.DetailView`
|
| 205 |
+
generic view provides the publisher to the context, but how do we get
|
| 206 |
+
additional information in that template?
|
| 207 |
+
|
| 208 |
+
The answer is to subclass :class:`~django.views.generic.detail.DetailView`
|
| 209 |
+
and provide your own implementation of the ``get_context_data`` method.
|
| 210 |
+
The default implementation adds the object being displayed to the template, but
|
| 211 |
+
you can override it to send more::
|
| 212 |
+
|
| 213 |
+
from django.views.generic import DetailView
|
| 214 |
+
from books.models import Book, Publisher
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
class PublisherDetailView(DetailView):
|
| 218 |
+
model = Publisher
|
| 219 |
+
|
| 220 |
+
def get_context_data(self, **kwargs):
|
| 221 |
+
# Call the base implementation first to get a context
|
| 222 |
+
context = super().get_context_data(**kwargs)
|
| 223 |
+
# Add in a QuerySet of all the books
|
| 224 |
+
context["book_list"] = Book.objects.all()
|
| 225 |
+
return context
|
| 226 |
+
|
| 227 |
+
.. note::
|
| 228 |
+
|
| 229 |
+
Generally, ``get_context_data`` will merge the context data of all parent
|
| 230 |
+
classes with those of the current class. To preserve this behavior in your
|
| 231 |
+
own classes where you want to alter the context, you should be sure to call
|
| 232 |
+
``get_context_data`` on the super class. When no two classes try to define the
|
| 233 |
+
same key, this will give the expected results. However if any class
|
| 234 |
+
attempts to override a key after parent classes have set it (after the call
|
| 235 |
+
to super), any children of that class will also need to explicitly set it
|
| 236 |
+
after super if they want to be sure to override all parents. If you're
|
| 237 |
+
having trouble, review the method resolution order of your view.
|
| 238 |
+
|
| 239 |
+
Another consideration is that the context data from class-based generic
|
| 240 |
+
views will override data provided by context processors; see
|
| 241 |
+
:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_data` for
|
| 242 |
+
an example.
|
| 243 |
+
|
| 244 |
+
.. _generic-views-list-subsets:
|
| 245 |
+
|
| 246 |
+
Viewing subsets of objects
|
| 247 |
+
--------------------------
|
| 248 |
+
|
| 249 |
+
Now let's take a closer look at the ``model`` argument we've been
|
| 250 |
+
using all along. The ``model`` argument, which specifies the database
|
| 251 |
+
model that the view will operate upon, is available on all the
|
| 252 |
+
generic views that operate on a single object or a collection of
|
| 253 |
+
objects. However, the ``model`` argument is not the only way to
|
| 254 |
+
specify the objects that the view will operate upon -- you can also
|
| 255 |
+
specify the list of objects using the ``queryset`` argument::
|
| 256 |
+
|
| 257 |
+
from django.views.generic import DetailView
|
| 258 |
+
from books.models import Publisher
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
class PublisherDetailView(DetailView):
|
| 262 |
+
context_object_name = "publisher"
|
| 263 |
+
queryset = Publisher.objects.all()
|
| 264 |
+
|
| 265 |
+
Specifying ``model = Publisher`` is shorthand for saying ``queryset =
|
| 266 |
+
Publisher.objects.all()``. However, by using ``queryset`` to define a filtered
|
| 267 |
+
list of objects you can be more specific about the objects that will be visible
|
| 268 |
+
in the view (see :doc:`/topics/db/queries` for more information about
|
| 269 |
+
:class:`~django.db.models.query.QuerySet` objects, and see the
|
| 270 |
+
:doc:`class-based views reference </ref/class-based-views/index>` for the
|
| 271 |
+
complete details).
|
| 272 |
+
|
| 273 |
+
To pick an example, we might want to order a list of books by publication date,
|
| 274 |
+
with the most recent first::
|
| 275 |
+
|
| 276 |
+
from django.views.generic import ListView
|
| 277 |
+
from books.models import Book
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
class BookListView(ListView):
|
| 281 |
+
queryset = Book.objects.order_by("-publication_date")
|
| 282 |
+
context_object_name = "book_list"
|
| 283 |
+
|
| 284 |
+
That's a pretty minimal example, but it illustrates the idea nicely. You'll
|
| 285 |
+
usually want to do more than just reorder objects. If you want to present a
|
| 286 |
+
list of books by a particular publisher, you can use the same technique::
|
| 287 |
+
|
| 288 |
+
from django.views.generic import ListView
|
| 289 |
+
from books.models import Book
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
class AcmeBookListView(ListView):
|
| 293 |
+
context_object_name = "book_list"
|
| 294 |
+
queryset = Book.objects.filter(publisher__name="ACME Publishing")
|
| 295 |
+
template_name = "books/acme_list.html"
|
| 296 |
+
|
| 297 |
+
Notice that along with a filtered ``queryset``, we're also using a custom
|
| 298 |
+
template name. If we didn't, the generic view would use the same template as the
|
| 299 |
+
"vanilla" object list, which might not be what we want.
|
| 300 |
+
|
| 301 |
+
Also notice that this isn't a very elegant way of doing publisher-specific
|
| 302 |
+
books. If we want to add another publisher page, we'd need another handful of
|
| 303 |
+
lines in the URLconf, and more than a few publishers would get unreasonable.
|
| 304 |
+
We'll deal with this problem in the next section.
|
| 305 |
+
|
| 306 |
+
.. note::
|
| 307 |
+
|
| 308 |
+
If you get a 404 when requesting ``/books/acme/``, check to ensure you
|
| 309 |
+
actually have a Publisher with the name 'ACME Publishing'. Generic
|
| 310 |
+
views have an ``allow_empty`` parameter for this case. See the
|
| 311 |
+
:doc:`class-based-views reference</ref/class-based-views/index>` for more
|
| 312 |
+
details.
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
Dynamic filtering
|
| 316 |
+
-----------------
|
| 317 |
+
|
| 318 |
+
Another common need is to filter down the objects given in a list page by some
|
| 319 |
+
key in the URL. Earlier we hard-coded the publisher's name in the URLconf, but
|
| 320 |
+
what if we wanted to write a view that displayed all the books by some arbitrary
|
| 321 |
+
publisher?
|
| 322 |
+
|
| 323 |
+
Handily, the ``ListView`` has a
|
| 324 |
+
:meth:`~django.views.generic.list.MultipleObjectMixin.get_queryset` method we
|
| 325 |
+
can override. By default, it returns the value of the ``queryset`` attribute,
|
| 326 |
+
but we can use it to add more logic.
|
| 327 |
+
|
| 328 |
+
The key part to making this work is that when class-based views are called,
|
| 329 |
+
various useful things are stored on ``self``; as well as the request
|
| 330 |
+
(``self.request``) this includes the positional (``self.args``) and name-based
|
| 331 |
+
(``self.kwargs``) arguments captured according to the URLconf.
|
| 332 |
+
|
| 333 |
+
Here, we have a URLconf with a single captured group::
|
| 334 |
+
|
| 335 |
+
# urls.py
|
| 336 |
+
from django.urls import path
|
| 337 |
+
from books.views import PublisherBookListView
|
| 338 |
+
|
| 339 |
+
urlpatterns = [
|
| 340 |
+
path("books/<publisher>/", PublisherBookListView.as_view()),
|
| 341 |
+
]
|
| 342 |
+
|
| 343 |
+
Next, we'll write the ``PublisherBookListView`` view itself::
|
| 344 |
+
|
| 345 |
+
# views.py
|
| 346 |
+
from django.shortcuts import get_object_or_404
|
| 347 |
+
from django.views.generic import ListView
|
| 348 |
+
from books.models import Book, Publisher
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
class PublisherBookListView(ListView):
|
| 352 |
+
template_name = "books/books_by_publisher.html"
|
| 353 |
+
|
| 354 |
+
def get_queryset(self):
|
| 355 |
+
self.publisher = get_object_or_404(Publisher, name=self.kwargs["publisher"])
|
| 356 |
+
return Book.objects.filter(publisher=self.publisher)
|
| 357 |
+
|
| 358 |
+
Using ``get_queryset`` to add logic to the queryset selection is as convenient
|
| 359 |
+
as it is powerful. For instance, if we wanted, we could use
|
| 360 |
+
``self.request.user`` to filter using the current user, or other more complex
|
| 361 |
+
logic.
|
| 362 |
+
|
| 363 |
+
We can also add the publisher into the context at the same time, so we can
|
| 364 |
+
use it in the template::
|
| 365 |
+
|
| 366 |
+
# ...
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
def get_context_data(self, **kwargs):
|
| 370 |
+
# Call the base implementation first to get a context
|
| 371 |
+
context = super().get_context_data(**kwargs)
|
| 372 |
+
# Add in the publisher
|
| 373 |
+
context["publisher"] = self.publisher
|
| 374 |
+
return context
|
| 375 |
+
|
| 376 |
+
.. _generic-views-extra-work:
|
| 377 |
+
|
| 378 |
+
Performing extra work
|
| 379 |
+
---------------------
|
| 380 |
+
|
| 381 |
+
The last common pattern we'll look at involves doing some extra work before
|
| 382 |
+
or after calling the generic view.
|
| 383 |
+
|
| 384 |
+
Imagine we had a ``last_accessed`` field on our ``Author`` model that we were
|
| 385 |
+
using to keep track of the last time anybody looked at that author::
|
| 386 |
+
|
| 387 |
+
# models.py
|
| 388 |
+
from django.db import models
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
class Author(models.Model):
|
| 392 |
+
salutation = models.CharField(max_length=10)
|
| 393 |
+
name = models.CharField(max_length=200)
|
| 394 |
+
email = models.EmailField()
|
| 395 |
+
headshot = models.ImageField(upload_to="author_headshots")
|
| 396 |
+
last_accessed = models.DateTimeField()
|
| 397 |
+
|
| 398 |
+
The generic ``DetailView`` class wouldn't know anything about this field, but
|
| 399 |
+
once again we could write a custom view to keep that field updated.
|
| 400 |
+
|
| 401 |
+
First, we'd need to add an author detail bit in the URLconf to point to a
|
| 402 |
+
custom view::
|
| 403 |
+
|
| 404 |
+
from django.urls import path
|
| 405 |
+
from books.views import AuthorDetailView
|
| 406 |
+
|
| 407 |
+
urlpatterns = [
|
| 408 |
+
# ...
|
| 409 |
+
path("authors/<int:pk>/", AuthorDetailView.as_view(), name="author-detail"),
|
| 410 |
+
]
|
| 411 |
+
|
| 412 |
+
Then we'd write our new view -- ``get_object`` is the method that retrieves the
|
| 413 |
+
object -- so we override it and wrap the call::
|
| 414 |
+
|
| 415 |
+
from django.utils import timezone
|
| 416 |
+
from django.views.generic import DetailView
|
| 417 |
+
from books.models import Author
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
class AuthorDetailView(DetailView):
|
| 421 |
+
queryset = Author.objects.all()
|
| 422 |
+
|
| 423 |
+
def get_object(self):
|
| 424 |
+
obj = super().get_object()
|
| 425 |
+
# Record the last accessed date
|
| 426 |
+
obj.last_accessed = timezone.now()
|
| 427 |
+
obj.save()
|
| 428 |
+
return obj
|
| 429 |
+
|
| 430 |
+
.. note::
|
| 431 |
+
|
| 432 |
+
The URLconf here uses the named group ``pk`` - this name is the default
|
| 433 |
+
name that ``DetailView`` uses to find the value of the primary key used to
|
| 434 |
+
filter the queryset.
|
| 435 |
+
|
| 436 |
+
If you want to call the group something else, you can set
|
| 437 |
+
:attr:`~django.views.generic.detail.SingleObjectMixin.pk_url_kwarg`
|
| 438 |
+
on the view.
|
testbed/django__django/docs/topics/class-based-views/generic-editing.txt
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
====================================
|
| 2 |
+
Form handling with class-based views
|
| 3 |
+
====================================
|
| 4 |
+
|
| 5 |
+
Form processing generally has 3 paths:
|
| 6 |
+
|
| 7 |
+
* Initial GET (blank or prepopulated form)
|
| 8 |
+
* POST with invalid data (typically redisplay form with errors)
|
| 9 |
+
* POST with valid data (process the data and typically redirect)
|
| 10 |
+
|
| 11 |
+
Implementing this yourself often results in a lot of repeated boilerplate code
|
| 12 |
+
(see :ref:`Using a form in a view<using-a-form-in-a-view>`). To help avoid
|
| 13 |
+
this, Django provides a collection of generic class-based views for form
|
| 14 |
+
processing.
|
| 15 |
+
|
| 16 |
+
Basic forms
|
| 17 |
+
===========
|
| 18 |
+
|
| 19 |
+
Given a contact form:
|
| 20 |
+
|
| 21 |
+
.. code-block:: python
|
| 22 |
+
:caption: ``forms.py``
|
| 23 |
+
|
| 24 |
+
from django import forms
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class ContactForm(forms.Form):
|
| 28 |
+
name = forms.CharField()
|
| 29 |
+
message = forms.CharField(widget=forms.Textarea)
|
| 30 |
+
|
| 31 |
+
def send_email(self):
|
| 32 |
+
# send email using the self.cleaned_data dictionary
|
| 33 |
+
pass
|
| 34 |
+
|
| 35 |
+
The view can be constructed using a ``FormView``:
|
| 36 |
+
|
| 37 |
+
.. code-block:: python
|
| 38 |
+
:caption: ``views.py``
|
| 39 |
+
|
| 40 |
+
from myapp.forms import ContactForm
|
| 41 |
+
from django.views.generic.edit import FormView
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class ContactFormView(FormView):
|
| 45 |
+
template_name = "contact.html"
|
| 46 |
+
form_class = ContactForm
|
| 47 |
+
success_url = "/thanks/"
|
| 48 |
+
|
| 49 |
+
def form_valid(self, form):
|
| 50 |
+
# This method is called when valid form data has been POSTed.
|
| 51 |
+
# It should return an HttpResponse.
|
| 52 |
+
form.send_email()
|
| 53 |
+
return super().form_valid(form)
|
| 54 |
+
|
| 55 |
+
Notes:
|
| 56 |
+
|
| 57 |
+
* FormView inherits
|
| 58 |
+
:class:`~django.views.generic.base.TemplateResponseMixin` so
|
| 59 |
+
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
|
| 60 |
+
can be used here.
|
| 61 |
+
* The default implementation for
|
| 62 |
+
:meth:`~django.views.generic.edit.FormMixin.form_valid` simply
|
| 63 |
+
redirects to the :attr:`~django.views.generic.edit.FormMixin.success_url`.
|
| 64 |
+
|
| 65 |
+
Model forms
|
| 66 |
+
===========
|
| 67 |
+
|
| 68 |
+
Generic views really shine when working with models. These generic
|
| 69 |
+
views will automatically create a :class:`~django.forms.ModelForm`, so long as
|
| 70 |
+
they can work out which model class to use:
|
| 71 |
+
|
| 72 |
+
* If the :attr:`~django.views.generic.edit.ModelFormMixin.model` attribute is
|
| 73 |
+
given, that model class will be used.
|
| 74 |
+
* If :meth:`~django.views.generic.detail.SingleObjectMixin.get_object()`
|
| 75 |
+
returns an object, the class of that object will be used.
|
| 76 |
+
* If a :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` is
|
| 77 |
+
given, the model for that queryset will be used.
|
| 78 |
+
|
| 79 |
+
Model form views provide a
|
| 80 |
+
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` implementation
|
| 81 |
+
that saves the model automatically. You can override this if you have any
|
| 82 |
+
special requirements; see below for examples.
|
| 83 |
+
|
| 84 |
+
You don't even need to provide a ``success_url`` for
|
| 85 |
+
:class:`~django.views.generic.edit.CreateView` or
|
| 86 |
+
:class:`~django.views.generic.edit.UpdateView` - they will use
|
| 87 |
+
:meth:`~django.db.models.Model.get_absolute_url()` on the model object if available.
|
| 88 |
+
|
| 89 |
+
If you want to use a custom :class:`~django.forms.ModelForm` (for instance to
|
| 90 |
+
add extra validation), set
|
| 91 |
+
:attr:`~django.views.generic.edit.FormMixin.form_class` on your view.
|
| 92 |
+
|
| 93 |
+
.. note::
|
| 94 |
+
When specifying a custom form class, you must still specify the model,
|
| 95 |
+
even though the :attr:`~django.views.generic.edit.FormMixin.form_class` may
|
| 96 |
+
be a :class:`~django.forms.ModelForm`.
|
| 97 |
+
|
| 98 |
+
First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
|
| 99 |
+
``Author`` class:
|
| 100 |
+
|
| 101 |
+
.. code-block:: python
|
| 102 |
+
:caption: ``models.py``
|
| 103 |
+
|
| 104 |
+
from django.db import models
|
| 105 |
+
from django.urls import reverse
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
class Author(models.Model):
|
| 109 |
+
name = models.CharField(max_length=200)
|
| 110 |
+
|
| 111 |
+
def get_absolute_url(self):
|
| 112 |
+
return reverse("author-detail", kwargs={"pk": self.pk})
|
| 113 |
+
|
| 114 |
+
Then we can use :class:`CreateView` and friends to do the actual
|
| 115 |
+
work. Notice how we're just configuring the generic class-based views
|
| 116 |
+
here; we don't have to write any logic ourselves:
|
| 117 |
+
|
| 118 |
+
.. code-block:: python
|
| 119 |
+
:caption: ``views.py``
|
| 120 |
+
|
| 121 |
+
from django.urls import reverse_lazy
|
| 122 |
+
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
| 123 |
+
from myapp.models import Author
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class AuthorCreateView(CreateView):
|
| 127 |
+
model = Author
|
| 128 |
+
fields = ["name"]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class AuthorUpdateView(UpdateView):
|
| 132 |
+
model = Author
|
| 133 |
+
fields = ["name"]
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
class AuthorDeleteView(DeleteView):
|
| 137 |
+
model = Author
|
| 138 |
+
success_url = reverse_lazy("author-list")
|
| 139 |
+
|
| 140 |
+
.. note::
|
| 141 |
+
We have to use :func:`~django.urls.reverse_lazy` instead of
|
| 142 |
+
``reverse()``, as the urls are not loaded when the file is imported.
|
| 143 |
+
|
| 144 |
+
The ``fields`` attribute works the same way as the ``fields`` attribute on the
|
| 145 |
+
inner ``Meta`` class on :class:`~django.forms.ModelForm`. Unless you define the
|
| 146 |
+
form class in another way, the attribute is required and the view will raise
|
| 147 |
+
an :exc:`~django.core.exceptions.ImproperlyConfigured` exception if it's not.
|
| 148 |
+
|
| 149 |
+
If you specify both the :attr:`~django.views.generic.edit.ModelFormMixin.fields`
|
| 150 |
+
and :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
|
| 151 |
+
:exc:`~django.core.exceptions.ImproperlyConfigured` exception will be raised.
|
| 152 |
+
|
| 153 |
+
Finally, we hook these new views into the URLconf:
|
| 154 |
+
|
| 155 |
+
.. code-block:: python
|
| 156 |
+
:caption: ``urls.py``
|
| 157 |
+
|
| 158 |
+
from django.urls import path
|
| 159 |
+
from myapp.views import AuthorCreateView, AuthorDeleteView, AuthorUpdateView
|
| 160 |
+
|
| 161 |
+
urlpatterns = [
|
| 162 |
+
# ...
|
| 163 |
+
path("author/add/", AuthorCreateView.as_view(), name="author-add"),
|
| 164 |
+
path("author/<int:pk>/", AuthorUpdateView.as_view(), name="author-update"),
|
| 165 |
+
path("author/<int:pk>/delete/", AuthorDeleteView.as_view(), name="author-delete"),
|
| 166 |
+
]
|
| 167 |
+
|
| 168 |
+
.. note::
|
| 169 |
+
|
| 170 |
+
These views inherit
|
| 171 |
+
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
| 172 |
+
which uses
|
| 173 |
+
:attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
|
| 174 |
+
to construct the
|
| 175 |
+
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
|
| 176 |
+
based on the model.
|
| 177 |
+
|
| 178 |
+
In this example:
|
| 179 |
+
|
| 180 |
+
* :class:`CreateView` and :class:`UpdateView` use ``myapp/author_form.html``
|
| 181 |
+
* :class:`DeleteView` uses ``myapp/author_confirm_delete.html``
|
| 182 |
+
|
| 183 |
+
If you wish to have separate templates for :class:`CreateView` and
|
| 184 |
+
:class:`UpdateView`, you can set either
|
| 185 |
+
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name` or
|
| 186 |
+
:attr:`~django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix`
|
| 187 |
+
on your view class.
|
| 188 |
+
|
| 189 |
+
Models and ``request.user``
|
| 190 |
+
===========================
|
| 191 |
+
|
| 192 |
+
To track the user that created an object using a :class:`CreateView`,
|
| 193 |
+
you can use a custom :class:`~django.forms.ModelForm` to do this. First, add
|
| 194 |
+
the foreign key relation to the model:
|
| 195 |
+
|
| 196 |
+
.. code-block:: python
|
| 197 |
+
:caption: ``models.py``
|
| 198 |
+
|
| 199 |
+
from django.contrib.auth.models import User
|
| 200 |
+
from django.db import models
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
class Author(models.Model):
|
| 204 |
+
name = models.CharField(max_length=200)
|
| 205 |
+
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
| 206 |
+
|
| 207 |
+
# ...
|
| 208 |
+
|
| 209 |
+
In the view, ensure that you don't include ``created_by`` in the list of fields
|
| 210 |
+
to edit, and override
|
| 211 |
+
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` to add the user:
|
| 212 |
+
|
| 213 |
+
.. code-block:: python
|
| 214 |
+
:caption: ``views.py``
|
| 215 |
+
|
| 216 |
+
from django.contrib.auth.mixins import LoginRequiredMixin
|
| 217 |
+
from django.views.generic.edit import CreateView
|
| 218 |
+
from myapp.models import Author
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
class AuthorCreateView(LoginRequiredMixin, CreateView):
|
| 222 |
+
model = Author
|
| 223 |
+
fields = ["name"]
|
| 224 |
+
|
| 225 |
+
def form_valid(self, form):
|
| 226 |
+
form.instance.created_by = self.request.user
|
| 227 |
+
return super().form_valid(form)
|
| 228 |
+
|
| 229 |
+
:class:`~django.contrib.auth.mixins.LoginRequiredMixin` prevents users who
|
| 230 |
+
aren't logged in from accessing the form. If you omit that, you'll need to
|
| 231 |
+
handle unauthorized users in :meth:`~.ModelFormMixin.form_valid()`.
|
| 232 |
+
|
| 233 |
+
.. _content-negotiation-example:
|
| 234 |
+
|
| 235 |
+
Content negotiation example
|
| 236 |
+
===========================
|
| 237 |
+
|
| 238 |
+
Here is an example showing how you might go about implementing a form that
|
| 239 |
+
works with an API-based workflow as well as 'normal' form POSTs::
|
| 240 |
+
|
| 241 |
+
from django.http import JsonResponse
|
| 242 |
+
from django.views.generic.edit import CreateView
|
| 243 |
+
from myapp.models import Author
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
class JsonableResponseMixin:
|
| 247 |
+
"""
|
| 248 |
+
Mixin to add JSON support to a form.
|
| 249 |
+
Must be used with an object-based FormView (e.g. CreateView)
|
| 250 |
+
"""
|
| 251 |
+
|
| 252 |
+
def form_invalid(self, form):
|
| 253 |
+
response = super().form_invalid(form)
|
| 254 |
+
if self.request.accepts("text/html"):
|
| 255 |
+
return response
|
| 256 |
+
else:
|
| 257 |
+
return JsonResponse(form.errors, status=400)
|
| 258 |
+
|
| 259 |
+
def form_valid(self, form):
|
| 260 |
+
# We make sure to call the parent's form_valid() method because
|
| 261 |
+
# it might do some processing (in the case of CreateView, it will
|
| 262 |
+
# call form.save() for example).
|
| 263 |
+
response = super().form_valid(form)
|
| 264 |
+
if self.request.accepts("text/html"):
|
| 265 |
+
return response
|
| 266 |
+
else:
|
| 267 |
+
data = {
|
| 268 |
+
"pk": self.object.pk,
|
| 269 |
+
}
|
| 270 |
+
return JsonResponse(data)
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
class AuthorCreateView(JsonableResponseMixin, CreateView):
|
| 274 |
+
model = Author
|
| 275 |
+
fields = ["name"]
|
testbed/django__django/docs/topics/class-based-views/index.txt
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
=================
|
| 2 |
+
Class-based views
|
| 3 |
+
=================
|
| 4 |
+
|
| 5 |
+
A view is a callable which takes a request and returns a
|
| 6 |
+
response. This can be more than just a function, and Django provides
|
| 7 |
+
an example of some classes which can be used as views. These allow you
|
| 8 |
+
to structure your views and reuse code by harnessing inheritance and
|
| 9 |
+
mixins. There are also some generic views for tasks which we'll get to later,
|
| 10 |
+
but you may want to design your own structure of reusable views which suits
|
| 11 |
+
your use case. For full details, see the :doc:`class-based views reference
|
| 12 |
+
documentation</ref/class-based-views/index>`.
|
| 13 |
+
|
| 14 |
+
.. toctree::
|
| 15 |
+
:maxdepth: 1
|
| 16 |
+
|
| 17 |
+
intro
|
| 18 |
+
generic-display
|
| 19 |
+
generic-editing
|
| 20 |
+
mixins
|
| 21 |
+
|
| 22 |
+
Basic examples
|
| 23 |
+
==============
|
| 24 |
+
|
| 25 |
+
Django provides base view classes which will suit a wide range of applications.
|
| 26 |
+
All views inherit from the :class:`~django.views.generic.base.View` class, which
|
| 27 |
+
handles linking the view into the URLs, HTTP method dispatching and other
|
| 28 |
+
common features. :class:`~django.views.generic.base.RedirectView` provides a
|
| 29 |
+
HTTP redirect, and :class:`~django.views.generic.base.TemplateView` extends the
|
| 30 |
+
base class to make it also render a template.
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
Usage in your URLconf
|
| 34 |
+
=====================
|
| 35 |
+
|
| 36 |
+
The most direct way to use generic views is to create them directly in your
|
| 37 |
+
URLconf. If you're only changing a few attributes on a class-based view, you
|
| 38 |
+
can pass them into the :meth:`~django.views.generic.base.View.as_view` method
|
| 39 |
+
call itself::
|
| 40 |
+
|
| 41 |
+
from django.urls import path
|
| 42 |
+
from django.views.generic import TemplateView
|
| 43 |
+
|
| 44 |
+
urlpatterns = [
|
| 45 |
+
path("about/", TemplateView.as_view(template_name="about.html")),
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
Any arguments passed to :meth:`~django.views.generic.base.View.as_view` will
|
| 49 |
+
override attributes set on the class. In this example, we set ``template_name``
|
| 50 |
+
on the ``TemplateView``. A similar overriding pattern can be used for the
|
| 51 |
+
``url`` attribute on :class:`~django.views.generic.base.RedirectView`.
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
Subclassing generic views
|
| 55 |
+
=========================
|
| 56 |
+
|
| 57 |
+
The second, more powerful way to use generic views is to inherit from an
|
| 58 |
+
existing view and override attributes (such as the ``template_name``) or
|
| 59 |
+
methods (such as ``get_context_data``) in your subclass to provide new values
|
| 60 |
+
or methods. Consider, for example, a view that just displays one template,
|
| 61 |
+
``about.html``. Django has a generic view to do this -
|
| 62 |
+
:class:`~django.views.generic.base.TemplateView` - so we can subclass it, and
|
| 63 |
+
override the template name::
|
| 64 |
+
|
| 65 |
+
# some_app/views.py
|
| 66 |
+
from django.views.generic import TemplateView
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class AboutView(TemplateView):
|
| 70 |
+
template_name = "about.html"
|
| 71 |
+
|
| 72 |
+
Then we need to add this new view into our URLconf.
|
| 73 |
+
:class:`~django.views.generic.base.TemplateView` is a class, not a function, so
|
| 74 |
+
we point the URL to the :meth:`~django.views.generic.base.View.as_view` class
|
| 75 |
+
method instead, which provides a function-like entry to class-based views::
|
| 76 |
+
|
| 77 |
+
# urls.py
|
| 78 |
+
from django.urls import path
|
| 79 |
+
from some_app.views import AboutView
|
| 80 |
+
|
| 81 |
+
urlpatterns = [
|
| 82 |
+
path("about/", AboutView.as_view()),
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
For more information on how to use the built in generic views, consult the next
|
| 87 |
+
topic on :doc:`generic class-based views</topics/class-based-views/generic-display>`.
|
| 88 |
+
|
| 89 |
+
.. _supporting-other-http-methods:
|
| 90 |
+
|
| 91 |
+
Supporting other HTTP methods
|
| 92 |
+
-----------------------------
|
| 93 |
+
|
| 94 |
+
Suppose somebody wants to access our book library over HTTP using the views
|
| 95 |
+
as an API. The API client would connect every now and then and download book
|
| 96 |
+
data for the books published since last visit. But if no new books appeared
|
| 97 |
+
since then, it is a waste of CPU time and bandwidth to fetch the books from the
|
| 98 |
+
database, render a full response and send it to the client. It might be
|
| 99 |
+
preferable to ask the API when the most recent book was published.
|
| 100 |
+
|
| 101 |
+
We map the URL to book list view in the URLconf::
|
| 102 |
+
|
| 103 |
+
from django.urls import path
|
| 104 |
+
from books.views import BookListView
|
| 105 |
+
|
| 106 |
+
urlpatterns = [
|
| 107 |
+
path("books/", BookListView.as_view()),
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
And the view::
|
| 111 |
+
|
| 112 |
+
from django.http import HttpResponse
|
| 113 |
+
from django.views.generic import ListView
|
| 114 |
+
from books.models import Book
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
class BookListView(ListView):
|
| 118 |
+
model = Book
|
| 119 |
+
|
| 120 |
+
def head(self, *args, **kwargs):
|
| 121 |
+
last_book = self.get_queryset().latest("publication_date")
|
| 122 |
+
response = HttpResponse(
|
| 123 |
+
# RFC 1123 date format.
|
| 124 |
+
headers={
|
| 125 |
+
"Last-Modified": last_book.publication_date.strftime(
|
| 126 |
+
"%a, %d %b %Y %H:%M:%S GMT"
|
| 127 |
+
)
|
| 128 |
+
},
|
| 129 |
+
)
|
| 130 |
+
return response
|
| 131 |
+
|
| 132 |
+
If the view is accessed from a ``GET`` request, an object list is returned in
|
| 133 |
+
the response (using the ``book_list.html`` template). But if the client issues
|
| 134 |
+
a ``HEAD`` request, the response has an empty body and the ``Last-Modified``
|
| 135 |
+
header indicates when the most recent book was published. Based on this
|
| 136 |
+
information, the client may or may not download the full object list.
|
| 137 |
+
|
| 138 |
+
.. _async-class-based-views:
|
| 139 |
+
|
| 140 |
+
Asynchronous class-based views
|
| 141 |
+
==============================
|
| 142 |
+
|
| 143 |
+
As well as the synchronous (``def``) method handlers already shown, ``View``
|
| 144 |
+
subclasses may define asynchronous (``async def``) method handlers to leverage
|
| 145 |
+
asynchronous code using ``await``::
|
| 146 |
+
|
| 147 |
+
import asyncio
|
| 148 |
+
from django.http import HttpResponse
|
| 149 |
+
from django.views import View
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
class AsyncView(View):
|
| 153 |
+
async def get(self, request, *args, **kwargs):
|
| 154 |
+
# Perform io-blocking view logic using await, sleep for example.
|
| 155 |
+
await asyncio.sleep(1)
|
| 156 |
+
return HttpResponse("Hello async world!")
|
| 157 |
+
|
| 158 |
+
Within a single view-class, all user-defined method handlers must be either
|
| 159 |
+
synchronous, using ``def``, or all asynchronous, using ``async def``. An
|
| 160 |
+
``ImproperlyConfigured`` exception will be raised in ``as_view()`` if ``def``
|
| 161 |
+
and ``async def`` declarations are mixed.
|
| 162 |
+
|
| 163 |
+
Django will automatically detect asynchronous views and run them in an
|
| 164 |
+
asynchronous context. You can read more about Django's asynchronous support,
|
| 165 |
+
and how to best use async views, in :doc:`/topics/async`.
|
testbed/django__django/docs/topics/class-based-views/intro.txt
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
=================================
|
| 2 |
+
Introduction to class-based views
|
| 3 |
+
=================================
|
| 4 |
+
|
| 5 |
+
Class-based views provide an alternative way to implement views as Python
|
| 6 |
+
objects instead of functions. They do not replace function-based views, but
|
| 7 |
+
have certain differences and advantages when compared to function-based views:
|
| 8 |
+
|
| 9 |
+
* Organization of code related to specific HTTP methods (``GET``, ``POST``,
|
| 10 |
+
etc.) can be addressed by separate methods instead of conditional branching.
|
| 11 |
+
|
| 12 |
+
* Object oriented techniques such as mixins (multiple inheritance) can be
|
| 13 |
+
used to factor code into reusable components.
|
| 14 |
+
|
| 15 |
+
The relationship and history of generic views, class-based views, and class-based generic views
|
| 16 |
+
===============================================================================================
|
| 17 |
+
|
| 18 |
+
In the beginning there was only the view function contract, Django passed your
|
| 19 |
+
function an :class:`~django.http.HttpRequest` and expected back an
|
| 20 |
+
:class:`~django.http.HttpResponse`. This was the extent of what Django provided.
|
| 21 |
+
|
| 22 |
+
Early on it was recognized that there were common idioms and patterns found in
|
| 23 |
+
view development. Function-based generic views were introduced to abstract
|
| 24 |
+
these patterns and ease view development for the common cases.
|
| 25 |
+
|
| 26 |
+
The problem with function-based generic views is that while they covered the
|
| 27 |
+
simple cases well, there was no way to extend or customize them beyond some
|
| 28 |
+
configuration options, limiting their usefulness in many real-world
|
| 29 |
+
applications.
|
| 30 |
+
|
| 31 |
+
Class-based generic views were created with the same objective as
|
| 32 |
+
function-based generic views, to make view development easier. However, the way
|
| 33 |
+
the solution is implemented, through the use of mixins, provides a toolkit that
|
| 34 |
+
results in class-based generic views being more extensible and flexible than
|
| 35 |
+
their function-based counterparts.
|
| 36 |
+
|
| 37 |
+
If you have tried function based generic views in the past and found them
|
| 38 |
+
lacking, you should not think of class-based generic views as a class-based
|
| 39 |
+
equivalent, but rather as a fresh approach to solving the original problems
|
| 40 |
+
that generic views were meant to solve.
|
| 41 |
+
|
| 42 |
+
The toolkit of base classes and mixins that Django uses to build class-based
|
| 43 |
+
generic views are built for maximum flexibility, and as such have many hooks in
|
| 44 |
+
the form of default method implementations and attributes that you are unlikely
|
| 45 |
+
to be concerned with in the simplest use cases. For example, instead of
|
| 46 |
+
limiting you to a class-based attribute for ``form_class``, the implementation
|
| 47 |
+
uses a ``get_form`` method, which calls a ``get_form_class`` method, which in
|
| 48 |
+
its default implementation returns the ``form_class`` attribute of the class.
|
| 49 |
+
This gives you several options for specifying what form to use, from an
|
| 50 |
+
attribute, to a fully dynamic, callable hook. These options seem to add hollow
|
| 51 |
+
complexity for simple situations, but without them, more advanced designs would
|
| 52 |
+
be limited.
|
| 53 |
+
|
| 54 |
+
Using class-based views
|
| 55 |
+
=======================
|
| 56 |
+
|
| 57 |
+
At its core, a class-based view allows you to respond to different HTTP request
|
| 58 |
+
methods with different class instance methods, instead of with conditionally
|
| 59 |
+
branching code inside a single view function.
|
| 60 |
+
|
| 61 |
+
So where the code to handle HTTP ``GET`` in a view function would look
|
| 62 |
+
something like::
|
| 63 |
+
|
| 64 |
+
from django.http import HttpResponse
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def my_view(request):
|
| 68 |
+
if request.method == "GET":
|
| 69 |
+
# <view logic>
|
| 70 |
+
return HttpResponse("result")
|
| 71 |
+
|
| 72 |
+
In a class-based view, this would become::
|
| 73 |
+
|
| 74 |
+
from django.http import HttpResponse
|
| 75 |
+
from django.views import View
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class MyView(View):
|
| 79 |
+
def get(self, request):
|
| 80 |
+
# <view logic>
|
| 81 |
+
return HttpResponse("result")
|
| 82 |
+
|
| 83 |
+
Because Django's URL resolver expects to send the request and associated
|
| 84 |
+
arguments to a callable function, not a class, class-based views have an
|
| 85 |
+
:meth:`~django.views.generic.base.View.as_view` class method which returns a
|
| 86 |
+
function that can be called when a request arrives for a URL matching the
|
| 87 |
+
associated pattern. The function creates an instance of the class, calls
|
| 88 |
+
:meth:`~django.views.generic.base.View.setup` to initialize its attributes, and
|
| 89 |
+
then calls its :meth:`~django.views.generic.base.View.dispatch` method.
|
| 90 |
+
``dispatch`` looks at the request to determine whether it is a ``GET``,
|
| 91 |
+
``POST``, etc, and relays the request to a matching method if one is defined,
|
| 92 |
+
or raises :class:`~django.http.HttpResponseNotAllowed` if not::
|
| 93 |
+
|
| 94 |
+
# urls.py
|
| 95 |
+
from django.urls import path
|
| 96 |
+
from myapp.views import MyView
|
| 97 |
+
|
| 98 |
+
urlpatterns = [
|
| 99 |
+
path("about/", MyView.as_view()),
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
It is worth noting that what your method returns is identical to what you
|
| 104 |
+
return from a function-based view, namely some form of
|
| 105 |
+
:class:`~django.http.HttpResponse`. This means that
|
| 106 |
+
:doc:`http shortcuts </topics/http/shortcuts>` or
|
| 107 |
+
:class:`~django.template.response.TemplateResponse` objects are valid to use
|
| 108 |
+
inside a class-based view.
|
| 109 |
+
|
| 110 |
+
While a minimal class-based view does not require any class attributes to
|
| 111 |
+
perform its job, class attributes are useful in many class-based designs,
|
| 112 |
+
and there are two ways to configure or set class attributes.
|
| 113 |
+
|
| 114 |
+
The first is the standard Python way of subclassing and overriding attributes
|
| 115 |
+
and methods in the subclass. So that if your parent class had an attribute
|
| 116 |
+
``greeting`` like this::
|
| 117 |
+
|
| 118 |
+
from django.http import HttpResponse
|
| 119 |
+
from django.views import View
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
class GreetingView(View):
|
| 123 |
+
greeting = "Good Day"
|
| 124 |
+
|
| 125 |
+
def get(self, request):
|
| 126 |
+
return HttpResponse(self.greeting)
|
| 127 |
+
|
| 128 |
+
You can override that in a subclass::
|
| 129 |
+
|
| 130 |
+
class MorningGreetingView(GreetingView):
|
| 131 |
+
greeting = "Morning to ya"
|
| 132 |
+
|
| 133 |
+
Another option is to configure class attributes as keyword arguments to the
|
| 134 |
+
:meth:`~django.views.generic.base.View.as_view` call in the URLconf::
|
| 135 |
+
|
| 136 |
+
urlpatterns = [
|
| 137 |
+
path("about/", GreetingView.as_view(greeting="G'day")),
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
.. note::
|
| 141 |
+
|
| 142 |
+
While your class is instantiated for each request dispatched to it, class
|
| 143 |
+
attributes set through the
|
| 144 |
+
:meth:`~django.views.generic.base.View.as_view` entry point are
|
| 145 |
+
configured only once at the time your URLs are imported.
|
| 146 |
+
|
| 147 |
+
Using mixins
|
| 148 |
+
============
|
| 149 |
+
|
| 150 |
+
Mixins are a form of multiple inheritance where behaviors and attributes of
|
| 151 |
+
multiple parent classes can be combined.
|
| 152 |
+
|
| 153 |
+
For example, in the generic class-based views there is a mixin called
|
| 154 |
+
:class:`~django.views.generic.base.TemplateResponseMixin` whose primary purpose
|
| 155 |
+
is to define the method
|
| 156 |
+
:meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response`.
|
| 157 |
+
When combined with the behavior of the :class:`~django.views.generic.base.View`
|
| 158 |
+
base class, the result is a :class:`~django.views.generic.base.TemplateView`
|
| 159 |
+
class that will dispatch requests to the appropriate matching methods (a
|
| 160 |
+
behavior defined in the ``View`` base class), and that has a
|
| 161 |
+
:meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response`
|
| 162 |
+
method that uses a
|
| 163 |
+
:attr:`~django.views.generic.base.TemplateResponseMixin.template_name`
|
| 164 |
+
attribute to return a :class:`~django.template.response.TemplateResponse`
|
| 165 |
+
object (a behavior defined in the ``TemplateResponseMixin``).
|
| 166 |
+
|
| 167 |
+
Mixins are an excellent way of reusing code across multiple classes, but they
|
| 168 |
+
come with some cost. The more your code is scattered among mixins, the harder
|
| 169 |
+
it will be to read a child class and know what exactly it is doing, and the
|
| 170 |
+
harder it will be to know which methods from which mixins to override if you
|
| 171 |
+
are subclassing something that has a deep inheritance tree.
|
| 172 |
+
|
| 173 |
+
Note also that you can only inherit from one generic view - that is, only one
|
| 174 |
+
parent class may inherit from :class:`~django.views.generic.base.View` and
|
| 175 |
+
the rest (if any) should be mixins. Trying to inherit from more than one class
|
| 176 |
+
that inherits from ``View`` - for example, trying to use a form at the top of a
|
| 177 |
+
list and combining :class:`~django.views.generic.edit.ProcessFormView` and
|
| 178 |
+
:class:`~django.views.generic.list.ListView` - won't work as expected.
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
Handling forms with class-based views
|
| 182 |
+
=====================================
|
| 183 |
+
|
| 184 |
+
A basic function-based view that handles forms may look something like this::
|
| 185 |
+
|
| 186 |
+
from django.http import HttpResponseRedirect
|
| 187 |
+
from django.shortcuts import render
|
| 188 |
+
|
| 189 |
+
from .forms import MyForm
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
def myview(request):
|
| 193 |
+
if request.method == "POST":
|
| 194 |
+
form = MyForm(request.POST)
|
| 195 |
+
if form.is_valid():
|
| 196 |
+
# <process form cleaned data>
|
| 197 |
+
return HttpResponseRedirect("/success/")
|
| 198 |
+
else:
|
| 199 |
+
form = MyForm(initial={"key": "value"})
|
| 200 |
+
|
| 201 |
+
return render(request, "form_template.html", {"form": form})
|
| 202 |
+
|
| 203 |
+
A similar class-based view might look like::
|
| 204 |
+
|
| 205 |
+
from django.http import HttpResponseRedirect
|
| 206 |
+
from django.shortcuts import render
|
| 207 |
+
from django.views import View
|
| 208 |
+
|
| 209 |
+
from .forms import MyForm
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
class MyFormView(View):
|
| 213 |
+
form_class = MyForm
|
| 214 |
+
initial = {"key": "value"}
|
| 215 |
+
template_name = "form_template.html"
|
| 216 |
+
|
| 217 |
+
def get(self, request, *args, **kwargs):
|
| 218 |
+
form = self.form_class(initial=self.initial)
|
| 219 |
+
return render(request, self.template_name, {"form": form})
|
| 220 |
+
|
| 221 |
+
def post(self, request, *args, **kwargs):
|
| 222 |
+
form = self.form_class(request.POST)
|
| 223 |
+
if form.is_valid():
|
| 224 |
+
# <process form cleaned data>
|
| 225 |
+
return HttpResponseRedirect("/success/")
|
| 226 |
+
|
| 227 |
+
return render(request, self.template_name, {"form": form})
|
| 228 |
+
|
| 229 |
+
This is a minimal case, but you can see that you would then have the option
|
| 230 |
+
of customizing this view by overriding any of the class attributes, e.g.
|
| 231 |
+
``form_class``, via URLconf configuration, or subclassing and overriding one or
|
| 232 |
+
more of the methods (or both!).
|
| 233 |
+
|
| 234 |
+
Decorating class-based views
|
| 235 |
+
============================
|
| 236 |
+
|
| 237 |
+
The extension of class-based views isn't limited to using mixins. You can also
|
| 238 |
+
use decorators. Since class-based views aren't functions, decorating them works
|
| 239 |
+
differently depending on if you're using ``as_view()`` or creating a subclass.
|
| 240 |
+
|
| 241 |
+
Decorating in URLconf
|
| 242 |
+
---------------------
|
| 243 |
+
|
| 244 |
+
You can adjust class-based views by decorating the result of the
|
| 245 |
+
:meth:`~django.views.generic.base.View.as_view` method. The easiest place to do
|
| 246 |
+
this is in the URLconf where you deploy your view::
|
| 247 |
+
|
| 248 |
+
from django.contrib.auth.decorators import login_required, permission_required
|
| 249 |
+
from django.views.generic import TemplateView
|
| 250 |
+
|
| 251 |
+
from .views import VoteView
|
| 252 |
+
|
| 253 |
+
urlpatterns = [
|
| 254 |
+
path("about/", login_required(TemplateView.as_view(template_name="secret.html"))),
|
| 255 |
+
path("vote/", permission_required("polls.can_vote")(VoteView.as_view())),
|
| 256 |
+
]
|
| 257 |
+
|
| 258 |
+
This approach applies the decorator on a per-instance basis. If you
|
| 259 |
+
want every instance of a view to be decorated, you need to take a
|
| 260 |
+
different approach.
|
| 261 |
+
|
| 262 |
+
.. _decorating-class-based-views:
|
| 263 |
+
|
| 264 |
+
Decorating the class
|
| 265 |
+
--------------------
|
| 266 |
+
|
| 267 |
+
To decorate every instance of a class-based view, you need to decorate
|
| 268 |
+
the class definition itself. To do this you apply the decorator to the
|
| 269 |
+
:meth:`~django.views.generic.base.View.dispatch` method of the class.
|
| 270 |
+
|
| 271 |
+
A method on a class isn't quite the same as a standalone function, so you can't
|
| 272 |
+
just apply a function decorator to the method -- you need to transform it into
|
| 273 |
+
a method decorator first. The ``method_decorator`` decorator transforms a
|
| 274 |
+
function decorator into a method decorator so that it can be used on an
|
| 275 |
+
instance method. For example::
|
| 276 |
+
|
| 277 |
+
from django.contrib.auth.decorators import login_required
|
| 278 |
+
from django.utils.decorators import method_decorator
|
| 279 |
+
from django.views.generic import TemplateView
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
class ProtectedView(TemplateView):
|
| 283 |
+
template_name = "secret.html"
|
| 284 |
+
|
| 285 |
+
@method_decorator(login_required)
|
| 286 |
+
def dispatch(self, *args, **kwargs):
|
| 287 |
+
return super().dispatch(*args, **kwargs)
|
| 288 |
+
|
| 289 |
+
Or, more succinctly, you can decorate the class instead and pass the name
|
| 290 |
+
of the method to be decorated as the keyword argument ``name``::
|
| 291 |
+
|
| 292 |
+
@method_decorator(login_required, name="dispatch")
|
| 293 |
+
class ProtectedView(TemplateView):
|
| 294 |
+
template_name = "secret.html"
|
| 295 |
+
|
| 296 |
+
If you have a set of common decorators used in several places, you can define
|
| 297 |
+
a list or tuple of decorators and use this instead of invoking
|
| 298 |
+
``method_decorator()`` multiple times. These two classes are equivalent::
|
| 299 |
+
|
| 300 |
+
decorators = [never_cache, login_required]
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
@method_decorator(decorators, name="dispatch")
|
| 304 |
+
class ProtectedView(TemplateView):
|
| 305 |
+
template_name = "secret.html"
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
@method_decorator(never_cache, name="dispatch")
|
| 309 |
+
@method_decorator(login_required, name="dispatch")
|
| 310 |
+
class ProtectedView(TemplateView):
|
| 311 |
+
template_name = "secret.html"
|
| 312 |
+
|
| 313 |
+
The decorators will process a request in the order they are passed to the
|
| 314 |
+
decorator. In the example, ``never_cache()`` will process the request before
|
| 315 |
+
``login_required()``.
|
| 316 |
+
|
| 317 |
+
In this example, every instance of ``ProtectedView`` will have login
|
| 318 |
+
protection. These examples use ``login_required``, however, the same behavior
|
| 319 |
+
can be obtained by using
|
| 320 |
+
:class:`~django.contrib.auth.mixins.LoginRequiredMixin`.
|
| 321 |
+
|
| 322 |
+
.. note::
|
| 323 |
+
|
| 324 |
+
``method_decorator`` passes ``*args`` and ``**kwargs``
|
| 325 |
+
as parameters to the decorated method on the class. If your method
|
| 326 |
+
does not accept a compatible set of parameters it will raise a
|
| 327 |
+
``TypeError`` exception.
|