Fixed #1399 -- Added template_object_name hook to generic views. Thanks, ChaosKCW

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2453 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-03-01 03:37:57 +00:00
parent ace140662b
commit 6330e286c3
5 changed files with 64 additions and 19 deletions

View File

@ -45,6 +45,7 @@ answer newbie questions, and generally made Django that much better:
Antonio Cavedoni <http://cavedoni.com/> Antonio Cavedoni <http://cavedoni.com/>
C8E C8E
Amit Chakradeo <http://amit.chakradeo.net/> Amit Chakradeo <http://amit.chakradeo.net/>
ChaosKCW
Matt Croydon <http://www.postneo.com/> Matt Croydon <http://www.postneo.com/>
Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/> Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
Jason Davies (Esaj) <http://www.jasondavies.com/> Jason Davies (Esaj) <http://www.jasondavies.com/>

View File

@ -73,7 +73,8 @@ def create_object(request, app_label, module_name, template_name=None,
def update_object(request, app_label, module_name, object_id=None, slug=None, def update_object(request, app_label, module_name, object_id=None, slug=None,
slug_field=None, template_name=None, template_loader=loader, slug_field=None, template_name=None, template_loader=loader,
extra_lookup_kwargs={}, extra_context={}, post_save_redirect=None, extra_lookup_kwargs={}, extra_context={}, post_save_redirect=None,
login_required=False, follow=None, context_processors=None): login_required=False, follow=None, context_processors=None,
template_object_name='object'):
""" """
Generic object-update function. Generic object-update function.
@ -133,7 +134,7 @@ def update_object(request, app_label, module_name, object_id=None, slug=None,
t = template_loader.get_template(template_name) t = template_loader.get_template(template_name)
c = DjangoContext(request, { c = DjangoContext(request, {
'form': form, 'form': form,
'object': object, template_object_name: object,
}, context_processors) }, context_processors)
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):
@ -147,7 +148,7 @@ def update_object(request, app_label, module_name, object_id=None, slug=None,
def delete_object(request, app_label, module_name, post_delete_redirect, def delete_object(request, app_label, module_name, post_delete_redirect,
object_id=None, slug=None, slug_field=None, template_name=None, object_id=None, slug=None, slug_field=None, template_name=None,
template_loader=loader, extra_lookup_kwargs={}, extra_context={}, template_loader=loader, extra_lookup_kwargs={}, extra_context={},
login_required=False, context_processors=None): login_required=False, context_processors=None, template_object_name='object'):
""" """
Generic object-delete function. Generic object-delete function.
@ -189,7 +190,7 @@ def delete_object(request, app_label, module_name, post_delete_redirect,
template_name = "%s/%s_confirm_delete" % (app_label, module_name) template_name = "%s/%s_confirm_delete" % (app_label, module_name)
t = template_loader.get_template(template_name) t = template_loader.get_template(template_name)
c = DjangoContext(request, { c = DjangoContext(request, {
'object': object, template_object_name: object,
}, context_processors) }, context_processors)
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):

View File

@ -89,7 +89,7 @@ def archive_year(request, year, app_label, module_name, date_field,
def archive_month(request, year, month, app_label, module_name, date_field, def archive_month(request, year, month, app_label, module_name, date_field,
month_format='%b', template_name=None, template_loader=loader, month_format='%b', template_name=None, template_loader=loader,
extra_lookup_kwargs={}, extra_context={}, allow_empty=False, extra_lookup_kwargs={}, extra_context={}, allow_empty=False,
context_processors=None): context_processors=None, template_object_name='object'):
""" """
Generic monthly archive view. Generic monthly archive view.
@ -129,7 +129,7 @@ def archive_month(request, year, month, app_label, module_name, date_field,
template_name = "%s/%s_archive_month" % (app_label, module_name) template_name = "%s/%s_archive_month" % (app_label, module_name)
t = template_loader.get_template(template_name) t = template_loader.get_template(template_name)
c = DjangoContext(request, { c = DjangoContext(request, {
'object_list': object_list, '%s_list' % template_object_name: object_list,
'month': date, 'month': date,
'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None, 'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,
'previous_month': first_day - datetime.timedelta(days=1), 'previous_month': first_day - datetime.timedelta(days=1),
@ -144,7 +144,7 @@ def archive_month(request, year, month, app_label, module_name, date_field,
def archive_day(request, year, month, day, app_label, module_name, date_field, def archive_day(request, year, month, day, app_label, module_name, date_field,
month_format='%b', day_format='%d', template_name=None, month_format='%b', day_format='%d', template_name=None,
template_loader=loader, extra_lookup_kwargs={}, extra_context={}, template_loader=loader, extra_lookup_kwargs={}, extra_context={},
allow_empty=False, context_processors=None): allow_empty=False, context_processors=None, template_object_name='object'):
""" """
Generic daily archive view. Generic daily archive view.
@ -180,7 +180,7 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
template_name = "%s/%s_archive_day" % (app_label, module_name) template_name = "%s/%s_archive_day" % (app_label, module_name)
t = template_loader.get_template(template_name) t = template_loader.get_template(template_name)
c = DjangoContext(request, { c = DjangoContext(request, {
'object_list': object_list, '%s_list' % template_object_name: object_list,
'day': date, 'day': date,
'previous_day': date - datetime.timedelta(days=1), 'previous_day': date - datetime.timedelta(days=1),
'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None, 'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,
@ -208,7 +208,7 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
month_format='%b', day_format='%d', object_id=None, slug=None, month_format='%b', day_format='%d', object_id=None, slug=None,
slug_field=None, template_name=None, template_name_field=None, slug_field=None, template_name=None, template_name_field=None,
template_loader=loader, extra_lookup_kwargs={}, extra_context={}, template_loader=loader, extra_lookup_kwargs={}, extra_context={},
context_processors=None): context_processors=None, template_object_name='object'):
""" """
Generic detail view from year/month/day/slug or year/month/day/id structure. Generic detail view from year/month/day/slug or year/month/day/id structure.
@ -249,7 +249,7 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
else: else:
t = template_loader.get_template(template_name) t = template_loader.get_template(template_name)
c = DjangoContext(request, { c = DjangoContext(request, {
'object': object, template_object_name: object,
}, context_processors) }, context_processors)
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):

View File

@ -8,7 +8,7 @@ from django.core.exceptions import Http404, ObjectDoesNotExist
def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False, def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False,
template_name=None, template_loader=loader, extra_lookup_kwargs={}, template_name=None, template_loader=loader, extra_lookup_kwargs={},
extra_context={}, context_processors=None): extra_context={}, context_processors=None, template_object_name='object'):
""" """
Generic list of objects. Generic list of objects.
@ -49,7 +49,7 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
else: else:
raise Http404 raise Http404
c = DjangoContext(request, { c = DjangoContext(request, {
'object_list': object_list, '%s_list' % template_object_name: object_list,
'is_paginated': paginator.pages > 1, 'is_paginated': paginator.pages > 1,
'results_per_page': paginate_by, 'results_per_page': paginate_by,
'has_next': paginator.has_next_page(page - 1), 'has_next': paginator.has_next_page(page - 1),
@ -63,7 +63,7 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
else: else:
object_list = mod.get_list(**lookup_kwargs) object_list = mod.get_list(**lookup_kwargs)
c = DjangoContext(request, { c = DjangoContext(request, {
'object_list': object_list, '%s_list' % template_object_name: object_list,
'is_paginated': False 'is_paginated': False
}, context_processors) }, context_processors)
if len(object_list) == 0 and not allow_empty: if len(object_list) == 0 and not allow_empty:
@ -81,7 +81,7 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
def object_detail(request, app_label, module_name, object_id=None, slug=None, def object_detail(request, app_label, module_name, object_id=None, slug=None,
slug_field=None, template_name=None, template_name_field=None, slug_field=None, template_name=None, template_name_field=None,
template_loader=loader, extra_lookup_kwargs={}, extra_context={}, template_loader=loader, extra_lookup_kwargs={}, extra_context={},
context_processors=None): context_processors=None, template_object_name='object'):
""" """
Generic list of objects. Generic list of objects.
@ -111,7 +111,7 @@ def object_detail(request, app_label, module_name, object_id=None, slug=None,
else: else:
t = template_loader.get_template(template_name) t = template_loader.get_template(template_name)
c = DjangoContext(request, { c = DjangoContext(request, {
'object': object, template_object_name: object,
}, context_processors) }, context_processors)
for key, value in extra_context.items(): for key, value in extra_context.items():
if callable(value): if callable(value):

View File

@ -193,6 +193,10 @@ The date-based generic functions are:
**New in Django development version:** Takes an optional ``allow_empty`` **New in Django development version:** Takes an optional ``allow_empty``
parameter, as ``archive_index``. parameter, as ``archive_index``.
**New in Django development version:** Takes an optional
``template_object_name`` parameter, which designates the name of the
template variable to use. Default is ``'object'``.
Uses the template ``app_label/module_name_archive_month`` by default. Uses the template ``app_label/module_name_archive_month`` by default.
Has the following template context: Has the following template context:
@ -207,7 +211,11 @@ The date-based generic functions are:
**New in Django development version.** The first day of the **New in Django development version.** The first day of the
previous month (a datetime.date object) previous month (a datetime.date object)
``object_list`` ``object_list``
List of objects published in the given month List of objects published in the given month.
In the Django development version, you can change this variable
name from ``object_list`` by using the ``template_object_name``
parameter. (See above.) For example, if ``template_object_name`` is
``foo``, the variable will be ``foo_list``.
``archive_day`` ``archive_day``
Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be
@ -217,12 +225,20 @@ The date-based generic functions are:
also pass ``day_format``, which defaults to ``"%d"`` (day of the month as a also pass ``day_format``, which defaults to ``"%d"`` (day of the month as a
decimal number, 1-31). decimal number, 1-31).
**New in Django development version:** Takes an optional
``template_object_name`` parameter, which designates the name of the
template variable to use. Default is ``'object'``.
Uses the template ``app_label/module_name_archive_day`` by default. Uses the template ``app_label/module_name_archive_day`` by default.
Has the following template context: Has the following template context:
``object_list`` ``object_list``
List of objects published this day List of objects published on the given day.
In the Django development version, you can change this variable
name from ``object_list`` by using the ``template_object_name``
parameter. (See above.) For example, if ``template_object_name`` is
``foo``, the variable will be ``foo_list``.
``day`` ``day``
The given day (a datetime.datetime object) The given day (a datetime.datetime object)
``previous_day`` ``previous_day``
@ -254,6 +270,10 @@ The date-based generic functions are:
As in ``archive_day``, ``object_detail`` takes optional ``month_format`` As in ``archive_day``, ``object_detail`` takes optional ``month_format``
and ``day_format`` parameters. and ``day_format`` parameters.
**New in Django development version:** Takes an optional
``template_object_name`` parameter, which designates the name of the
template variable to use. Default is ``'object'``.
.. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941 .. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
Using list/detail generic views Using list/detail generic views
@ -285,6 +305,9 @@ Individual views are:
``allow_empty`` If ``False`` and there are no objects to display, ``allow_empty`` If ``False`` and there are no objects to display,
the view will raise a 404 instead of displaying the view will raise a 404 instead of displaying
an empty index page. ``False`` is default. an empty index page. ``False`` is default.
``template_object_name`` **New in Django development version.** Designates
the name of the object template variable. Default
is ``'object'``.
======================= ================================================= ======================= =================================================
Uses the template ``app_label/module_name_list`` by default. Uses the template ``app_label/module_name_list`` by default.
@ -292,7 +315,11 @@ Individual views are:
Has the following template context: Has the following template context:
``object_list`` ``object_list``
List of objects List of objects. In the Django development version, you can change
this variable name from ``object_list`` by using the
``template_object_name`` parameter. (See above.) For example, if
``template_object_name`` is ``foo``, the variable will be
``foo_list``.
``is_paginated`` ``is_paginated``
Are the results paginated? Either True or False Are the results paginated? Either True or False
@ -362,6 +389,10 @@ The create/update/delete views are:
``list_detail.object_detail`` does (see above), and the same ``list_detail.object_detail`` does (see above), and the same
``post_save_redirect`` as ``create_object`` does. ``post_save_redirect`` as ``create_object`` does.
**New in Django development version:** Takes an optional
``template_object_name`` parameter, which designates the name of the
template variable to use. Default is ``'object'``.
Uses the template ``app_label/module_name_form`` by default. Uses the template ``app_label/module_name_form`` by default.
Has the following template context: Has the following template context:
@ -369,7 +400,11 @@ The create/update/delete views are:
form form
The form wrapper for the object The form wrapper for the object
object object
The original object being edited The original object being edited.
In the Django development version, you can change this variable
name from ``object`` by using the ``template_object_name``
parameter. (See above.) For example, if ``template_object_name`` is
``foo``, the variable will be ``foo`` instead of ``object``.
``delete_object`` ``delete_object``
Delete an existing object. The given object will only actually be deleted Delete an existing object. The given object will only actually be deleted
@ -384,7 +419,15 @@ The create/update/delete views are:
``app_label/module_name_confirm_delete`` by default. It uses no template ``app_label/module_name_confirm_delete`` by default. It uses no template
if POSTed -- it simply deletes the object and redirects. if POSTed -- it simply deletes the object and redirects.
**New in Django development version:** Takes an optional
``template_object_name`` parameter, which designates the name of the
template variable to use. Default is ``'object'``.
Has the following template context: Has the following template context:
object object
The object about to be deleted The object about to be deleted
In the Django development version, you can change this variable
name from ``object`` by using the ``template_object_name``
parameter. (See above.) For example, if ``template_object_name`` is
``foo``, the variable will be ``foo`` instead of ``object``.