mirror of https://github.com/django/django.git
Fixed #195: generic views now allow callables in extra_context dicts -- thanks, Moof!
git-svn-id: http://code.djangoproject.com/svn/django/trunk@307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
f6c4395329
commit
6c43b0e06c
|
@ -8,7 +8,7 @@ from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
|
||||||
from django.core.exceptions import Http404, ObjectDoesNotExist, ImproperlyConfigured
|
from django.core.exceptions import Http404, ObjectDoesNotExist, ImproperlyConfigured
|
||||||
|
|
||||||
def create_object(request, app_label, module_name, template_name=None,
|
def create_object(request, app_label, module_name, template_name=None,
|
||||||
extra_context=None, post_save_redirect=None, login_required=False):
|
extra_context={}, post_save_redirect=None, login_required=False):
|
||||||
"""
|
"""
|
||||||
Generic object-creation function.
|
Generic object-creation function.
|
||||||
|
|
||||||
|
@ -57,13 +57,16 @@ def create_object(request, app_label, module_name, template_name=None,
|
||||||
c = Context(request, {
|
c = Context(request, {
|
||||||
'form' : form,
|
'form' : form,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
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, extra_lookup_kwargs={},
|
slug_field=None, template_name=None, extra_lookup_kwargs={},
|
||||||
extra_context=None, post_save_redirect=None, login_required=False):
|
extra_context={}, post_save_redirect=None, login_required=False):
|
||||||
"""
|
"""
|
||||||
Generic object-update function.
|
Generic object-update function.
|
||||||
|
|
||||||
|
@ -125,15 +128,18 @@ def update_object(request, app_label, module_name, object_id=None, slug=None,
|
||||||
'form' : form,
|
'form' : form,
|
||||||
'object' : object,
|
'object' : object,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
response = HttpResponse(t.render(c))
|
response = HttpResponse(t.render(c))
|
||||||
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
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,
|
||||||
extra_lookup_kwargs={}, extra_context=None, login_required=False):
|
extra_lookup_kwargs={}, extra_context={}, login_required=False):
|
||||||
"""
|
"""
|
||||||
Generic object-delete function.
|
Generic object-delete function.
|
||||||
|
|
||||||
|
@ -177,8 +183,11 @@ def delete_object(request, app_label, module_name, post_delete_redirect,
|
||||||
c = Context(request, {
|
c = Context(request, {
|
||||||
'object' : object,
|
'object' : object,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
response = HttpResponse(t.render(c))
|
response = HttpResponse(t.render(c))
|
||||||
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -7,7 +7,7 @@ from django.utils.httpwrappers import HttpResponse
|
||||||
import datetime, time
|
import datetime, time
|
||||||
|
|
||||||
def archive_index(request, app_label, module_name, date_field, num_latest=15,
|
def archive_index(request, app_label, module_name, date_field, num_latest=15,
|
||||||
template_name=None, extra_lookup_kwargs={}, extra_context=None):
|
template_name=None, extra_lookup_kwargs={}, extra_context={}):
|
||||||
"""
|
"""
|
||||||
Generic top-level archive of date-based objects.
|
Generic top-level archive of date-based objects.
|
||||||
|
|
||||||
|
@ -41,12 +41,15 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15,
|
||||||
'date_list' : date_list,
|
'date_list' : date_list,
|
||||||
'latest' : latest,
|
'latest' : latest,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
def archive_year(request, year, app_label, module_name, date_field,
|
def archive_year(request, year, app_label, module_name, date_field,
|
||||||
template_name=None, extra_lookup_kwargs={}, extra_context=None):
|
template_name=None, extra_lookup_kwargs={}, extra_context={}):
|
||||||
"""
|
"""
|
||||||
Generic yearly archive view.
|
Generic yearly archive view.
|
||||||
|
|
||||||
|
@ -74,12 +77,15 @@ def archive_year(request, year, app_label, module_name, date_field,
|
||||||
'date_list': date_list,
|
'date_list': date_list,
|
||||||
'year': year,
|
'year': year,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
def archive_month(request, year, month, app_label, module_name, date_field,
|
def archive_month(request, year, month, app_label, module_name, date_field,
|
||||||
template_name=None, extra_lookup_kwargs={}, extra_context=None):
|
template_name=None, extra_lookup_kwargs={}, extra_context={}):
|
||||||
"""
|
"""
|
||||||
Generic monthly archive view.
|
Generic monthly archive view.
|
||||||
|
|
||||||
|
@ -121,12 +127,15 @@ def archive_month(request, year, month, app_label, module_name, date_field,
|
||||||
'object_list': object_list,
|
'object_list': object_list,
|
||||||
'month': date,
|
'month': date,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
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,
|
||||||
template_name=None, extra_lookup_kwargs={}, extra_context=None,
|
template_name=None, extra_lookup_kwargs={}, extra_context={},
|
||||||
allow_empty=False):
|
allow_empty=False):
|
||||||
"""
|
"""
|
||||||
Generic daily archive view.
|
Generic daily archive view.
|
||||||
|
@ -167,8 +176,11 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
|
||||||
'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,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
return HttpResponse(t.render(c))
|
return HttpResponse(t.render(c))
|
||||||
|
|
||||||
def archive_today(request, **kwargs):
|
def archive_today(request, **kwargs):
|
||||||
|
@ -185,7 +197,7 @@ def archive_today(request, **kwargs):
|
||||||
|
|
||||||
def object_detail(request, year, month, day, app_label, module_name, date_field,
|
def object_detail(request, year, month, day, app_label, module_name, date_field,
|
||||||
object_id=None, slug=None, slug_field=None, template_name=None,
|
object_id=None, slug=None, slug_field=None, template_name=None,
|
||||||
template_name_field=None, extra_lookup_kwargs={}, extra_context=None):
|
template_name_field=None, extra_lookup_kwargs={}, extra_context={}):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
|
|
||||||
|
@ -227,8 +239,11 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
|
||||||
c = Context(request, {
|
c = Context(request, {
|
||||||
'object': object,
|
'object': object,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
response = HttpResponse(t.render(c))
|
response = HttpResponse(t.render(c))
|
||||||
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
||||||
return response
|
return response
|
||||||
|
|
|
@ -7,7 +7,7 @@ from django.core.paginator import ObjectPaginator, InvalidPage
|
||||||
from django.core.exceptions import Http404, ObjectDoesNotExist
|
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, extra_lookup_kwargs={}, extra_context=None):
|
template_name=None, extra_lookup_kwargs={}, extra_context={}):
|
||||||
"""
|
"""
|
||||||
Generic list of objects.
|
Generic list of objects.
|
||||||
|
|
||||||
|
@ -61,8 +61,11 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
|
||||||
})
|
})
|
||||||
if len(object_list) == 0 and not allow_empty:
|
if len(object_list) == 0 and not allow_empty:
|
||||||
raise Http404
|
raise Http404
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
if not template_name:
|
if not template_name:
|
||||||
template_name = "%s/%s_list" % (app_label, module_name)
|
template_name = "%s/%s_list" % (app_label, module_name)
|
||||||
t = template_loader.get_template(template_name)
|
t = template_loader.get_template(template_name)
|
||||||
|
@ -70,7 +73,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,
|
||||||
extra_lookup_kwargs={}, extra_context=None):
|
extra_lookup_kwargs={}, extra_context={}):
|
||||||
"""
|
"""
|
||||||
Generic list of objects.
|
Generic list of objects.
|
||||||
|
|
||||||
|
@ -102,8 +105,11 @@ def object_detail(request, app_label, module_name, object_id=None, slug=None,
|
||||||
c = Context(request, {
|
c = Context(request, {
|
||||||
'object' : object,
|
'object' : object,
|
||||||
})
|
})
|
||||||
if extra_context:
|
for key, value in extra_context.items():
|
||||||
c.update(extra_context)
|
if callable(value):
|
||||||
|
c[key] = value()
|
||||||
|
else:
|
||||||
|
c[key] = value
|
||||||
response = HttpResponse(t.render(c))
|
response = HttpResponse(t.render(c))
|
||||||
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
|
||||||
return response
|
return response
|
||||||
|
|
Loading…
Reference in New Issue