Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #1399 -- Added template_object_name hook to generic views. Than…

…ks, ChaosKCW

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2453 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6330e286c3b739f9e6e2210d6568865cf08cf3e1 1 parent ace1406
@adrianholovaty adrianholovaty authored
View
1  AUTHORS
@@ -45,6 +45,7 @@ answer newbie questions, and generally made Django that much better:
Antonio Cavedoni <http://cavedoni.com/>
C8E
Amit Chakradeo <http://amit.chakradeo.net/>
+ ChaosKCW
Matt Croydon <http://www.postneo.com/>
Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
Jason Davies (Esaj) <http://www.jasondavies.com/>
View
9 django/views/generic/create_update.py
@@ -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,
slug_field=None, template_name=None, template_loader=loader,
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.
@@ -133,7 +134,7 @@ def update_object(request, app_label, module_name, object_id=None, slug=None,
t = template_loader.get_template(template_name)
c = DjangoContext(request, {
'form': form,
- 'object': object,
+ template_object_name: object,
}, context_processors)
for key, value in extra_context.items():
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,
object_id=None, slug=None, slug_field=None, template_name=None,
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.
@@ -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)
t = template_loader.get_template(template_name)
c = DjangoContext(request, {
- 'object': object,
+ template_object_name: object,
}, context_processors)
for key, value in extra_context.items():
if callable(value):
View
12 django/views/generic/date_based.py
@@ -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,
month_format='%b', template_name=None, template_loader=loader,
extra_lookup_kwargs={}, extra_context={}, allow_empty=False,
- context_processors=None):
+ context_processors=None, template_object_name='object'):
"""
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)
t = template_loader.get_template(template_name)
c = DjangoContext(request, {
- 'object_list': object_list,
+ '%s_list' % template_object_name: object_list,
'month': date,
'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,
'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,
month_format='%b', day_format='%d', template_name=None,
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.
@@ -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)
t = template_loader.get_template(template_name)
c = DjangoContext(request, {
- 'object_list': object_list,
+ '%s_list' % template_object_name: object_list,
'day': date,
'previous_day': date - datetime.timedelta(days=1),
'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,
slug_field=None, template_name=None, template_name_field=None,
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.
@@ -249,7 +249,7 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
else:
t = template_loader.get_template(template_name)
c = DjangoContext(request, {
- 'object': object,
+ template_object_name: object,
}, context_processors)
for key, value in extra_context.items():
if callable(value):
View
10 django/views/generic/list_detail.py
@@ -8,7 +8,7 @@
def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False,
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.
@@ -49,7 +49,7 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
else:
raise Http404
c = DjangoContext(request, {
- 'object_list': object_list,
+ '%s_list' % template_object_name: object_list,
'is_paginated': paginator.pages > 1,
'results_per_page': paginate_by,
'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:
object_list = mod.get_list(**lookup_kwargs)
c = DjangoContext(request, {
- 'object_list': object_list,
+ '%s_list' % template_object_name: object_list,
'is_paginated': False
}, context_processors)
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,
slug_field=None, template_name=None, template_name_field=None,
template_loader=loader, extra_lookup_kwargs={}, extra_context={},
- context_processors=None):
+ context_processors=None, template_object_name='object'):
"""
Generic list of objects.
@@ -111,7 +111,7 @@ def object_detail(request, app_label, module_name, object_id=None, slug=None,
else:
t = template_loader.get_template(template_name)
c = DjangoContext(request, {
- 'object': object,
+ template_object_name: object,
}, context_processors)
for key, value in extra_context.items():
if callable(value):
View
51 docs/generic_views.txt
@@ -193,6 +193,10 @@ The date-based generic functions are:
**New in Django development version:** Takes an optional ``allow_empty``
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.
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
previous month (a datetime.date object)
``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``
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
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.
Has the following template context:
``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``
The given day (a datetime.datetime object)
``previous_day``
@@ -254,6 +270,10 @@ The date-based generic functions are:
As in ``archive_day``, ``object_detail`` takes optional ``month_format``
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
Using list/detail generic views
@@ -285,6 +305,9 @@ Individual views are:
``allow_empty`` If ``False`` and there are no objects to display,
the view will raise a 404 instead of displaying
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.
@@ -292,7 +315,11 @@ Individual views are:
Has the following template context:
``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``
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
``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.
Has the following template context:
@@ -369,7 +400,11 @@ The create/update/delete views are:
form
The form wrapper for the 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 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
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:
object
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``.

0 comments on commit 6330e28

Please sign in to comment.
Something went wrong with that request. Please try again.