mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #34140 -- Reformatted code blocks in docs with blacken-docs.
This commit is contained in:
committed by
Mariusz Felisiak
parent
6015bab80e
commit
14459f80ee
@@ -454,6 +454,7 @@ database-compatible values. A custom field might look something like::
|
||||
|
||||
class CustomModelField(models.Field):
|
||||
...
|
||||
|
||||
def db_type(self):
|
||||
...
|
||||
|
||||
@@ -591,7 +592,6 @@ decorator to one that works with methods. For example, you would
|
||||
change code from this::
|
||||
|
||||
class MyClass(object):
|
||||
|
||||
@login_required
|
||||
def my_view(self, request):
|
||||
pass
|
||||
@@ -600,8 +600,8 @@ to this::
|
||||
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
class MyClass(object):
|
||||
|
||||
class MyClass(object):
|
||||
@method_decorator(login_required)
|
||||
def my_view(self, request):
|
||||
pass
|
||||
@@ -612,8 +612,8 @@ or::
|
||||
|
||||
login_required_m = method_decorator(login_required)
|
||||
|
||||
class MyClass(object):
|
||||
|
||||
class MyClass(object):
|
||||
@login_required_m
|
||||
def my_view(self, request):
|
||||
pass
|
||||
@@ -798,10 +798,10 @@ automatically translated to the new-style format.
|
||||
In the old-style (pre 1.2) format, you had a number of ``DATABASE_``
|
||||
settings in your settings file. For example::
|
||||
|
||||
DATABASE_NAME = 'test_db'
|
||||
DATABASE_ENGINE = 'postgresql_psycopg2'
|
||||
DATABASE_USER = 'myusername'
|
||||
DATABASE_PASSWORD = 's3krit'
|
||||
DATABASE_NAME = "test_db"
|
||||
DATABASE_ENGINE = "postgresql_psycopg2"
|
||||
DATABASE_USER = "myusername"
|
||||
DATABASE_PASSWORD = "s3krit"
|
||||
|
||||
These settings are now in a dictionary named
|
||||
:setting:`DATABASES`. Each item in the dictionary corresponds to a
|
||||
@@ -810,11 +810,11 @@ default database connection. The setting names have also been
|
||||
shortened. The previous sample settings would now look like this::
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'NAME': 'test_db',
|
||||
'ENGINE': 'django.db.backends.postgresql_psycopg2',
|
||||
'USER': 'myusername',
|
||||
'PASSWORD': 's3krit',
|
||||
"default": {
|
||||
"NAME": "test_db",
|
||||
"ENGINE": "django.db.backends.postgresql_psycopg2",
|
||||
"USER": "myusername",
|
||||
"PASSWORD": "s3krit",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -883,6 +883,7 @@ email backend API. Old code that explicitly instantiated an instance
|
||||
of an SMTPConnection::
|
||||
|
||||
from django.core.mail import SMTPConnection
|
||||
|
||||
connection = SMTPConnection()
|
||||
messages = get_notification_email()
|
||||
connection.send_messages(messages)
|
||||
@@ -891,6 +892,7 @@ of an SMTPConnection::
|
||||
instantiate a generic email connection::
|
||||
|
||||
from django.core.mail import get_connection
|
||||
|
||||
connection = get_connection()
|
||||
messages = get_notification_email()
|
||||
connection.send_messages(messages)
|
||||
@@ -901,7 +903,8 @@ connection with which to send email, you can explicitly request an
|
||||
SMTP connection::
|
||||
|
||||
from django.core.mail import get_connection
|
||||
connection = get_connection('django.core.mail.backends.smtp.EmailBackend')
|
||||
|
||||
connection = get_connection("django.core.mail.backends.smtp.EmailBackend")
|
||||
messages = get_notification_email()
|
||||
connection.send_messages(messages)
|
||||
|
||||
@@ -909,7 +912,9 @@ If your call to construct an instance of ``SMTPConnection`` required
|
||||
additional arguments, those arguments can be passed to the
|
||||
:meth:`~django.core.mail.get_connection()` call::
|
||||
|
||||
connection = get_connection('django.core.mail.backends.smtp.EmailBackend', hostname='localhost', port=1234)
|
||||
connection = get_connection(
|
||||
"django.core.mail.backends.smtp.EmailBackend", hostname="localhost", port=1234
|
||||
)
|
||||
|
||||
User Messages API
|
||||
-----------------
|
||||
@@ -920,12 +925,13 @@ The API for storing messages in the user ``Message`` model (via
|
||||
|
||||
To upgrade your code, you need to replace any instances of this::
|
||||
|
||||
user.message_set.create('a message')
|
||||
user.message_set.create("a message")
|
||||
|
||||
...with the following::
|
||||
|
||||
from django.contrib import messages
|
||||
messages.add_message(request, messages.INFO, 'a message')
|
||||
|
||||
messages.add_message(request, messages.INFO, "a message")
|
||||
|
||||
Additionally, if you make use of the method, you need to replace the
|
||||
following::
|
||||
@@ -936,6 +942,7 @@ following::
|
||||
...with::
|
||||
|
||||
from django.contrib import messages
|
||||
|
||||
for message in messages.get_messages(request):
|
||||
...
|
||||
|
||||
@@ -955,19 +962,22 @@ back to default settings if set to ``False``.
|
||||
To get the different date formats, instead of writing this::
|
||||
|
||||
from django.utils.translation import get_date_formats
|
||||
|
||||
date_format, datetime_format, time_format = get_date_formats()
|
||||
|
||||
...use::
|
||||
|
||||
from django.utils import formats
|
||||
date_format = formats.get_format('DATE_FORMAT')
|
||||
datetime_format = formats.get_format('DATETIME_FORMAT')
|
||||
time_format = formats.get_format('TIME_FORMAT')
|
||||
|
||||
date_format = formats.get_format("DATE_FORMAT")
|
||||
datetime_format = formats.get_format("DATETIME_FORMAT")
|
||||
time_format = formats.get_format("TIME_FORMAT")
|
||||
|
||||
Or, when directly formatting a date value::
|
||||
|
||||
from django.utils import formats
|
||||
value_formatted = formats.date_format(value, 'DATETIME_FORMAT')
|
||||
|
||||
value_formatted = formats.date_format(value, "DATETIME_FORMAT")
|
||||
|
||||
The same applies to the globals found in ``django.forms.fields``:
|
||||
|
||||
@@ -1003,14 +1013,18 @@ the following :doc:`URLconf </topics/http/urls>`::
|
||||
from myproject.feeds import LatestEntries, LatestEntriesByCategory
|
||||
|
||||
feeds = {
|
||||
'latest': LatestEntries,
|
||||
'categories': LatestEntriesByCategory,
|
||||
"latest": LatestEntries,
|
||||
"categories": LatestEntriesByCategory,
|
||||
}
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = patterns(
|
||||
"",
|
||||
# ...
|
||||
(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
|
||||
{'feed_dict': feeds}),
|
||||
(
|
||||
r"^feeds/(?P<url>.*)/$",
|
||||
"django.contrib.syndication.views.feed",
|
||||
{"feed_dict": feeds},
|
||||
),
|
||||
# ...
|
||||
)
|
||||
|
||||
@@ -1019,10 +1033,11 @@ Using the new Feed class, these feeds can be deployed directly as views::
|
||||
from django.conf.urls.defaults import *
|
||||
from myproject.feeds import LatestEntries, LatestEntriesByCategory
|
||||
|
||||
urlpatterns = patterns('',
|
||||
urlpatterns = patterns(
|
||||
"",
|
||||
# ...
|
||||
(r'^feeds/latest/$', LatestEntries()),
|
||||
(r'^feeds/categories/(?P<category_id>\d+)/$', LatestEntriesByCategory()),
|
||||
(r"^feeds/latest/$", LatestEntries()),
|
||||
(r"^feeds/categories/(?P<category_id>\d+)/$", LatestEntriesByCategory()),
|
||||
# ...
|
||||
)
|
||||
|
||||
@@ -1045,6 +1060,7 @@ URL, so it would look like this::
|
||||
from django.shortcuts import get_object_or_404
|
||||
from myproject.models import Category
|
||||
|
||||
|
||||
class LatestEntriesByCategory(Feed):
|
||||
def get_object(self, request, category_id):
|
||||
return get_object_or_404(Category, id=category_id)
|
||||
@@ -1121,6 +1137,7 @@ below::
|
||||
Would need to be changed::
|
||||
|
||||
from django.db import connection
|
||||
|
||||
PostGISAdaptor = connection.ops.Adapter
|
||||
|
||||
``SpatialRefSys`` and ``GeometryColumns`` models
|
||||
@@ -1146,7 +1163,7 @@ is using a supported spatial database backend.
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from django.db.models import get_app, get_models
|
||||
>>> get_models(get_app('gis'))
|
||||
>>> get_models(get_app("gis"))
|
||||
[]
|
||||
|
||||
To get the correct ``SpatialRefSys`` and ``GeometryColumns``
|
||||
@@ -1155,8 +1172,8 @@ for your spatial database use the methods provided by the spatial backend:
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from django.db import connections
|
||||
>>> SpatialRefSys = connections['my_spatialite'].ops.spatial_ref_sys()
|
||||
>>> GeometryColumns = connections['my_postgis'].ops.geometry_columns()
|
||||
>>> SpatialRefSys = connections["my_spatialite"].ops.spatial_ref_sys()
|
||||
>>> GeometryColumns = connections["my_postgis"].ops.geometry_columns()
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -1166,8 +1183,8 @@ for your spatial database use the methods provided by the spatial backend:
|
||||
In other words, to ensure that the models in the example above
|
||||
use the correct database::
|
||||
|
||||
sr_qs = SpatialRefSys.objects.using('my_spatialite').filter(...)
|
||||
gc_qs = GeometryColumns.objects.using('my_postgis').filter(...)
|
||||
sr_qs = SpatialRefSys.objects.using("my_spatialite").filter(...)
|
||||
gc_qs = GeometryColumns.objects.using("my_postgis").filter(...)
|
||||
|
||||
Language code ``no``
|
||||
--------------------
|
||||
|
||||
Reference in New Issue
Block a user