mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Removed unnecessary code-block directives.
This commit is contained in:
@@ -94,7 +94,7 @@ whose username matches the current system user.
|
||||
You can also change a password programmatically, using
|
||||
:meth:`~django.contrib.auth.models.User.set_password()`:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from django.contrib.auth.models import User
|
||||
>>> u = User.objects.get(username='john')
|
||||
@@ -182,9 +182,7 @@ customize permissions for different object instances of the same type.
|
||||
fields: ``groups`` and ``user_permissions``.
|
||||
:class:`~django.contrib.auth.models.User` objects can access their related
|
||||
objects in the same way as any other :doc:`Django model
|
||||
</topics/db/models>`:
|
||||
|
||||
.. code-block:: python
|
||||
</topics/db/models>`::
|
||||
|
||||
myuser.groups = [group_list]
|
||||
myuser.groups.add(group, group, ...)
|
||||
|
||||
@@ -679,7 +679,7 @@ to the ``cache`` template tag; ``vary_on`` is a list of all additional arguments
|
||||
passed to the tag. This function can be useful for invalidating or overwriting
|
||||
a cached item, for example:
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from django.core.cache import cache
|
||||
>>> from django.core.cache.utils import make_template_fragment_key
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ collection of objects. This topic guide describes the ways that aggregate values
|
||||
can be generated and returned using Django queries.
|
||||
|
||||
Throughout this guide, we'll refer to the following models. These models are
|
||||
used to track the inventory for a series of online bookstores:
|
||||
used to track the inventory for a series of online bookstores::
|
||||
|
||||
.. _queryset-model-example:
|
||||
|
||||
|
||||
@@ -2,11 +2,7 @@
|
||||
Many-to-one relationships
|
||||
#########################
|
||||
|
||||
.. highlight:: pycon
|
||||
|
||||
To define a many-to-one relationship, use :class:`~django.db.models.ForeignKey`.
|
||||
|
||||
.. code-block:: python
|
||||
To define a many-to-one relationship, use :class:`~django.db.models.ForeignKey`::
|
||||
|
||||
from django.db import models
|
||||
|
||||
@@ -32,6 +28,8 @@ To define a many-to-one relationship, use :class:`~django.db.models.ForeignKey`.
|
||||
What follows are examples of operations that can be performed using the Python
|
||||
API facilities.
|
||||
|
||||
.. highlight:: pycon
|
||||
|
||||
Create a few Reporters::
|
||||
|
||||
>>> r = Reporter(first_name='John', last_name='Smith', email='john@example.com')
|
||||
|
||||
@@ -2,13 +2,9 @@
|
||||
One-to-one relationships
|
||||
########################
|
||||
|
||||
.. highlight:: pycon
|
||||
|
||||
To define a one-to-one relationship, use :ref:`ref-onetoone`.
|
||||
|
||||
In this example, a ``Place`` optionally can be a ``Restaurant``:
|
||||
|
||||
.. code-block:: python
|
||||
In this example, a ``Place`` optionally can be a ``Restaurant``::
|
||||
|
||||
from django.db import models
|
||||
|
||||
@@ -37,6 +33,8 @@ In this example, a ``Place`` optionally can be a ``Restaurant``:
|
||||
What follows are examples of operations that can be performed using the Python
|
||||
API facilities.
|
||||
|
||||
.. highlight:: pycon
|
||||
|
||||
Create a couple of Places::
|
||||
|
||||
>>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
|
||||
|
||||
@@ -24,9 +24,7 @@ the alias of ``default`` when no other database has been selected.
|
||||
|
||||
The following is an example ``settings.py`` snippet defining two
|
||||
databases -- a default PostgreSQL database and a MySQL database called
|
||||
``users``:
|
||||
|
||||
.. code-block:: python
|
||||
``users``::
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
|
||||
@@ -324,16 +324,12 @@ manager.
|
||||
|
||||
.. _`Python ticket #9220`: http://bugs.python.org/issue9220
|
||||
|
||||
Using a cursor as a context manager:
|
||||
|
||||
.. code-block:: python
|
||||
Using a cursor as a context manager::
|
||||
|
||||
with connection.cursor() as c:
|
||||
c.execute(...)
|
||||
|
||||
is equivalent to:
|
||||
|
||||
.. code-block:: python
|
||||
is equivalent to::
|
||||
|
||||
c = connection.cursor()
|
||||
try:
|
||||
|
||||
@@ -554,9 +554,7 @@ Using a formset in views and templates
|
||||
|
||||
Using a formset inside a view is as easy as using a regular ``Form`` class.
|
||||
The only thing you will want to be aware of is making sure to use the
|
||||
management form inside the template. Let's look at a sample view:
|
||||
|
||||
.. code-block:: python
|
||||
management form inside the template. Let's look at a sample view::
|
||||
|
||||
from django.forms.formsets import formset_factory
|
||||
from django.shortcuts import render_to_response
|
||||
@@ -633,9 +631,7 @@ You are able to use more than one formset in a view if you like. Formsets
|
||||
borrow much of its behavior from forms. With that said you are able to use
|
||||
``prefix`` to prefix formset form field names with a given value to allow
|
||||
more than one formset to be sent to a view without name clashing. Lets take
|
||||
a look at how this might be accomplished:
|
||||
|
||||
.. code-block:: python
|
||||
a look at how this might be accomplished::
|
||||
|
||||
from django.forms.formsets import formset_factory
|
||||
from django.shortcuts import render_to_response
|
||||
|
||||
@@ -224,9 +224,7 @@ 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:
|
||||
|
||||
.. code-block:: python
|
||||
it in Django is this::
|
||||
|
||||
from django import forms
|
||||
|
||||
@@ -273,9 +271,7 @@ same view which published the form. This allows us to reuse some of the same
|
||||
logic.
|
||||
|
||||
To handle the form we need to instantiate it in the view for the URL where we
|
||||
want it to be published:
|
||||
|
||||
.. code-block:: python
|
||||
want it to be published::
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponseRedirect
|
||||
@@ -386,9 +382,7 @@ 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 Web site:
|
||||
|
||||
.. code-block:: python
|
||||
to implement "contact me" functionality on a personal Web site::
|
||||
|
||||
from django import forms
|
||||
|
||||
@@ -434,9 +428,7 @@ In the contact form example above, ``cc_myself`` will be a boolean value.
|
||||
Likewise, fields such as :class:`IntegerField` and :class:`FloatField` convert
|
||||
values to a Python ``int`` and ``float`` respectively.
|
||||
|
||||
Here's how the form data could be processed in the view that handles this form:
|
||||
|
||||
.. code-block:: python
|
||||
Here's how the form data could be processed in the view that handles this form::
|
||||
|
||||
from django.core.mail import send_mail
|
||||
|
||||
|
||||
@@ -851,7 +851,9 @@ Saving objects in the formset
|
||||
-----------------------------
|
||||
|
||||
As with a ``ModelForm``, you can save the data as a model object. This is done
|
||||
with the formset's ``save()`` method::
|
||||
with the formset's ``save()`` method:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Create a formset instance with POST data.
|
||||
>>> formset = AuthorFormSet(request.POST)
|
||||
@@ -869,7 +871,9 @@ excluded), these fields will not be set by the ``save()`` method. You can find
|
||||
more information about this restriction, which also holds for regular
|
||||
``ModelForms``, in `Selecting the fields to use`_.
|
||||
|
||||
Pass ``commit=False`` to return the unsaved model instances::
|
||||
Pass ``commit=False`` to return the unsaved model instances:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# don't save to the database
|
||||
>>> instances = formset.save(commit=False)
|
||||
|
||||
@@ -260,9 +260,7 @@ list::
|
||||
:func:`~django.views.decorators.csrf.csrf_protect` on the function that
|
||||
actually processes the request. Note that this means that the handlers may
|
||||
start receiving the file upload before the CSRF checks have been done.
|
||||
Example code:
|
||||
|
||||
.. code-block:: python
|
||||
Example code::
|
||||
|
||||
from django.views.decorators.csrf import csrf_exempt, csrf_protect
|
||||
|
||||
|
||||
@@ -15,9 +15,7 @@ application directory.
|
||||
A simple view
|
||||
=============
|
||||
|
||||
Here's a view that returns the current date and time, as an HTML document:
|
||||
|
||||
.. code-block:: python
|
||||
Here's a view that returns the current date and time, as an HTML document::
|
||||
|
||||
from django.http import HttpResponse
|
||||
import datetime
|
||||
|
||||
@@ -1049,6 +1049,7 @@ whenever you restart your application server.
|
||||
from django.views.i18n import javascript_catalog
|
||||
|
||||
last_modified_date = timezone.now()
|
||||
|
||||
@last_modified(lambda req, **kw: last_modified_date)
|
||||
def cached_javascript_catalog(request, domain='djangojs', packages=None):
|
||||
return javascript_catalog(request, domain, packages)
|
||||
|
||||
@@ -77,9 +77,7 @@ Receiver functions
|
||||
------------------
|
||||
|
||||
First, we need to define a receiver function. A receiver can be any Python
|
||||
function or method:
|
||||
|
||||
.. code-block:: python
|
||||
function or method::
|
||||
|
||||
def my_callback(sender, **kwargs):
|
||||
print("Request finished!")
|
||||
@@ -106,9 +104,7 @@ Connecting receiver functions
|
||||
-----------------------------
|
||||
|
||||
There are two ways you can connect a receiver to a signal. You can take the
|
||||
manual connect route:
|
||||
|
||||
.. code-block:: python
|
||||
manual connect route::
|
||||
|
||||
from django.core.signals import request_finished
|
||||
|
||||
@@ -120,9 +116,7 @@ Alternatively, you can use a :func:`receiver` decorator:
|
||||
|
||||
:param signal: A signal or a list of signals to connect a function to.
|
||||
|
||||
Here's how you connect with the decorator:
|
||||
|
||||
.. code-block:: python
|
||||
Here's how you connect with the decorator::
|
||||
|
||||
from django.core.signals import request_finished
|
||||
from django.dispatch import receiver
|
||||
@@ -133,7 +127,6 @@ Here's how you connect with the decorator:
|
||||
|
||||
Now, our ``my_callback`` function will be called each time a request finishes.
|
||||
|
||||
|
||||
.. admonition:: Where should this code live?
|
||||
|
||||
Strictly speaking, signal handling and registration code can live anywhere
|
||||
@@ -167,14 +160,13 @@ when one *specific* model is saved.
|
||||
In these cases, you can register to receive signals sent only by particular
|
||||
senders. In the case of :data:`django.db.models.signals.pre_save`, the sender
|
||||
will be the model class being saved, so you can indicate that you only want
|
||||
signals sent by some model:
|
||||
|
||||
.. code-block:: python
|
||||
signals sent by some model::
|
||||
|
||||
from django.db.models.signals import pre_save
|
||||
from django.dispatch import receiver
|
||||
from myapp.models import MyModel
|
||||
|
||||
|
||||
@receiver(pre_save, sender=MyModel)
|
||||
def my_handler(sender, **kwargs):
|
||||
...
|
||||
@@ -200,9 +192,7 @@ send an email whenever a model is saved), pass a unique identifier as
|
||||
the ``dispatch_uid`` argument to identify your receiver function. This
|
||||
identifier will usually be a string, although any hashable object will
|
||||
suffice. The end result is that your receiver function will only be
|
||||
bound to the signal once for each unique ``dispatch_uid`` value.
|
||||
|
||||
.. code-block:: python
|
||||
bound to the signal once for each unique ``dispatch_uid`` value::
|
||||
|
||||
from django.core.signals import request_finished
|
||||
|
||||
@@ -224,9 +214,7 @@ All signals are :class:`django.dispatch.Signal` instances. The
|
||||
to listeners. This is purely documentational, however, as there is nothing that
|
||||
checks that the signal actually provides these arguments to its listeners.
|
||||
|
||||
For example:
|
||||
|
||||
.. code-block:: python
|
||||
For example::
|
||||
|
||||
import django.dispatch
|
||||
|
||||
@@ -250,9 +238,7 @@ To send a signal, call either :meth:`Signal.send` or :meth:`Signal.send_robust`.
|
||||
You must provide the ``sender`` argument (which is a class most of the time),
|
||||
and may provide as many other keyword arguments as you like.
|
||||
|
||||
For example, here's how sending our ``pizza_done`` signal might look:
|
||||
|
||||
.. code-block:: python
|
||||
For example, here's how sending our ``pizza_done`` signal might look::
|
||||
|
||||
class PizzaStore(object):
|
||||
...
|
||||
|
||||
@@ -695,9 +695,7 @@ via the :djadminopt:`--liveserver` option, for example:
|
||||
|
||||
Another way of changing the default server address is by setting the
|
||||
`DJANGO_LIVE_TEST_SERVER_ADDRESS` environment variable somewhere in your
|
||||
code (for example, in a :ref:`custom test runner<topics-testing-test_runner>`):
|
||||
|
||||
.. code-block:: python
|
||||
code (for example, in a :ref:`custom test runner<topics-testing-test_runner>`)::
|
||||
|
||||
import os
|
||||
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = 'localhost:8082'
|
||||
@@ -727,9 +725,7 @@ Python path:
|
||||
pip install selenium
|
||||
|
||||
Then, add a ``LiveServerTestCase``-based test to your app's tests module
|
||||
(for example: ``myapp/tests.py``). The code for this test may look as follows:
|
||||
|
||||
.. code-block:: python
|
||||
(for example: ``myapp/tests.py``). The code for this test may look as follows::
|
||||
|
||||
from django.test import LiveServerTestCase
|
||||
from selenium.webdriver.firefox.webdriver import WebDriver
|
||||
@@ -805,9 +801,7 @@ out the `full reference`_ for more details.
|
||||
need to check that a response is received by Selenium and that the next
|
||||
page is loaded before proceeding with further test execution.
|
||||
Do this, for example, by making Selenium wait until the ``<body>`` HTML tag
|
||||
is found in the response (requires Selenium > 2.13):
|
||||
|
||||
.. code-block:: python
|
||||
is found in the response (requires Selenium > 2.13)::
|
||||
|
||||
def test_login(self):
|
||||
from selenium.webdriver.support.wait import WebDriverWait
|
||||
|
||||
Reference in New Issue
Block a user