mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Alphabetized imports in various docs.
Follow-up ofd97cce3409
and7d3fe36c62
.
This commit is contained in:
parent
1b7d524cfa
commit
35319bf12c
36 changed files with 71 additions and 71 deletions
|
@ -210,7 +210,7 @@ The default implementation simply adds the object being displayed to the
|
|||
template, but you can override it to send more::
|
||||
|
||||
from django.views.generic import DetailView
|
||||
from books.models import Publisher, Book
|
||||
from books.models import Book, Publisher
|
||||
|
||||
class PublisherDetail(DetailView):
|
||||
|
||||
|
@ -409,8 +409,8 @@ custom view::
|
|||
Then we'd write our new view -- ``get_object`` is the method that retrieves the
|
||||
object -- so we simply override it and wrap the call::
|
||||
|
||||
from django.views.generic import DetailView
|
||||
from django.utils import timezone
|
||||
from django.views.generic import DetailView
|
||||
from books.models import Author
|
||||
|
||||
class AuthorDetailView(DetailView):
|
||||
|
|
|
@ -99,8 +99,8 @@ First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
|
|||
.. snippet::
|
||||
:filename: models.py
|
||||
|
||||
from django.urls import reverse
|
||||
from django.db import models
|
||||
from django.urls import reverse
|
||||
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=200)
|
||||
|
@ -115,8 +115,8 @@ here; we don't have to write any logic ourselves:
|
|||
.. snippet::
|
||||
:filename: views.py
|
||||
|
||||
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
||||
from myapp.models import Author
|
||||
|
||||
class AuthorCreate(CreateView):
|
||||
|
@ -150,7 +150,7 @@ Finally, we hook these new views into the URLconf:
|
|||
:filename: urls.py
|
||||
|
||||
from django.urls import path
|
||||
from myapp.views import AuthorCreate, AuthorUpdate, AuthorDelete
|
||||
from myapp.views import AuthorCreate, AuthorDelete, AuthorUpdate
|
||||
|
||||
urlpatterns = [
|
||||
# ...
|
||||
|
|
|
@ -521,8 +521,8 @@ write our own ``get_context_data()`` to make the
|
|||
``AuthorInterestForm`` available to the template. We'll skip the
|
||||
``get_object()`` override from before for clarity::
|
||||
|
||||
from django.views.generic import DetailView
|
||||
from django import forms
|
||||
from django.views.generic import DetailView
|
||||
from books.models import Author
|
||||
|
||||
class AuthorInterestForm(forms.Form):
|
||||
|
@ -542,8 +542,8 @@ can find the author we're talking about, and we have to remember to set
|
|||
``template_name`` to ensure that form errors will render the same
|
||||
template as ``AuthorDisplay`` is using on ``GET``::
|
||||
|
||||
from django.urls import reverse
|
||||
from django.http import HttpResponseForbidden
|
||||
from django.urls import reverse
|
||||
from django.views.generic import FormView
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
|
||||
|
|
|
@ -277,7 +277,7 @@ For example, we can ask for all publishers, annotated with their respective
|
|||
total book stock counters (note how we use ``'book'`` to specify the
|
||||
``Publisher`` -> ``Book`` reverse foreign key hop)::
|
||||
|
||||
>>> from django.db.models import Count, Min, Sum, Avg
|
||||
>>> from django.db.models import Avg, Count, Min, Sum
|
||||
>>> Publisher.objects.annotate(Count('book'))
|
||||
|
||||
(Every ``Publisher`` in the resulting ``QuerySet`` will have an extra attribute
|
||||
|
@ -324,7 +324,7 @@ constraining the objects for which an annotation is calculated. For example,
|
|||
you can generate an annotated list of all books that have a title starting
|
||||
with "Django" using the query::
|
||||
|
||||
>>> from django.db.models import Count, Avg
|
||||
>>> from django.db.models import Avg, Count
|
||||
>>> Book.objects.filter(name__startswith="Django").annotate(num_authors=Count('authors'))
|
||||
|
||||
When used with an ``aggregate()`` clause, a filter has the effect of
|
||||
|
@ -578,6 +578,6 @@ For example, if you wanted to calculate the average number of authors per
|
|||
book you first annotate the set of books with the author count, then
|
||||
aggregate that author count, referencing the annotation field::
|
||||
|
||||
>>> from django.db.models import Count, Avg
|
||||
>>> from django.db.models import Avg, Count
|
||||
>>> Book.objects.annotate(num_authors=Count('authors')).aggregate(Avg('num_authors'))
|
||||
{'num_authors__avg': 1.66}
|
||||
|
|
|
@ -192,7 +192,7 @@ Here's an example view that takes a ``subject``, ``message`` and ``from_email``
|
|||
from the request's POST data, sends that to admin@example.com and redirects to
|
||||
"/contact/thanks/" when it's done::
|
||||
|
||||
from django.core.mail import send_mail, BadHeaderError
|
||||
from django.core.mail import BadHeaderError, send_mail
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
|
||||
def send_email(request):
|
||||
|
|
|
@ -140,8 +140,8 @@ the proper storage for that file), you can use file storage systems directly.
|
|||
You can create an instance of some custom file storage class, or -- often more
|
||||
useful -- you can use the global default storage system::
|
||||
|
||||
>>> from django.core.files.storage import default_storage
|
||||
>>> from django.core.files.base import ContentFile
|
||||
>>> from django.core.files.storage import default_storage
|
||||
|
||||
>>> path = default_storage.save('/path/to/file', ContentFile('new content'))
|
||||
>>> path
|
||||
|
@ -169,8 +169,8 @@ which implements basic local filesystem file storage.
|
|||
For example, the following code will store uploaded files under
|
||||
``/media/photos`` regardless of what your :setting:`MEDIA_ROOT` setting is::
|
||||
|
||||
from django.db import models
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.db import models
|
||||
|
||||
fs = FileSystemStorage(location='/media/photos')
|
||||
|
||||
|
|
|
@ -279,8 +279,8 @@ want it to be published:
|
|||
.. snippet::
|
||||
:filename: views.py
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
|
||||
from .forms import NameForm
|
||||
|
||||
|
|
|
@ -295,8 +295,8 @@ You can override the error messages from ``NON_FIELD_ERRORS`` raised by model
|
|||
validation by adding the :data:`~django.core.exceptions.NON_FIELD_ERRORS` key
|
||||
to the ``error_messages`` dictionary of the ``ModelForm``’s inner ``Meta`` class::
|
||||
|
||||
from django.forms import ModelForm
|
||||
from django.core.exceptions import NON_FIELD_ERRORS
|
||||
from django.forms import ModelForm
|
||||
|
||||
class ArticleForm(ModelForm):
|
||||
class Meta:
|
||||
|
@ -573,7 +573,7 @@ fields like you would in a regular ``Form``.
|
|||
If you want to specify a field's validators, you can do so by defining
|
||||
the field declaratively and setting its ``validators`` parameter::
|
||||
|
||||
from django.forms import ModelForm, CharField
|
||||
from django.forms import CharField, ModelForm
|
||||
from myapp.models import Article
|
||||
|
||||
class ArticleForm(ModelForm):
|
||||
|
|
|
@ -168,7 +168,7 @@ For example::
|
|||
Register custom converter classes in your URLconf using
|
||||
:func:`~django.urls.register_converter`::
|
||||
|
||||
from django.urls import register_converter, path
|
||||
from django.urls import path, register_converter
|
||||
|
||||
from . import converters, views
|
||||
|
||||
|
@ -614,8 +614,8 @@ You can obtain these in template code by using:
|
|||
|
||||
Or in Python code::
|
||||
|
||||
from django.urls import reverse
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.urls import reverse
|
||||
|
||||
def redirect_to_year(request):
|
||||
# ...
|
||||
|
|
|
@ -84,8 +84,8 @@ as a shorter alias, ``_``, to save typing.
|
|||
In this example, the text ``"Welcome to my site."`` is marked as a translation
|
||||
string::
|
||||
|
||||
from django.utils.translation import gettext as _
|
||||
from django.http import HttpResponse
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
def my_view(request):
|
||||
output = _("Welcome to my site.")
|
||||
|
@ -94,8 +94,8 @@ string::
|
|||
Obviously, you could code this without using the alias. This example is
|
||||
identical to the previous one::
|
||||
|
||||
from django.utils.translation import gettext
|
||||
from django.http import HttpResponse
|
||||
from django.utils.translation import gettext
|
||||
|
||||
def my_view(request):
|
||||
output = gettext("Welcome to my site.")
|
||||
|
@ -205,8 +205,8 @@ of its value.)
|
|||
|
||||
For example::
|
||||
|
||||
from django.utils.translation import ngettext
|
||||
from django.http import HttpResponse
|
||||
from django.utils.translation import ngettext
|
||||
|
||||
def hello_world(request, count):
|
||||
page = ngettext(
|
||||
|
|
|
@ -86,7 +86,7 @@ show how you can display the results. This example assumes you have a
|
|||
|
||||
The view function looks like this::
|
||||
|
||||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
||||
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
|
||||
from django.shortcuts import render
|
||||
|
||||
def listing(request):
|
||||
|
|
|
@ -38,7 +38,7 @@ Example
|
|||
The following is a simple unit test using the request factory::
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser, User
|
||||
from django.test import TestCase, RequestFactory
|
||||
from django.test import RequestFactory, TestCase
|
||||
|
||||
from .views import MyView, my_view
|
||||
|
||||
|
|
|
@ -1023,7 +1023,7 @@ If you want to use a different ``Client`` class (for example, a subclass
|
|||
with customized behavior), use the :attr:`~SimpleTestCase.client_class` class
|
||||
attribute::
|
||||
|
||||
from django.test import TestCase, Client
|
||||
from django.test import Client, TestCase
|
||||
|
||||
class MyTestClient(Client):
|
||||
# Specialized methods for your environment
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue