Skip to content

Commit

Permalink
Merge pull request #1162 from sspross/patch-docs
Browse files Browse the repository at this point in the history
Add needed Imports to the Documentation, Part II
  • Loading branch information
mjtamlyn committed May 19, 2013
2 parents c28b795 + 1fe587d commit c6855e8
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 77 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ answer newbie questions, and generally made Django that much better:
David Krauth David Krauth
Kevin Kubasik <kevin@kubasik.net> Kevin Kubasik <kevin@kubasik.net>
kurtiss@meetro.com kurtiss@meetro.com
Vladimir Kuzma <vladimirkuzma.ch@gmail.com>
Denis Kuzmichyov <kuzmichyov@gmail.com> Denis Kuzmichyov <kuzmichyov@gmail.com>
Panos Laganakos <panos.laganakos@gmail.com> Panos Laganakos <panos.laganakos@gmail.com>
Nick Lane <nick.lane.au@gmail.com> Nick Lane <nick.lane.au@gmail.com>
Expand Down
48 changes: 33 additions & 15 deletions docs/howto/custom-template-tags.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -300,18 +300,21 @@ Template filter code falls into one of two situations:


.. code-block:: python .. code-block:: python


from django.utils.html import conditional_escape from django import template
from django.utils.safestring import mark_safe from django.utils.html import conditional_escape

from django.utils.safestring import mark_safe
@register.filter(needs_autoescape=True)
def initial_letter_filter(text, autoescape=None): register = template.Library()
first, other = text[0], text[1:]
if autoescape: @register.filter(needs_autoescape=True)
esc = conditional_escape def initial_letter_filter(text, autoescape=None):
else: first, other = text[0], text[1:]
esc = lambda x: x if autoescape:
result = '<strong>%s</strong>%s' % (esc(first), esc(other)) esc = conditional_escape
return mark_safe(result) else:
esc = lambda x: x
result = '<strong>%s</strong>%s' % (esc(first), esc(other))
return mark_safe(result)


The ``needs_autoescape`` flag and the ``autoescape`` keyword argument mean The ``needs_autoescape`` flag and the ``autoescape`` keyword argument mean
that our function will know whether automatic escaping is in effect when the that our function will know whether automatic escaping is in effect when the
Expand Down Expand Up @@ -454,8 +457,9 @@ Continuing the above example, we need to define ``CurrentTimeNode``:


.. code-block:: python .. code-block:: python


from django import template
import datetime import datetime
from django import template

class CurrentTimeNode(template.Node): class CurrentTimeNode(template.Node):
def __init__(self, format_string): def __init__(self, format_string):
self.format_string = format_string self.format_string = format_string
Expand Down Expand Up @@ -498,6 +502,8 @@ The ``__init__`` method for the ``Context`` class takes a parameter called


.. code-block:: python .. code-block:: python


from django.template import Context

def render(self, context): def render(self, context):
# ... # ...
new_context = Context({'var': obj}, autoescape=context.autoescape) new_context = Context({'var': obj}, autoescape=context.autoescape)
Expand Down Expand Up @@ -545,7 +551,10 @@ A naive implementation of ``CycleNode`` might look something like this:


.. code-block:: python .. code-block:: python


class CycleNode(Node): import itertools
from django import template

class CycleNode(template.Node):
def __init__(self, cyclevars): def __init__(self, cyclevars):
self.cycle_iter = itertools.cycle(cyclevars) self.cycle_iter = itertools.cycle(cyclevars)
def render(self, context): def render(self, context):
Expand Down Expand Up @@ -576,7 +585,7 @@ Let's refactor our ``CycleNode`` implementation to use the ``render_context``:


.. code-block:: python .. code-block:: python


class CycleNode(Node): class CycleNode(template.Node):
def __init__(self, cyclevars): def __init__(self, cyclevars):
self.cyclevars = cyclevars self.cyclevars = cyclevars
def render(self, context): def render(self, context):
Expand Down Expand Up @@ -664,6 +673,7 @@ Now your tag should begin to look like this:
.. code-block:: python .. code-block:: python


from django import template from django import template

def do_format_time(parser, token): def do_format_time(parser, token):
try: try:
# split_contents() knows not to split quoted strings. # split_contents() knows not to split quoted strings.
Expand Down Expand Up @@ -722,6 +732,11 @@ Our earlier ``current_time`` function could thus be written like this:


.. code-block:: python .. code-block:: python


import datetime
from django import template

register = template.Library()

def current_time(format_string): def current_time(format_string):
return datetime.datetime.now().strftime(format_string) return datetime.datetime.now().strftime(format_string)


Expand Down Expand Up @@ -965,6 +980,9 @@ outputting it:


.. code-block:: python .. code-block:: python


import datetime
from django import template

class CurrentTimeNode2(template.Node): class CurrentTimeNode2(template.Node):
def __init__(self, format_string): def __init__(self, format_string):
self.format_string = format_string self.format_string = format_string
Expand Down
66 changes: 54 additions & 12 deletions docs/ref/contrib/admin/index.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ The ``ModelAdmin`` is very flexible. It has several options for dealing with
customizing the interface. All options are defined on the ``ModelAdmin`` customizing the interface. All options are defined on the ``ModelAdmin``
subclass:: subclass::


from django.contrib import admin

class AuthorAdmin(admin.ModelAdmin): class AuthorAdmin(admin.ModelAdmin):
date_hierarchy = 'pub_date' date_hierarchy = 'pub_date'


Expand Down Expand Up @@ -157,6 +159,8 @@ subclass::


For example, let's consider the following model:: For example, let's consider the following model::


from django.db import models

class Author(models.Model): class Author(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
title = models.CharField(max_length=3) title = models.CharField(max_length=3)
Expand All @@ -166,6 +170,8 @@ subclass::
and ``title`` fields, you would specify ``fields`` or ``exclude`` like and ``title`` fields, you would specify ``fields`` or ``exclude`` like
this:: this::


from django.contrib import admin

class AuthorAdmin(admin.ModelAdmin): class AuthorAdmin(admin.ModelAdmin):
fields = ('name', 'title') fields = ('name', 'title')


Expand Down Expand Up @@ -234,6 +240,8 @@ subclass::
A full example, taken from the A full example, taken from the
:class:`django.contrib.flatpages.models.FlatPage` model:: :class:`django.contrib.flatpages.models.FlatPage` model::


from django.contrib import admin

class FlatPageAdmin(admin.ModelAdmin): class FlatPageAdmin(admin.ModelAdmin):
fieldsets = ( fieldsets = (
(None, { (None, {
Expand Down Expand Up @@ -356,6 +364,10 @@ subclass::
If your ``ModelForm`` and ``ModelAdmin`` both define an ``exclude`` If your ``ModelForm`` and ``ModelAdmin`` both define an ``exclude``
option then ``ModelAdmin`` takes precedence:: option then ``ModelAdmin`` takes precedence::


from django import forms
from django.contrib import admin
from myapp.models import Person

class PersonForm(forms.ModelForm): class PersonForm(forms.ModelForm):


class Meta: class Meta:
Expand Down Expand Up @@ -459,6 +471,9 @@ subclass::
the same as the callable, but ``self`` in this context is the model the same as the callable, but ``self`` in this context is the model
instance. Here's a full model example:: instance. Here's a full model example::


from django.db import models
from django.contrib import admin

class Person(models.Model): class Person(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
birthday = models.DateField() birthday = models.DateField()
Expand Down Expand Up @@ -494,6 +509,8 @@ subclass::


Here's a full example model:: Here's a full example model::


from django.db import models
from django.contrib import admin
from django.utils.html import format_html from django.utils.html import format_html


class Person(models.Model): class Person(models.Model):
Expand All @@ -519,6 +536,9 @@ subclass::


Here's a full example model:: Here's a full example model::


from django.db import models
from django.contrib import admin

class Person(models.Model): class Person(models.Model):
first_name = models.CharField(max_length=50) first_name = models.CharField(max_length=50)
birthday = models.DateField() birthday = models.DateField()
Expand Down Expand Up @@ -547,6 +567,8 @@ subclass::


For example:: For example::


from django.db import models
from django.contrib import admin
from django.utils.html import format_html from django.utils.html import format_html


class Person(models.Model): class Person(models.Model):
Expand Down Expand Up @@ -634,13 +656,13 @@ subclass::
``BooleanField``, ``CharField``, ``DateField``, ``DateTimeField``, ``BooleanField``, ``CharField``, ``DateField``, ``DateTimeField``,
``IntegerField``, ``ForeignKey`` or ``ManyToManyField``, for example:: ``IntegerField``, ``ForeignKey`` or ``ManyToManyField``, for example::


class PersonAdmin(ModelAdmin): class PersonAdmin(admin.ModelAdmin):
list_filter = ('is_staff', 'company') list_filter = ('is_staff', 'company')


Field names in ``list_filter`` can also span relations Field names in ``list_filter`` can also span relations
using the ``__`` lookup, for example:: using the ``__`` lookup, for example::


class PersonAdmin(UserAdmin): class PersonAdmin(admin.UserAdmin):
list_filter = ('company__name',) list_filter = ('company__name',)


* a class inheriting from ``django.contrib.admin.SimpleListFilter``, * a class inheriting from ``django.contrib.admin.SimpleListFilter``,
Expand All @@ -650,10 +672,10 @@ subclass::


from datetime import date from datetime import date


from django.contrib import admin
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter


class DecadeBornListFilter(SimpleListFilter): class DecadeBornListFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the # Human-readable title which will be displayed in the
# right admin sidebar just above the filter options. # right admin sidebar just above the filter options.
title = _('decade born') title = _('decade born')
Expand Down Expand Up @@ -689,7 +711,7 @@ subclass::
return queryset.filter(birthday__gte=date(1990, 1, 1), return queryset.filter(birthday__gte=date(1990, 1, 1),
birthday__lte=date(1999, 12, 31)) birthday__lte=date(1999, 12, 31))


class PersonAdmin(ModelAdmin): class PersonAdmin(admin.ModelAdmin):
list_filter = (DecadeBornListFilter,) list_filter = (DecadeBornListFilter,)


.. note:: .. note::
Expand Down Expand Up @@ -732,11 +754,9 @@ subclass::
element is a class inheriting from element is a class inheriting from
``django.contrib.admin.FieldListFilter``, for example:: ``django.contrib.admin.FieldListFilter``, for example::


from django.contrib.admin import BooleanFieldListFilter class PersonAdmin(admin.ModelAdmin):

class PersonAdmin(ModelAdmin):
list_filter = ( list_filter = (
('is_staff', BooleanFieldListFilter), ('is_staff', admin.BooleanFieldListFilter),
) )


.. note:: .. note::
Expand All @@ -746,7 +766,7 @@ subclass::


It is possible to specify a custom template for rendering a list filter:: It is possible to specify a custom template for rendering a list filter::


class FilterWithCustomTemplate(SimpleListFilter): class FilterWithCustomTemplate(admin.SimpleListFilter):
template = "custom_template.html" template = "custom_template.html"


See the default template provided by django (``admin/filter.html``) for See the default template provided by django (``admin/filter.html``) for
Expand Down Expand Up @@ -876,10 +896,11 @@ subclass::
the admin interface to provide feedback on the status of the objects being the admin interface to provide feedback on the status of the objects being
edited, for example:: edited, for example::


from django.contrib import admin
from django.utils.html import format_html_join from django.utils.html import format_html_join
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe


class PersonAdmin(ModelAdmin): class PersonAdmin(admin.ModelAdmin):
readonly_fields = ('address_report',) readonly_fields = ('address_report',)


def address_report(self, instance): def address_report(self, instance):
Expand Down Expand Up @@ -1038,6 +1059,8 @@ templates used by the :class:`ModelAdmin` views:


For example to attach ``request.user`` to the object prior to saving:: For example to attach ``request.user`` to the object prior to saving::


from django.contrib import admin

class ArticleAdmin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change): def save_model(self, request, obj, form, change):
obj.user = request.user obj.user = request.user
Expand Down Expand Up @@ -1071,7 +1094,7 @@ templates used by the :class:`ModelAdmin` views:
is expected to return a ``list`` or ``tuple`` for ordering similar is expected to return a ``list`` or ``tuple`` for ordering similar
to the :attr:`ordering` attribute. For example:: to the :attr:`ordering` attribute. For example::


class PersonAdmin(ModelAdmin): class PersonAdmin(admin.ModelAdmin):


def get_ordering(self, request): def get_ordering(self, request):
if request.user.is_superuser: if request.user.is_superuser:
Expand Down Expand Up @@ -1298,6 +1321,8 @@ templates used by the :class:`ModelAdmin` views:
Returns a :class:`~django.forms.ModelForm` class for use in the ``Formset`` Returns a :class:`~django.forms.ModelForm` class for use in the ``Formset``
on the changelist page. To use a custom form, for example:: on the changelist page. To use a custom form, for example::


from django import forms

class MyForm(forms.ModelForm): class MyForm(forms.ModelForm):
pass pass


Expand Down Expand Up @@ -1539,6 +1564,8 @@ information.
The admin interface has the ability to edit models on the same page as a The admin interface has the ability to edit models on the same page as a
parent model. These are called inlines. Suppose you have these two models:: parent model. These are called inlines. Suppose you have these two models::


from django.db import models

class Author(models.Model): class Author(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)


Expand All @@ -1549,6 +1576,8 @@ information.
You can edit the books authored by an author on the author page. You add You can edit the books authored by an author on the author page. You add
inlines to a model by specifying them in a ``ModelAdmin.inlines``:: inlines to a model by specifying them in a ``ModelAdmin.inlines``::


from django.contrib import admin

class BookInline(admin.TabularInline): class BookInline(admin.TabularInline):
model = Book model = Book


Expand Down Expand Up @@ -1682,6 +1711,8 @@ Working with a model with two or more foreign keys to the same parent model
It is sometimes possible to have more than one foreign key to the same model. It is sometimes possible to have more than one foreign key to the same model.
Take this model for instance:: Take this model for instance::


from django.db import models

class Friendship(models.Model): class Friendship(models.Model):
to_person = models.ForeignKey(Person, related_name="friends") to_person = models.ForeignKey(Person, related_name="friends")
from_person = models.ForeignKey(Person, related_name="from_friends") from_person = models.ForeignKey(Person, related_name="from_friends")
Expand All @@ -1690,6 +1721,9 @@ If you wanted to display an inline on the ``Person`` admin add/change pages
you need to explicitly define the foreign key since it is unable to do so you need to explicitly define the foreign key since it is unable to do so
automatically:: automatically::


from django.contrib import admin
from myapp.models import Friendship

class FriendshipInline(admin.TabularInline): class FriendshipInline(admin.TabularInline):
model = Friendship model = Friendship
fk_name = "to_person" fk_name = "to_person"
Expand All @@ -1712,6 +1746,8 @@ widgets with inlines.


Suppose we have the following models:: Suppose we have the following models::


from django.db import models

class Person(models.Model): class Person(models.Model):
name = models.CharField(max_length=128) name = models.CharField(max_length=128)


Expand All @@ -1722,6 +1758,8 @@ Suppose we have the following models::
If you want to display many-to-many relations using an inline, you can do If you want to display many-to-many relations using an inline, you can do
so by defining an ``InlineModelAdmin`` object for the relationship:: so by defining an ``InlineModelAdmin`` object for the relationship::


from django.contrib import admin

class MembershipInline(admin.TabularInline): class MembershipInline(admin.TabularInline):
model = Group.members.through model = Group.members.through


Expand Down Expand Up @@ -1768,6 +1806,8 @@ However, we still want to be able to edit that information inline. Fortunately,
this is easy to do with inline admin models. Suppose we have the following this is easy to do with inline admin models. Suppose we have the following
models:: models::


from django.db import models

class Person(models.Model): class Person(models.Model):
name = models.CharField(max_length=128) name = models.CharField(max_length=128)


Expand Down Expand Up @@ -1816,6 +1856,8 @@ Using generic relations as an inline
It is possible to use an inline with generically related objects. Let's say It is possible to use an inline with generically related objects. Let's say
you have the following models:: you have the following models::


from django.db import models

class Image(models.Model): class Image(models.Model):
image = models.ImageField(upload_to="images") image = models.ImageField(upload_to="images")
content_type = models.ForeignKey(ContentType) content_type = models.ForeignKey(ContentType)
Expand Down
1 change: 1 addition & 0 deletions docs/ref/contrib/csrf.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ Utilities
the middleware. Example:: the middleware. Example::


from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse


@csrf_exempt @csrf_exempt
def my_view(request): def my_view(request):
Expand Down
1 change: 1 addition & 0 deletions docs/ref/contrib/formtools/form-preview.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ How to use ``FormPreview``
overrides the ``done()`` method:: overrides the ``done()`` method::


from django.contrib.formtools.preview import FormPreview from django.contrib.formtools.preview import FormPreview
from django.http import HttpResponseRedirect
from myapp.models import SomeModel from myapp.models import SomeModel


class SomeModelFormPreview(FormPreview): class SomeModelFormPreview(FormPreview):
Expand Down
Loading

0 comments on commit c6855e8

Please sign in to comment.