Feed.get_context_data(**kwargs)
There is also a way to pass additional information to title and description templates, if you need to supply more than the two variables mentioned before. You can provide your implementation of get_context_data
method in your Feed
subclass. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from mysite.models import Article from django.contrib.syndication.views import Feed class ArticlesFeed(Feed): title = "My articles" description_template = "feeds/articles.html" def items( self ): return Article.objects.order_by( '-pub_date' )[: 5 ] def get_context_data( self , * * kwargs): context = super (ArticlesFeed, self ).get_context_data( * * kwargs) context[ 'foo' ] = 'bar' return context |
And the template:
1 | Something about {{ foo }}: {{ obj.description }} |
This method will be called once per each item in the list returned by items()
with the following keyword arguments:
-
item
: the current item. For backward compatibility reasons, the name of this context variable is{{ obj }}
. -
obj
: the object returned byget_object()
. By default this is not exposed to the templates to avoid confusion with{{ obj }}
(see above), but you can use it in your implementation ofget_context_data()
. -
site
: current site as described above. -
request
: current request.
The behavior of get_context_data()
mimics that of generic views - you’re supposed to call super()
to retrieve context data from parent class, add your data and return the modified dictionary.
Please login to continue.