Opened 13 years ago

Closed 13 years ago

Last modified 12 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


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:/ 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:/ :

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

Attached is a patch that enables this behaviour

Attachments (1)

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

Download all attachments as: .zip

Change History (2)

Changed 13 years ago by Moof <moof@…>

Patch against r304 of

comment:1 Changed 13 years ago by Jacob

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