summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorZbigniew Siciarz <antyqjon@gmail.com>2013-02-24 15:00:34 +0100
committerJacob Kaplan-Moss <jacob@jacobian.org>2013-02-24 08:33:20 -0600
commit0a8402eb052a5c35085baa5408aaf4ee36ebc0a6 (patch)
treeda243369985c25bd5b0a34e17560c81036317e16 /docs
parent4506ae0497d388f8bc118b9f6f916a5da48d599a (diff)
Test case and docs for custom context data in feeds
Thanks Paul Winkler for the initial patch. (Ref #18112).
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/contrib/syndication.txt54
-rw-r--r--docs/topics/class-based-views/generic-display.txt2
2 files changed, 56 insertions, 0 deletions
diff --git a/docs/ref/contrib/syndication.txt b/docs/ref/contrib/syndication.txt
index 65aa7b57b4..02159c415b 100644
--- a/docs/ref/contrib/syndication.txt
+++ b/docs/ref/contrib/syndication.txt
@@ -137,6 +137,51 @@ into those elements.
See `a complex example`_ below that uses a description template.
+ 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::
+
+ 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:
+
+ .. code-block:: html+django
+
+ 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 by ``get_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 of ``get_context_data()``.
+
+ * ``site``: current site as described above.
+
+ * ``request``: current request.
+
+ The behavior of ``get_context_data()`` mimics that of
+ :ref:`generic views <adding-extra-context>` - you're supposed to call
+ ``super()`` to retrieve context data from parent class, add your data
+ and return the modified dictionary.
+
* To specify the contents of ``<link>``, you have two options. For each item
in ``items()``, Django first tries calling the
``item_link()`` method on the
@@ -599,6 +644,15 @@ This example illustrates all possible attributes and methods for a
item_description = 'A description of the item.' # Hard-coded description.
+ def get_context_data(self, **kwargs):
+ """
+ Returns a dictionary to use as extra context if either
+ description_template or item_template are used.
+
+ Default implementation preserves the old behavior
+ of using {'obj': item, 'site': current_site} as the context.
+ """
+
# ITEM LINK -- One of these three is required. The framework looks for
# them in this order.
diff --git a/docs/topics/class-based-views/generic-display.txt b/docs/topics/class-based-views/generic-display.txt
index 8fe6cd0d65..8695af7fe6 100644
--- a/docs/topics/class-based-views/generic-display.txt
+++ b/docs/topics/class-based-views/generic-display.txt
@@ -188,6 +188,8 @@ Providing a useful ``context_object_name`` is always a good idea. Your
coworkers who design templates will thank you.
+.. _adding-extra-context:
+
Adding extra context
--------------------