#195 closed defect (fixed)
Patch: Allow callables in the extra_context dictionary of django.views.generic.date_based
Reported by: | 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)
Change History (2)
by , 19 years ago
Attachment: | date_based_callable_context.patch added |
---|
comment:1 by , 19 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch against r304 of date_based.py