mirror of
https://github.com/django/django.git
synced 2025-08-31 07:47:37 +00:00
Removed unnecessary code-block directives.
This commit is contained in:
parent
fa02120d36
commit
9d6551204e
32 changed files with 161 additions and 308 deletions
|
@ -15,9 +15,11 @@ processing.
|
|||
Basic Forms
|
||||
-----------
|
||||
|
||||
Given a simple contact form::
|
||||
Given a simple contact form:
|
||||
|
||||
.. snippet::
|
||||
:filename: forms.py
|
||||
|
||||
# forms.py
|
||||
from django import forms
|
||||
|
||||
class ContactForm(forms.Form):
|
||||
|
@ -28,9 +30,11 @@ Given a simple contact form::
|
|||
# send email using the self.cleaned_data dictionary
|
||||
pass
|
||||
|
||||
The view can be constructed using a ``FormView``::
|
||||
The view can be constructed using a ``FormView``:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
|
||||
# views.py
|
||||
from myapp.forms import ContactForm
|
||||
from django.views.generic.edit import FormView
|
||||
|
||||
|
@ -91,9 +95,9 @@ add extra validation) simply set
|
|||
First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
|
||||
``Author`` class:
|
||||
|
||||
.. code-block:: python
|
||||
.. snippet::
|
||||
:filename: models.py
|
||||
|
||||
# models.py
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
|
||||
|
@ -105,9 +109,11 @@ First we need to add :meth:`~django.db.models.Model.get_absolute_url()` to our
|
|||
|
||||
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::
|
||||
here; we don't have to write any logic ourselves:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
|
||||
# views.py
|
||||
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from myapp.models import Author
|
||||
|
@ -138,9 +144,11 @@ an :exc:`~django.core.exceptions.ImproperlyConfigured` exception if it's not.
|
|||
Omitting the ``fields`` attribute was previously allowed and resulted in a
|
||||
form with all of the model's fields.
|
||||
|
||||
Finally, we hook these new views into the URLconf::
|
||||
Finally, we hook these new views into the URLconf:
|
||||
|
||||
.. snippet::
|
||||
:filename: urls.py
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls import url
|
||||
from myapp.views import AuthorCreate, AuthorUpdate, AuthorDelete
|
||||
|
||||
|
@ -177,9 +185,11 @@ Models and request.user
|
|||
|
||||
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::
|
||||
the foreign key relation to the model:
|
||||
|
||||
.. snippet::
|
||||
:filename: models.py
|
||||
|
||||
# models.py
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
|
||||
|
@ -191,9 +201,11 @@ the foreign key relation to the model::
|
|||
|
||||
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::
|
||||
:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()` to add the user:
|
||||
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
|
||||
# views.py
|
||||
from django.views.generic.edit import CreateView
|
||||
from myapp.models import Author
|
||||
|
||||
|
|
|
@ -222,9 +222,9 @@ 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>`.
|
||||
|
||||
.. code-block:: python
|
||||
.. snippet::
|
||||
:filename: views.py
|
||||
|
||||
# views.py
|
||||
from django.http import HttpResponseForbidden, HttpResponseRedirect
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.views.generic import View
|
||||
|
@ -253,9 +253,11 @@ look up the author we're interested in, which it just does with a simple call
|
|||
to ``self.get_object()``. Everything else is taken care of for us by the
|
||||
mixin.
|
||||
|
||||
We can hook this into our URLs easily enough::
|
||||
We can hook this into our URLs easily enough:
|
||||
|
||||
.. snippet::
|
||||
:filename: urls.py
|
||||
|
||||
# urls.py
|
||||
from django.conf.urls import url
|
||||
from books.views import RecordInterest
|
||||
|
||||
|
@ -519,9 +521,7 @@ The ``AuthorDisplay`` view is almost the same as :ref:`when we
|
|||
first introduced AuthorDetail<generic-views-extra-work>`; we have to
|
||||
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.
|
||||
|
||||
.. code-block:: python
|
||||
``get_object()`` override from before for clarity::
|
||||
|
||||
from django.views.generic import DetailView
|
||||
from django import forms
|
||||
|
@ -542,9 +542,7 @@ Then the ``AuthorInterest`` is a simple :class:`FormView`, but we
|
|||
have to bring in :class:`~django.views.generic.detail.SingleObjectMixin` so we
|
||||
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``.
|
||||
|
||||
.. code-block:: python
|
||||
template as ``AuthorDisplay`` is using on ``GET``::
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpResponseForbidden
|
||||
|
@ -573,9 +571,7 @@ based view, so we can do that at the point we choose between the two subviews.
|
|||
You can of course pass through keyword arguments to
|
||||
:meth:`~django.views.generic.base.View.as_view()` in the same way you
|
||||
would in your URLconf, such as if you wanted the ``AuthorInterest`` behavior
|
||||
to also appear at another URL but using a different template.
|
||||
|
||||
.. code-block:: python
|
||||
to also appear at another URL but using a different template::
|
||||
|
||||
from django.views.generic import View
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue