Opened 5 years ago

Closed 4 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: master
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)

feedviews.diff (1.8 KB) - added by Paul Winkler 5 years ago.

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by Paul Winkler

Attachment: feedviews.diff added

comment:1 Changed 5 years ago by Paul Winkler

Type: UncategorizedNew feature

comment:2 Changed 4 years ago by Aymeric Augustin

Easy pickings: set
Needs documentation: set
Needs tests: set
Triage Stage: UnreviewedAccepted

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.

comment:3 Changed 4 years ago by Paul Winkler

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 Changed 4 years ago by Aymeric Augustin

This makes sense. Let's explain it in the documentation.

The patch just needs tests and docs before it can be committed.

comment:5 Changed 4 years ago by Zbigniew Siciarz

Owner: changed from nobody to Zbigniew Siciarz
Status: newassigned

comment:6 Changed 4 years ago by Zbigniew Siciarz

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 Changed 4 years ago by Zbigniew Siciarz

Resolution: fixed
Status: assignedclosed

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.

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