mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Refs #20910 -- Replaced snippet directive with code-block.
This commit is contained in:
committed by
Tim Graham
parent
f8ff529ee3
commit
c49ea6f591
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user