mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #20910 -- Replaced snippet directive with code-block.
This commit is contained in:
committed by
Tim Graham
parent
f8ff529ee3
commit
c49ea6f591
@@ -251,8 +251,8 @@ modify the pattern to work with any algorithm or with a custom user model.
|
||||
|
||||
First, we'll add the custom hasher:
|
||||
|
||||
.. snippet::
|
||||
:filename: accounts/hashers.py
|
||||
.. code-block:: python
|
||||
:caption: accounts/hashers.py
|
||||
|
||||
from django.contrib.auth.hashers import (
|
||||
PBKDF2PasswordHasher, SHA1PasswordHasher,
|
||||
@@ -271,8 +271,8 @@ First, we'll add the custom hasher:
|
||||
|
||||
The data migration might look something like:
|
||||
|
||||
.. snippet::
|
||||
:filename: accounts/migrations/0002_migrate_sha1_passwords.py
|
||||
.. code-block:: python
|
||||
:caption: accounts/migrations/0002_migrate_sha1_passwords.py
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
@@ -306,8 +306,8 @@ several thousand users, depending on the speed of your hardware.
|
||||
|
||||
Finally, we'll add a :setting:`PASSWORD_HASHERS` setting:
|
||||
|
||||
.. snippet::
|
||||
:filename: mysite/settings.py
|
||||
.. code-block:: python
|
||||
:caption: mysite/settings.py
|
||||
|
||||
PASSWORD_HASHERS = [
|
||||
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
|
||||
|
||||
@@ -18,8 +18,8 @@ Basic forms
|
||||
|
||||
Given a simple contact form:
|
||||
|
||||
.. snippet::
|
||||
:filename: forms.py
|
||||
.. code-block:: python
|
||||
:caption: forms.py
|
||||
|
||||
from django import forms
|
||||
|
||||
@@ -33,8 +33,8 @@ Given a simple contact form:
|
||||
|
||||
The view can be constructed using a ``FormView``:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from myapp.forms import ContactForm
|
||||
from django.views.generic.edit import FormView
|
||||
@@ -96,8 +96,8 @@ add extra validation) simply set
|
||||
First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
|
||||
``Author`` class:
|
||||
|
||||
.. snippet::
|
||||
:filename: models.py
|
||||
.. code-block:: python
|
||||
:caption: models.py
|
||||
|
||||
from django.db import models
|
||||
from django.urls import reverse
|
||||
@@ -112,8 +112,8 @@ Then we can use :class:`CreateView` and friends to do the actual
|
||||
work. Notice how we're just configuring the generic class-based views
|
||||
here; we don't have to write any logic ourselves:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
||||
@@ -146,8 +146,8 @@ and :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
|
||||
|
||||
Finally, we hook these new views into the URLconf:
|
||||
|
||||
.. snippet::
|
||||
:filename: urls.py
|
||||
.. code-block:: python
|
||||
:caption: urls.py
|
||||
|
||||
from django.urls import path
|
||||
from myapp.views import AuthorCreate, AuthorDelete, AuthorUpdate
|
||||
@@ -187,8 +187,8 @@ To track the user that created an object using a :class:`CreateView`,
|
||||
you can use a custom :class:`~django.forms.ModelForm` to do this. First, add
|
||||
the foreign key relation to the model:
|
||||
|
||||
.. snippet::
|
||||
:filename: models.py
|
||||
.. code-block:: python
|
||||
:caption: models.py
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
@@ -203,8 +203,8 @@ In the view, ensure that you don't include ``created_by`` in the list of fields
|
||||
to edit, and override
|
||||
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` to add the user:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from django.views.generic.edit import CreateView
|
||||
from myapp.models import Author
|
||||
|
||||
@@ -222,8 +222,8 @@ we'll want the functionality provided by
|
||||
We'll demonstrate this with the ``Author`` model we used in the
|
||||
:doc:`generic class-based views introduction<generic-display>`.
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from django.http import HttpResponseForbidden, HttpResponseRedirect
|
||||
from django.urls import reverse
|
||||
@@ -255,8 +255,8 @@ mixin.
|
||||
|
||||
We can hook this into our URLs easily enough:
|
||||
|
||||
.. snippet::
|
||||
:filename: urls.py
|
||||
.. code-block:: python
|
||||
:caption: urls.py
|
||||
|
||||
from django.urls import path
|
||||
from books.views import RecordInterest
|
||||
|
||||
@@ -1429,8 +1429,8 @@ store your models. You must import the models in the ``__init__.py`` file.
|
||||
For example, if you had ``organic.py`` and ``synthetic.py`` in the ``models``
|
||||
directory:
|
||||
|
||||
.. snippet::
|
||||
:filename: myapp/models/__init__.py
|
||||
.. code-block:: python
|
||||
:caption: myapp/models/__init__.py
|
||||
|
||||
from .organic import Person
|
||||
from .synthetic import Robot
|
||||
|
||||
@@ -226,8 +226,8 @@ The :class:`Form` class
|
||||
We already know what we want our HTML form to look like. Our starting point for
|
||||
it in Django is this:
|
||||
|
||||
.. snippet::
|
||||
:filename: forms.py
|
||||
.. code-block:: python
|
||||
:caption: forms.py
|
||||
|
||||
from django import forms
|
||||
|
||||
@@ -276,8 +276,8 @@ logic.
|
||||
To handle the form we need to instantiate it in the view for the URL where we
|
||||
want it to be published:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
@@ -404,8 +404,8 @@ More on fields
|
||||
Consider a more useful form than our minimal example above, which we could use
|
||||
to implement "contact me" functionality on a personal website:
|
||||
|
||||
.. snippet::
|
||||
:filename: forms.py
|
||||
.. code-block:: python
|
||||
:caption: forms.py
|
||||
|
||||
from django import forms
|
||||
|
||||
@@ -453,8 +453,8 @@ values to a Python ``int`` and ``float`` respectively.
|
||||
|
||||
Here's how the form data could be processed in the view that handles this form:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from django.core.mail import send_mail
|
||||
|
||||
|
||||
@@ -21,8 +21,8 @@ Basic file uploads
|
||||
|
||||
Consider a simple form containing a :class:`~django.forms.FileField`:
|
||||
|
||||
.. snippet::
|
||||
:filename: forms.py
|
||||
.. code-block:: python
|
||||
:caption: forms.py
|
||||
|
||||
from django import forms
|
||||
|
||||
@@ -46,8 +46,8 @@ Most of the time, you'll simply pass the file data from ``request`` into the
|
||||
form as described in :ref:`binding-uploaded-files`. This would look
|
||||
something like:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
@@ -133,8 +133,8 @@ Uploading multiple files
|
||||
If you want to upload multiple files using one form field, set the ``multiple``
|
||||
HTML attribute of field's widget:
|
||||
|
||||
.. snippet::
|
||||
:filename: forms.py
|
||||
.. code-block:: python
|
||||
:caption: forms.py
|
||||
|
||||
from django import forms
|
||||
|
||||
@@ -145,8 +145,8 @@ Then override the ``post`` method of your
|
||||
:class:`~django.views.generic.edit.FormView` subclass to handle multiple file
|
||||
uploads:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
.. code-block:: python
|
||||
:caption: views.py
|
||||
|
||||
from django.views.generic.edit import FormView
|
||||
from .forms import FileFieldForm
|
||||
|
||||
@@ -761,8 +761,8 @@ and one called ``'publisher-polls'``. Assume we have enhanced that application
|
||||
so that it takes the instance namespace into consideration when creating and
|
||||
displaying polls.
|
||||
|
||||
.. snippet::
|
||||
:filename: urls.py
|
||||
.. code-block:: python
|
||||
:caption: urls.py
|
||||
|
||||
from django.urls import include, path
|
||||
|
||||
@@ -771,8 +771,8 @@ displaying polls.
|
||||
path('publisher-polls/', include('polls.urls', namespace='publisher-polls')),
|
||||
]
|
||||
|
||||
.. snippet::
|
||||
:filename: polls/urls.py
|
||||
.. code-block:: python
|
||||
:caption: polls/urls.py
|
||||
|
||||
from django.urls import path
|
||||
|
||||
@@ -830,8 +830,8 @@ at the same level as the ``urlpatterns`` attribute. You have to pass the actual
|
||||
module, or a string reference to the module, to :func:`~django.urls.include`,
|
||||
not the list of ``urlpatterns`` itself.
|
||||
|
||||
.. snippet::
|
||||
:filename: polls/urls.py
|
||||
.. code-block:: python
|
||||
:caption: polls/urls.py
|
||||
|
||||
from django.urls import path
|
||||
|
||||
@@ -844,8 +844,8 @@ not the list of ``urlpatterns`` itself.
|
||||
...
|
||||
]
|
||||
|
||||
.. snippet::
|
||||
:filename: urls.py
|
||||
.. code-block:: python
|
||||
:caption: urls.py
|
||||
|
||||
from django.urls import include, path
|
||||
|
||||
|
||||
@@ -429,8 +429,8 @@ configuration process for :ref:`Django's default logging
|
||||
<default-logging-configuration>`. Here's an example that disables Django's
|
||||
logging configuration and then manually configures logging:
|
||||
|
||||
.. snippet::
|
||||
:filename: settings.py
|
||||
.. code-block:: python
|
||||
:caption: settings.py
|
||||
|
||||
LOGGING_CONFIG = None
|
||||
|
||||
|
||||
@@ -325,8 +325,8 @@ following structure::
|
||||
|
||||
Let's take a look inside a couple of those files:
|
||||
|
||||
.. snippet::
|
||||
:filename: runtests.py
|
||||
.. code-block:: python
|
||||
:caption: runtests.py
|
||||
|
||||
#!/usr/bin/env python
|
||||
import os
|
||||
@@ -353,8 +353,8 @@ necessary to use the Django test runner. You may want to add
|
||||
command-line options for controlling verbosity, passing in specific test
|
||||
labels to run, etc.
|
||||
|
||||
.. snippet::
|
||||
:filename: tests/test_settings.py
|
||||
.. code-block:: python
|
||||
:caption: tests/test_settings.py
|
||||
|
||||
SECRET_KEY = 'fake-key'
|
||||
INSTALLED_APPS = [
|
||||
|
||||
Reference in New Issue
Block a user