Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#195 closed defect (fixed)

Patch: Allow callables in the extra_context dictionary of django.views.generic.date_based

Reported by: Moof <moof@…> Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Sometimes you want to add extra context to your generic views that needs to be recalculated every time.

For example you currently have <li><a href="/weblog/2005/jul/">July 2005</a></li> in the static part of source:/djangoproject.com/django_website/templates/base_weblog.html because you can't pass an auto-generated list of months into the template, as it would get generated when the url map is generated.

The easiest way to solve this is to allow callables in the extra_context dictionary. Define a function or lambda which returns the data you want in the context when it's called, and then put a reference to it in the extra_context dict.

Example adapted from source:/djangoproject.com/django_website/apps/blog/urls/blog.py :

info_dict = {
    'app_label': 'blog',
    'module_name': 'entries',
    'date_field': 'pub_date',
    'extra_context': { 'month_list': 
        lambda: reverse(entries.get_pub_date_list('month', 
                            pub_date__lte=datetime.datetime.now())) }
}

Attached is a patch that enables this behaviour

Attachments (1)

date_based_callable_context.patch (2.2 KB ) - added by Moof <moof@…> 19 years ago.
Patch against r304 of date_based.py

Download all attachments as: .zip

Change History (2)

by Moof <moof@…>, 19 years ago

Patch against r304 of date_based.py

comment:1 by Jacob, 19 years ago

Resolution: fixed
Status: newclosed

(In [307]) Fixed #195: generic views now allow callables in extra_context dicts -- thanks, Moof!

Note: See TracTickets for help on using tickets.
Back to Top