Opened 13 years ago
Closed 11 years ago
#18112 closed New feature (fixed)
contrib.syndication.views: Add hook for getting extra context in feed templates
Reported by: | Paul Winkler | Owned by: | Zbigniew Siciarz |
---|---|---|---|
Component: | contrib.syndication | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
This seems to me like a useful low-impact hook for authors of Feed subclasses...
I was writing one today and wished I had such a hook.
It would be intentionally similar to the get_context_data
hook on class-based views.
Usage would look like:
class MyFeed(Feed): def get_context_data(**kwargs): context = super(MyFeed, self).get_context_data(**kwargs) context['foo'] = settings.FOO context['more_things'] = app.models.Things.objects.filter(...) return context
The description_template and title_template could then make use of the extra context variables.
Attaching a patch; will write tests if this seems worthwhile.
Attachments (1)
Change History (8)
by , 13 years ago
Attachment: | feedviews.diff added |
---|
comment:1 by , 13 years ago
Type: | Uncategorized → New feature |
---|
comment:2 by , 12 years ago
Easy pickings: | set |
---|---|
Needs documentation: | set |
Needs tests: | set |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 12 years ago
I can see how this is confusing.
Those names were not chosen by me, they are in the current implementation, as the old part of the diff shows.
If you look in the local scope of where I'm invoking this method, the old implementation
has these :
obj
- the object returned by get_object().
This was not being made available to the templates; I added it as the 'obj' kwarg
to get_context_data(), which by default ignores it; but I'm passing it so at least it's possible to use it
in your subclass if you want.
item
- the current feed item from this iteration of obj.items().
This was already being made available to the template context as, very unfortunately, 'obj'.
That kind of sucks, as it's easy to confuse with the one returned by get_object().
But it *has* to be named 'obj' in the template context or we'll break existing templates.
Any thoughts on how to make this less confusing without breaking backward compatibility?
comment:4 by , 12 years ago
This makes sense. Let's explain it in the documentation.
The patch just needs tests and docs before it can be committed.
comment:5 by , 12 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 12 years ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
I added a test case and a few words of documentation to the initial patch. See https://github.com/django/django/pull/826
comment:7 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Hmm, I guess trac did not pick up closing commit from GitHub.
In 0a8402eb052a5c35085baa5408aaf4ee36ebc0a6:
Fixed #18112: Test case and docs for custom context data in feeds; Thanks Paul Winkler for the initial patch.
I don't really understand the
obj
vs.item
dance; could you clarify why you chose this implementation?Otherwise, this looks like a good idea.