2005-11-12 11:44:53 +08:00
|
|
|
==============================
|
|
|
|
The syndication feed framework
|
|
|
|
==============================
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. module:: django.contrib.syndication
|
|
|
|
:synopsis: A framework for generating syndication feeds, in RSS and Atom,
|
|
|
|
quite easily.
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
Django comes with a high-level syndication-feed-generating framework for
|
2020-04-15 19:11:13 +08:00
|
|
|
creating RSS_ and :rfc:`Atom <4287>` feeds.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
To create any syndication feed, all you have to do is write a short
|
|
|
|
Python class. You can create as many feeds as you want.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
Django also comes with a lower-level feed-generating API. Use this if
|
|
|
|
you want to generate feeds outside of a Web context, or in some other
|
|
|
|
lower-level way.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
.. _RSS: http://www.whatisrss.com/
|
|
|
|
|
|
|
|
The high-level framework
|
|
|
|
========================
|
|
|
|
|
|
|
|
Overview
|
|
|
|
--------
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
The high-level feed-generating framework is supplied by the
|
|
|
|
:class:`~django.contrib.syndication.views.Feed` class. To create a
|
|
|
|
feed, write a :class:`~django.contrib.syndication.views.Feed` class
|
2010-08-20 03:27:44 +08:00
|
|
|
and point to an instance of it in your :doc:`URLconf
|
|
|
|
</topics/http/urls>`.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``Feed`` classes
|
|
|
|
----------------
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
A :class:`~django.contrib.syndication.views.Feed` class is a Python
|
|
|
|
class that represents a syndication feed. A feed can be simple (e.g.,
|
|
|
|
a "site news" feed, or a basic feed displaying the latest entries of a
|
|
|
|
blog) or more complex (e.g., a feed displaying all the blog entries in
|
|
|
|
a particular category, where the category is variable).
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
Feed classes subclass :class:`django.contrib.syndication.views.Feed`.
|
|
|
|
They can live anywhere in your codebase.
|
|
|
|
|
|
|
|
Instances of :class:`~django.contrib.syndication.views.Feed` classes
|
2010-08-20 03:27:44 +08:00
|
|
|
are views which can be used in your :doc:`URLconf </topics/http/urls>`.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
A simple example
|
|
|
|
----------------
|
|
|
|
|
2013-08-05 02:45:29 +08:00
|
|
|
This simple example, taken from a hypothetical police beat news site describes
|
|
|
|
a feed of the latest five news items::
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
from django.contrib.syndication.views import Feed
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2013-08-05 02:45:29 +08:00
|
|
|
from policebeat.models import NewsItem
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
class LatestEntriesFeed(Feed):
|
2013-08-05 02:45:29 +08:00
|
|
|
title = "Police beat site news"
|
2005-11-12 11:44:53 +08:00
|
|
|
link = "/sitenews/"
|
2013-08-05 02:45:29 +08:00
|
|
|
description = "Updates on changes and additions to police beat central."
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def items(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return NewsItem.objects.order_by('-pub_date')[:5]
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def item_title(self, item):
|
|
|
|
return item.title
|
|
|
|
|
|
|
|
def item_description(self, item):
|
|
|
|
return item.description
|
|
|
|
|
2012-11-19 07:07:38 +08:00
|
|
|
# item_link is only needed if NewsItem has no get_absolute_url method.
|
|
|
|
def item_link(self, item):
|
|
|
|
return reverse('news-item', args=[item.pk])
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
To connect a URL to this feed, put an instance of the Feed object in
|
2010-08-20 03:27:44 +08:00
|
|
|
your :doc:`URLconf </topics/http/urls>`. For example::
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2010-01-28 21:46:18 +08:00
|
|
|
from myproject.feeds import LatestEntriesFeed
|
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2010-01-28 21:46:18 +08:00
|
|
|
# ...
|
2016-10-21 01:29:04 +08:00
|
|
|
path('latest/feed/', LatestEntriesFeed()),
|
2010-01-28 21:46:18 +08:00
|
|
|
# ...
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
Note:
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
* The Feed class subclasses :class:`django.contrib.syndication.views.Feed`.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2011-03-11 01:21:16 +08:00
|
|
|
* ``title``, ``link`` and ``description`` correspond to the
|
2008-08-24 06:25:40 +08:00
|
|
|
standard RSS ``<title>``, ``<link>`` and ``<description>`` elements,
|
|
|
|
respectively.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
* ``items()`` is, a method that returns a list of objects that should be
|
|
|
|
included in the feed as ``<item>`` elements. Although this example returns
|
|
|
|
``NewsItem`` objects using Django's :doc:`object-relational mapper
|
|
|
|
</ref/models/querysets>`, ``items()`` doesn't have to return model instances.
|
|
|
|
Although you get a few bits of functionality "for free" by using Django
|
|
|
|
models, ``items()`` can return any type of object you want.
|
2006-11-07 09:54:34 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
* If you're creating an Atom feed, rather than an RSS feed, set the
|
2011-03-04 06:20:54 +08:00
|
|
|
``subtitle`` attribute instead of the ``description`` attribute.
|
2008-08-24 06:25:40 +08:00
|
|
|
See `Publishing Atom and RSS feeds in tandem`_, later, for an example.
|
2006-11-07 09:54:34 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
One thing is left to do. In an RSS feed, each ``<item>`` has a ``<title>``,
|
2008-08-24 06:25:40 +08:00
|
|
|
``<link>`` and ``<description>``. We need to tell the framework what data to put
|
|
|
|
into those elements.
|
2006-11-07 09:54:34 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* For the contents of ``<title>`` and ``<description>``, Django tries
|
|
|
|
calling the methods ``item_title()`` and ``item_description()`` on
|
|
|
|
the :class:`~django.contrib.syndication.views.Feed` class. They are passed
|
|
|
|
a single parameter, ``item``, which is the object itself. These are
|
2017-01-21 05:04:05 +08:00
|
|
|
optional; by default, the string representation of the object is used for
|
2011-10-14 08:12:01 +08:00
|
|
|
both.
|
|
|
|
|
|
|
|
If you want to do any special formatting for either the title or
|
2015-01-03 23:10:12 +08:00
|
|
|
description, :doc:`Django templates </ref/templates/language>` can be used
|
2011-10-14 08:12:01 +08:00
|
|
|
instead. Their paths can be specified with the ``title_template`` and
|
|
|
|
``description_template`` attributes on the
|
|
|
|
:class:`~django.contrib.syndication.views.Feed` class. The templates are
|
|
|
|
rendered for each item and are passed two template context variables:
|
|
|
|
|
|
|
|
* ``{{ obj }}`` -- The current object (one of whichever objects you
|
|
|
|
returned in ``items()``).
|
|
|
|
|
|
|
|
* ``{{ site }}`` -- A :class:`django.contrib.sites.models.Site` object
|
|
|
|
representing the current site. This is useful for ``{{ site.domain
|
|
|
|
}}`` or ``{{ site.name }}``. If you do *not* have the Django sites
|
|
|
|
framework installed, this will be set to a
|
2014-01-26 04:54:25 +08:00
|
|
|
:class:`~django.contrib.sites.requests.RequestSite` object. See the
|
2011-10-14 08:12:01 +08:00
|
|
|
:ref:`RequestSite section of the sites framework documentation
|
|
|
|
<requestsite-objects>` for more.
|
|
|
|
|
|
|
|
See `a complex example`_ below that uses a description template.
|
|
|
|
|
2014-01-23 05:17:32 +08:00
|
|
|
.. method:: Feed.get_context_data(**kwargs)
|
2013-02-24 22:00:34 +08:00
|
|
|
|
2013-06-06 00:55:50 +08:00
|
|
|
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::
|
2013-02-24 22:00:34 +08:00
|
|
|
|
2013-06-06 00:55:50 +08:00
|
|
|
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]
|
2013-02-24 22:00:34 +08:00
|
|
|
|
2013-06-06 00:55:50 +08:00
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-22 14:57:14 +08:00
|
|
|
context = super().get_context_data(**kwargs)
|
2013-06-06 00:55:50 +08:00
|
|
|
context['foo'] = 'bar'
|
|
|
|
return context
|
2013-02-24 22:00:34 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* 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
|
|
|
|
:class:`~django.contrib.syndication.views.Feed` class. In a similar way to
|
|
|
|
the title and description, it is passed it a single parameter,
|
|
|
|
``item``. If that method doesn't exist, Django tries executing a
|
|
|
|
``get_absolute_url()`` method on that object. Both
|
|
|
|
``get_absolute_url()`` and ``item_link()`` should return the
|
|
|
|
item's URL as a normal Python string. As with ``get_absolute_url()``, the
|
|
|
|
result of ``item_link()`` will be included directly in the URL, so you
|
|
|
|
are responsible for doing all necessary URL quoting and conversion to
|
|
|
|
ASCII inside the method itself.
|
2006-11-07 09:54:34 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
A complex example
|
|
|
|
-----------------
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
The framework also supports more complex feeds, via arguments.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2013-08-05 02:45:29 +08:00
|
|
|
For example, a website could offer an RSS feed of recent crimes for every
|
|
|
|
police beat in a city. It'd be silly to create a separate
|
2010-01-28 21:46:18 +08:00
|
|
|
:class:`~django.contrib.syndication.views.Feed` class for each police beat; that
|
2008-08-24 06:25:40 +08:00
|
|
|
would violate the :ref:`DRY principle <dry>` and would couple data to
|
2010-01-28 21:46:18 +08:00
|
|
|
programming logic. Instead, the syndication framework lets you access the
|
2010-08-20 03:27:44 +08:00
|
|
|
arguments passed from your :doc:`URLconf </topics/http/urls>` so feeds can output
|
2010-01-28 21:46:18 +08:00
|
|
|
items based on information in the feed's URL.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2013-08-05 02:45:29 +08:00
|
|
|
The police beat feeds could be accessible via URLs like this:
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* :file:`/beats/613/rss/` -- Returns recent crimes for beat 613.
|
|
|
|
* :file:`/beats/1424/rss/` -- Returns recent crimes for beat 1424.
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
These can be matched with a :doc:`URLconf </topics/http/urls>` line such as::
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
path('beats/<int:beat_id>/rss/', BeatFeed()),
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
Like a view, the arguments in the URL are passed to the ``get_object()``
|
2010-01-28 21:46:18 +08:00
|
|
|
method along with the request object.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
Here's the code for these beat-specific feeds::
|
|
|
|
|
2015-06-09 23:04:32 +08:00
|
|
|
from django.contrib.syndication.views import Feed
|
2007-11-30 01:41:35 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
class BeatFeed(Feed):
|
2010-01-28 21:46:18 +08:00
|
|
|
description_template = 'feeds/beat_description.html'
|
|
|
|
|
|
|
|
def get_object(self, request, beat_id):
|
2015-09-15 03:29:10 +08:00
|
|
|
return Beat.objects.get(pk=beat_id)
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def title(self, obj):
|
2013-08-05 02:45:29 +08:00
|
|
|
return "Police beat central: Crimes for beat %s" % obj.beat
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def link(self, obj):
|
|
|
|
return obj.get_absolute_url()
|
|
|
|
|
|
|
|
def description(self, obj):
|
|
|
|
return "Crimes recently reported in police beat %s" % obj.beat
|
|
|
|
|
|
|
|
def items(self, obj):
|
2010-01-28 21:46:18 +08:00
|
|
|
return Crime.objects.filter(beat=obj).order_by('-crime_date')[:30]
|
|
|
|
|
|
|
|
To generate the feed's ``<title>``, ``<link>`` and ``<description>``, Django
|
2011-03-04 06:20:54 +08:00
|
|
|
uses the ``title()``, ``link()`` and ``description()`` methods. In
|
2019-06-17 22:54:55 +08:00
|
|
|
the previous example, they were string class attributes, but this example
|
2010-01-28 21:46:18 +08:00
|
|
|
illustrates that they can be either strings *or* methods. For each of
|
2011-03-04 06:20:54 +08:00
|
|
|
``title``, ``link`` and ``description``, Django follows this
|
2010-01-28 21:46:18 +08:00
|
|
|
algorithm:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* First, it tries to call a method, passing the ``obj`` argument, where
|
|
|
|
``obj`` is the object returned by ``get_object()``.
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Failing that, it tries to call a method with no arguments.
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Failing that, it uses the class attribute.
|
2010-01-28 21:46:18 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
Also note that ``items()`` also follows the same algorithm -- first, it
|
|
|
|
tries ``items(obj)``, then ``items()``, then finally an ``items``
|
2010-01-28 21:46:18 +08:00
|
|
|
class attribute (which should be a list).
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
We are using a template for the item descriptions. It can be as minimal as
|
|
|
|
this:
|
2010-01-28 21:46:18 +08:00
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{{ obj.description }}
|
|
|
|
|
|
|
|
However, you are free to add formatting as desired.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
The ``ExampleFeed`` class below gives full documentation on methods and
|
2010-01-28 21:46:18 +08:00
|
|
|
attributes of :class:`~django.contrib.syndication.views.Feed` classes.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
Specifying the type of feed
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
By default, feeds produced in this framework use RSS 2.0.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
To change that, add a ``feed_type`` attribute to your
|
2010-01-28 21:46:18 +08:00
|
|
|
:class:`~django.contrib.syndication.views.Feed` class, like so::
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
from django.utils.feedgenerator import Atom1Feed
|
|
|
|
|
|
|
|
class MyFeed(Feed):
|
|
|
|
feed_type = Atom1Feed
|
|
|
|
|
|
|
|
Note that you set ``feed_type`` to a class object, not an instance.
|
|
|
|
|
2005-11-12 12:00:35 +08:00
|
|
|
Currently available feed types are:
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* :class:`django.utils.feedgenerator.Rss201rev2Feed` (RSS 2.01. Default.)
|
|
|
|
* :class:`django.utils.feedgenerator.RssUserland091Feed` (RSS 0.91.)
|
|
|
|
* :class:`django.utils.feedgenerator.Atom1Feed` (Atom 1.0.)
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
Enclosures
|
|
|
|
----------
|
|
|
|
|
|
|
|
To specify enclosures, such as those used in creating podcast feeds, use the
|
2015-08-21 17:50:43 +08:00
|
|
|
``item_enclosures`` hook or, alternatively and if you only have a single
|
|
|
|
enclosure per item, the ``item_enclosure_url``, ``item_enclosure_length``, and
|
2011-03-04 06:20:54 +08:00
|
|
|
``item_enclosure_mime_type`` hooks. See the ``ExampleFeed`` class below for
|
2005-11-12 11:44:53 +08:00
|
|
|
usage examples.
|
|
|
|
|
|
|
|
Language
|
|
|
|
--------
|
|
|
|
|
|
|
|
Feeds created by the syndication framework automatically include the
|
2019-04-11 05:48:58 +08:00
|
|
|
appropriate ``<language>`` tag (RSS 2.0) or ``xml:lang`` attribute (Atom). By
|
|
|
|
default, this is :func:`django.utils.translation.get_language()`. You can change it
|
|
|
|
by setting the ``language`` class attribute.
|
|
|
|
|
2005-11-14 12:59:20 +08:00
|
|
|
URLs
|
|
|
|
----
|
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
The ``link`` method/attribute can return either an absolute path (e.g.
|
2008-08-24 06:25:40 +08:00
|
|
|
:file:`"/blog/"`) or a URL with the fully-qualified domain and protocol (e.g.
|
2015-11-30 00:29:46 +08:00
|
|
|
``"https://www.example.com/blog/"``). If ``link`` doesn't return the domain,
|
2005-11-14 12:59:20 +08:00
|
|
|
the syndication framework will insert the domain of the current site, according
|
2008-08-24 06:25:40 +08:00
|
|
|
to your :setting:`SITE_ID setting <SITE_ID>`.
|
2005-11-14 12:59:20 +08:00
|
|
|
|
|
|
|
Atom feeds require a ``<link rel="self">`` that defines the feed's current
|
|
|
|
location. The syndication framework populates this automatically, using the
|
2008-08-24 06:25:40 +08:00
|
|
|
domain of the current site according to the :setting:`SITE_ID` setting.
|
2005-11-14 12:59:20 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
Publishing Atom and RSS feeds in tandem
|
|
|
|
---------------------------------------
|
|
|
|
|
|
|
|
Some developers like to make available both Atom *and* RSS versions of their
|
2019-06-17 22:54:55 +08:00
|
|
|
feeds. To do that, you can create a subclass of your
|
|
|
|
:class:`~django.contrib.syndication.views.Feed` class and set the ``feed_type``
|
|
|
|
to something different. Then update your URLconf to add the extra versions.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
Here's a full example::
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
from django.contrib.syndication.views import Feed
|
2013-08-05 02:45:29 +08:00
|
|
|
from policebeat.models import NewsItem
|
2005-11-12 11:44:53 +08:00
|
|
|
from django.utils.feedgenerator import Atom1Feed
|
|
|
|
|
|
|
|
class RssSiteNewsFeed(Feed):
|
2013-08-05 02:45:29 +08:00
|
|
|
title = "Police beat site news"
|
2005-11-12 11:44:53 +08:00
|
|
|
link = "/sitenews/"
|
2013-08-05 02:45:29 +08:00
|
|
|
description = "Updates on changes and additions to police beat central."
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def items(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return NewsItem.objects.order_by('-pub_date')[:5]
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
class AtomSiteNewsFeed(RssSiteNewsFeed):
|
|
|
|
feed_type = Atom1Feed
|
2007-04-09 23:48:08 +08:00
|
|
|
subtitle = RssSiteNewsFeed.description
|
2007-04-09 20:35:05 +08:00
|
|
|
|
|
|
|
.. Note::
|
2011-03-04 06:20:54 +08:00
|
|
|
In this example, the RSS feed uses a ``description`` while the Atom
|
|
|
|
feed uses a ``subtitle``. That's because Atom feeds don't provide for
|
2008-08-24 06:25:40 +08:00
|
|
|
a feed-level "description," but they *do* provide for a "subtitle."
|
2007-04-09 23:48:08 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
If you provide a ``description`` in your
|
2010-01-28 21:46:18 +08:00
|
|
|
:class:`~django.contrib.syndication.views.Feed` class, Django will *not*
|
2011-03-04 06:20:54 +08:00
|
|
|
automatically put that into the ``subtitle`` element, because a
|
2008-08-24 06:25:40 +08:00
|
|
|
subtitle and description are not necessarily the same thing. Instead, you
|
2011-03-04 06:20:54 +08:00
|
|
|
should define a ``subtitle`` attribute.
|
2007-04-09 23:48:08 +08:00
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
In the above example, we set the Atom feed's ``subtitle`` to the RSS feed's
|
|
|
|
``description``, because it's quite short already.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
And the accompanying URLconf::
|
|
|
|
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls import path
|
2018-05-13 01:37:42 +08:00
|
|
|
from myproject.feeds import AtomSiteNewsFeed, RssSiteNewsFeed
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2014-04-02 08:46:34 +08:00
|
|
|
urlpatterns = [
|
2005-11-12 11:44:53 +08:00
|
|
|
# ...
|
2016-10-21 01:29:04 +08:00
|
|
|
path('sitenews/rss/', RssSiteNewsFeed()),
|
|
|
|
path('sitenews/atom/', AtomSiteNewsFeed()),
|
2005-11-12 11:44:53 +08:00
|
|
|
# ...
|
2014-04-02 08:46:34 +08:00
|
|
|
]
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``Feed`` class reference
|
|
|
|
------------------------
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
.. class:: views.Feed
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
This example illustrates all possible attributes and methods for a
|
2010-01-28 21:46:18 +08:00
|
|
|
:class:`~django.contrib.syndication.views.Feed` class::
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
from django.contrib.syndication.views import Feed
|
2005-11-20 12:53:28 +08:00
|
|
|
from django.utils import feedgenerator
|
|
|
|
|
|
|
|
class ExampleFeed(Feed):
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
# FEED TYPE -- Optional. This should be a class that subclasses
|
2010-01-28 21:46:18 +08:00
|
|
|
# django.utils.feedgenerator.SyndicationFeed. This designates
|
|
|
|
# which type of feed this should be: RSS 2.0, Atom 1.0, etc. If
|
|
|
|
# you don't specify feed_type, your feed will be RSS 2.0. This
|
|
|
|
# should be a class, not an instance of the class.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
feed_type = feedgenerator.Rss201rev2Feed
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
# TEMPLATE NAMES -- Optional. These should be strings
|
|
|
|
# representing names of Django templates that the system should
|
|
|
|
# use in rendering the title and description of your feed items.
|
|
|
|
# Both are optional. If a template is not specified, the
|
|
|
|
# item_title() or item_description() methods are used instead.
|
2006-06-27 07:19:03 +08:00
|
|
|
|
|
|
|
title_template = None
|
|
|
|
description_template = None
|
|
|
|
|
2019-04-11 05:48:58 +08:00
|
|
|
# LANGUAGE -- Optional. This should be a string specifying a language
|
|
|
|
# code. Defaults to django.utils.translation.get_language().
|
|
|
|
language = 'de'
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
# TITLE -- One of the following three is required. The framework
|
|
|
|
# looks for them in this order.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def title(self, obj):
|
|
|
|
"""
|
2010-01-28 21:46:18 +08:00
|
|
|
Takes the object returned by get_object() and returns the
|
|
|
|
feed's title as a normal Python string.
|
2005-11-12 11:44:53 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def title(self):
|
|
|
|
"""
|
|
|
|
Returns the feed's title as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
title = 'foo' # Hard-coded title.
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
# LINK -- One of the following three is required. The framework
|
|
|
|
# looks for them in this order.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def link(self, obj):
|
|
|
|
"""
|
2012-02-12 23:18:24 +08:00
|
|
|
# Takes the object returned by get_object() and returns the URL
|
|
|
|
# of the HTML version of the feed as a normal Python string.
|
2005-11-12 11:44:53 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def link(self):
|
|
|
|
"""
|
2012-02-12 23:18:24 +08:00
|
|
|
Returns the URL of the HTML version of the feed as a normal Python
|
|
|
|
string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
link = '/blog/' # Hard-coded URL.
|
|
|
|
|
|
|
|
# FEED_URL -- One of the following three is optional. The framework
|
|
|
|
# looks for them in this order.
|
|
|
|
|
|
|
|
def feed_url(self, obj):
|
|
|
|
"""
|
|
|
|
# Takes the object returned by get_object() and returns the feed's
|
|
|
|
# own URL as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def feed_url(self):
|
|
|
|
"""
|
|
|
|
Returns the feed's own URL as a normal Python string.
|
2005-11-12 11:44:53 +08:00
|
|
|
"""
|
|
|
|
|
2012-02-12 23:18:24 +08:00
|
|
|
feed_url = '/blog/rss/' # Hard-coded URL.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2007-07-10 20:33:55 +08:00
|
|
|
# GUID -- One of the following three is optional. The framework looks
|
|
|
|
# for them in this order. This property is only used for Atom feeds
|
2007-07-12 12:43:29 +08:00
|
|
|
# (where it is the feed-level ID element). If not provided, the feed
|
|
|
|
# link is used as the ID.
|
2007-07-10 20:33:55 +08:00
|
|
|
|
|
|
|
def feed_guid(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the globally
|
2007-07-12 12:43:29 +08:00
|
|
|
unique ID for the feed as a normal Python string.
|
2007-07-10 20:33:55 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def feed_guid(self):
|
|
|
|
"""
|
2007-07-12 12:43:29 +08:00
|
|
|
Returns the feed's globally unique ID as a normal Python string.
|
2007-07-10 20:33:55 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
feed_guid = '/foo/bar/1234' # Hard-coded guid.
|
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
# DESCRIPTION -- One of the following three is required. The framework
|
|
|
|
# looks for them in this order.
|
|
|
|
|
|
|
|
def description(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the feed's
|
|
|
|
description as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def description(self):
|
|
|
|
"""
|
|
|
|
Returns the feed's description as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
description = 'Foo bar baz.' # Hard-coded description.
|
|
|
|
|
2005-11-14 13:15:40 +08:00
|
|
|
# AUTHOR NAME --One of the following three is optional. The framework
|
|
|
|
# looks for them in this order.
|
|
|
|
|
|
|
|
def author_name(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the feed's
|
|
|
|
author's name as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def author_name(self):
|
|
|
|
"""
|
|
|
|
Returns the feed's author's name as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
author_name = 'Sally Smith' # Hard-coded author name.
|
|
|
|
|
2012-09-26 20:14:51 +08:00
|
|
|
# AUTHOR EMAIL --One of the following three is optional. The framework
|
2005-11-14 13:15:40 +08:00
|
|
|
# looks for them in this order.
|
|
|
|
|
|
|
|
def author_email(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the feed's
|
2011-04-02 00:10:22 +08:00
|
|
|
author's email as a normal Python string.
|
2005-11-14 13:15:40 +08:00
|
|
|
"""
|
|
|
|
|
2006-07-26 08:15:01 +08:00
|
|
|
def author_email(self):
|
2005-11-14 13:15:40 +08:00
|
|
|
"""
|
2011-04-02 00:10:22 +08:00
|
|
|
Returns the feed's author's email as a normal Python string.
|
2005-11-14 13:15:40 +08:00
|
|
|
"""
|
|
|
|
|
2011-04-02 00:10:22 +08:00
|
|
|
author_email = 'test@example.com' # Hard-coded author email.
|
2005-11-14 13:15:40 +08:00
|
|
|
|
|
|
|
# AUTHOR LINK --One of the following three is optional. The framework
|
|
|
|
# looks for them in this order. In each case, the URL should include
|
|
|
|
# the "http://" and domain name.
|
|
|
|
|
|
|
|
def author_link(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the feed's
|
|
|
|
author's URL as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def author_link(self):
|
|
|
|
"""
|
|
|
|
Returns the feed's author's URL as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
2015-11-30 00:29:46 +08:00
|
|
|
author_link = 'https://www.example.com/' # Hard-coded author URL.
|
2005-11-14 13:15:40 +08:00
|
|
|
|
2006-06-19 09:38:06 +08:00
|
|
|
# CATEGORIES -- One of the following three is optional. The framework
|
|
|
|
# looks for them in this order. In each case, the method/attribute
|
|
|
|
# should return an iterable object that returns strings.
|
|
|
|
|
|
|
|
def categories(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the feed's
|
|
|
|
categories as iterable over strings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def categories(self):
|
|
|
|
"""
|
|
|
|
Returns the feed's categories as iterable over strings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
categories = ("python", "django") # Hard-coded list of categories.
|
|
|
|
|
2007-02-10 16:36:39 +08:00
|
|
|
# COPYRIGHT NOTICE -- One of the following three is optional. The
|
|
|
|
# framework looks for them in this order.
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def feed_copyright(self, obj):
|
2007-02-10 16:36:39 +08:00
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the feed's
|
|
|
|
copyright notice as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def feed_copyright(self):
|
2007-02-10 16:36:39 +08:00
|
|
|
"""
|
|
|
|
Returns the feed's copyright notice as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
feed_copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
|
2007-02-10 16:36:39 +08:00
|
|
|
|
2007-10-20 22:54:38 +08:00
|
|
|
# TTL -- One of the following three is optional. The framework looks
|
|
|
|
# for them in this order. Ignored for Atom feeds.
|
|
|
|
|
|
|
|
def ttl(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns the feed's
|
2007-11-30 13:27:49 +08:00
|
|
|
TTL (Time To Live) as a normal Python string.
|
2007-10-20 22:54:38 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def ttl(self):
|
|
|
|
"""
|
2007-11-30 13:27:49 +08:00
|
|
|
Returns the feed's TTL as a normal Python string.
|
2007-10-20 22:54:38 +08:00
|
|
|
"""
|
|
|
|
|
2007-11-30 13:27:49 +08:00
|
|
|
ttl = 600 # Hard-coded Time To Live.
|
2007-10-20 22:54:38 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
# ITEMS -- One of the following three is required. The framework looks
|
|
|
|
# for them in this order.
|
|
|
|
|
|
|
|
def items(self, obj):
|
|
|
|
"""
|
|
|
|
Takes the object returned by get_object() and returns a list of
|
|
|
|
items to publish in this feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
"""
|
|
|
|
Returns a list of items to publish in this feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
items = ('Item 1', 'Item 2') # Hard-coded items.
|
|
|
|
|
|
|
|
# GET_OBJECT -- This is required for feeds that publish different data
|
|
|
|
# for different URL parameters. (See "A complex example" above.)
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
def get_object(self, request, *args, **kwargs):
|
2005-11-12 11:44:53 +08:00
|
|
|
"""
|
2010-01-28 21:46:18 +08:00
|
|
|
Takes the current request and the arguments from the URL, and
|
|
|
|
returns an object represented by this feed. Raises
|
2005-11-12 11:44:53 +08:00
|
|
|
django.core.exceptions.ObjectDoesNotExist on error.
|
|
|
|
"""
|
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
# ITEM TITLE AND DESCRIPTION -- If title_template or
|
|
|
|
# description_template are not defined, these are used instead. Both are
|
2017-01-21 05:04:05 +08:00
|
|
|
# optional, by default they will use the string representation of the
|
2010-01-28 21:46:18 +08:00
|
|
|
# item.
|
|
|
|
|
|
|
|
def item_title(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
title as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_title(self):
|
|
|
|
"""
|
|
|
|
Returns the title for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_title = 'Breaking News: Nothing Happening' # Hard-coded title.
|
|
|
|
|
|
|
|
def item_description(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
description as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_description(self):
|
|
|
|
"""
|
|
|
|
Returns the description for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_description = 'A description of the item.' # Hard-coded description.
|
|
|
|
|
2013-02-24 22:00:34 +08:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
# ITEM LINK -- One of these three is required. The framework looks for
|
|
|
|
# them in this order.
|
|
|
|
|
2007-12-02 04:08:45 +08:00
|
|
|
# First, the framework tries the two methods below, in
|
|
|
|
# order. Failing that, it falls back to the get_absolute_url()
|
|
|
|
# method on each item returned by items().
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def item_link(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's URL.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_link(self):
|
|
|
|
"""
|
|
|
|
Returns the URL for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
# ITEM_GUID -- The following method is optional. If not provided, the
|
|
|
|
# item's link is used by default.
|
2007-07-10 20:33:55 +08:00
|
|
|
|
|
|
|
def item_guid(self, obj):
|
|
|
|
"""
|
2007-07-12 12:43:29 +08:00
|
|
|
Takes an item, as return by items(), and returns the item's ID.
|
2007-07-10 20:33:55 +08:00
|
|
|
"""
|
|
|
|
|
2013-02-06 18:25:35 +08:00
|
|
|
# ITEM_GUID_IS_PERMALINK -- The following method is optional. If
|
|
|
|
# provided, it sets the 'isPermaLink' attribute of an item's
|
|
|
|
# GUID element. This method is used only when 'item_guid' is
|
|
|
|
# specified.
|
|
|
|
|
|
|
|
def item_guid_is_permalink(self, obj):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns a boolean.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_guid_is_permalink = False # Hard coded value
|
|
|
|
|
2007-07-12 12:43:29 +08:00
|
|
|
# ITEM AUTHOR NAME -- One of the following three is optional. The
|
2005-11-14 13:15:40 +08:00
|
|
|
# framework looks for them in this order.
|
|
|
|
|
|
|
|
def item_author_name(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
author's name as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_author_name(self):
|
|
|
|
"""
|
|
|
|
Returns the author name for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_author_name = 'Sally Smith' # Hard-coded author name.
|
|
|
|
|
2012-09-26 20:14:51 +08:00
|
|
|
# ITEM AUTHOR EMAIL --One of the following three is optional. The
|
2005-11-14 13:15:40 +08:00
|
|
|
# framework looks for them in this order.
|
|
|
|
#
|
|
|
|
# If you specify this, you must specify item_author_name.
|
|
|
|
|
|
|
|
def item_author_email(self, obj):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
2011-04-02 00:10:22 +08:00
|
|
|
author's email as a normal Python string.
|
2005-11-14 13:15:40 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def item_author_email(self):
|
|
|
|
"""
|
2011-04-02 00:10:22 +08:00
|
|
|
Returns the author email for every item in the feed.
|
2005-11-14 13:15:40 +08:00
|
|
|
"""
|
|
|
|
|
2011-04-02 00:10:22 +08:00
|
|
|
item_author_email = 'test@example.com' # Hard-coded author email.
|
2005-11-14 13:15:40 +08:00
|
|
|
|
2010-01-28 21:46:18 +08:00
|
|
|
# ITEM AUTHOR LINK -- One of the following three is optional. The
|
2005-11-14 13:15:40 +08:00
|
|
|
# framework looks for them in this order. In each case, the URL should
|
|
|
|
# include the "http://" and domain name.
|
|
|
|
#
|
|
|
|
# If you specify this, you must specify item_author_name.
|
|
|
|
|
|
|
|
def item_author_link(self, obj):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
author's URL as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_author_link(self):
|
|
|
|
"""
|
|
|
|
Returns the author URL for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
2015-11-30 00:29:46 +08:00
|
|
|
item_author_link = 'https://www.example.com/' # Hard-coded author URL.
|
2005-11-14 13:15:40 +08:00
|
|
|
|
2015-08-21 17:50:43 +08:00
|
|
|
# ITEM ENCLOSURES -- One of the following three is optional. The
|
|
|
|
# framework looks for them in this order. If one of them is defined,
|
|
|
|
# ``item_enclosure_url``, ``item_enclosure_length``, and
|
|
|
|
# ``item_enclosure_mime_type`` will have no effect.
|
|
|
|
|
|
|
|
def item_enclosures(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns a list of
|
|
|
|
``django.utils.feedgenerator.Enclosure`` objects.
|
|
|
|
"""
|
|
|
|
|
2015-12-17 03:49:45 +08:00
|
|
|
def item_enclosures(self):
|
2015-08-21 17:50:43 +08:00
|
|
|
"""
|
|
|
|
Returns the ``django.utils.feedgenerator.Enclosure`` list for every
|
|
|
|
item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_enclosures = [] # Hard-coded enclosure list
|
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
# ITEM ENCLOSURE URL -- One of these three is required if you're
|
2015-08-21 17:50:43 +08:00
|
|
|
# publishing enclosures and you're not using ``item_enclosures``. The
|
|
|
|
# framework looks for them in this order.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def item_enclosure_url(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
enclosure URL.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_enclosure_url(self):
|
|
|
|
"""
|
|
|
|
Returns the enclosure URL for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_enclosure_url = "/foo/bar.mp3" # Hard-coded enclosure link.
|
|
|
|
|
|
|
|
# ITEM ENCLOSURE LENGTH -- One of these three is required if you're
|
2015-08-21 17:50:43 +08:00
|
|
|
# publishing enclosures and you're not using ``item_enclosures``. The
|
|
|
|
# framework looks for them in this order. In each case, the returned
|
|
|
|
# value should be either an integer, or a string representation of the
|
|
|
|
# integer, in bytes.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def item_enclosure_length(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
enclosure length.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_enclosure_length(self):
|
|
|
|
"""
|
|
|
|
Returns the enclosure length for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_enclosure_length = 32000 # Hard-coded enclosure length.
|
|
|
|
|
|
|
|
# ITEM ENCLOSURE MIME TYPE -- One of these three is required if you're
|
2015-08-21 17:50:43 +08:00
|
|
|
# publishing enclosures and you're not using ``item_enclosures``. The
|
|
|
|
# framework looks for them in this order.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
def item_enclosure_mime_type(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
2007-05-03 04:31:15 +08:00
|
|
|
enclosure MIME type.
|
2005-11-12 11:44:53 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def item_enclosure_mime_type(self):
|
|
|
|
"""
|
2007-05-03 04:31:15 +08:00
|
|
|
Returns the enclosure MIME type for every item in the feed.
|
2005-11-12 11:44:53 +08:00
|
|
|
"""
|
|
|
|
|
2007-05-03 04:31:15 +08:00
|
|
|
item_enclosure_mime_type = "audio/mpeg" # Hard-coded enclosure MIME type.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
# ITEM PUBDATE -- It's optional to use one of these three. This is a
|
|
|
|
# hook that specifies how to get the pubdate for a given item.
|
|
|
|
# In each case, the method/attribute should return a Python
|
|
|
|
# datetime.datetime object.
|
|
|
|
|
|
|
|
def item_pubdate(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
pubdate.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_pubdate(self):
|
|
|
|
"""
|
|
|
|
Returns the pubdate for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_pubdate = datetime.datetime(2005, 5, 3) # Hard-coded pubdate.
|
|
|
|
|
2013-07-18 03:20:20 +08:00
|
|
|
# ITEM UPDATED -- It's optional to use one of these three. This is a
|
|
|
|
# hook that specifies how to get the updateddate for a given item.
|
|
|
|
# In each case, the method/attribute should return a Python
|
|
|
|
# datetime.datetime object.
|
|
|
|
|
|
|
|
def item_updateddate(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
updateddate.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_updateddate(self):
|
|
|
|
"""
|
2015-12-03 07:55:50 +08:00
|
|
|
Returns the updateddate for every item in the feed.
|
2013-07-18 03:20:20 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
item_updateddate = datetime.datetime(2005, 5, 3) # Hard-coded updateddate.
|
|
|
|
|
2006-06-19 09:38:06 +08:00
|
|
|
# ITEM CATEGORIES -- It's optional to use one of these three. This is
|
|
|
|
# a hook that specifies how to get the list of categories for a given
|
|
|
|
# item. In each case, the method/attribute should return an iterable
|
|
|
|
# object that returns strings.
|
|
|
|
|
|
|
|
def item_categories(self, item):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
categories.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_categories(self):
|
|
|
|
"""
|
|
|
|
Returns the categories for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_categories = ("python", "django") # Hard-coded categories.
|
|
|
|
|
2007-02-10 16:36:39 +08:00
|
|
|
# ITEM COPYRIGHT NOTICE (only applicable to Atom feeds) -- One of the
|
|
|
|
# following three is optional. The framework looks for them in this
|
|
|
|
# order.
|
|
|
|
|
|
|
|
def item_copyright(self, obj):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
copyright notice as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_copyright(self):
|
|
|
|
"""
|
|
|
|
Returns the copyright notice for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_copyright = 'Copyright (c) 2007, Sally Smith' # Hard-coded copyright notice.
|
|
|
|
|
2020-07-23 22:42:25 +08:00
|
|
|
# ITEM COMMENTS URL -- It's optional to use one of these three. This is
|
|
|
|
# a hook that specifies how to get the URL of a page for comments for a
|
|
|
|
# given item.
|
|
|
|
|
|
|
|
def item_comments(self, obj):
|
|
|
|
"""
|
|
|
|
Takes an item, as returned by items(), and returns the item's
|
|
|
|
comments URL as a normal Python string.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def item_comments(self):
|
|
|
|
"""
|
|
|
|
Returns the comments URL for every item in the feed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
item_comments = 'https://www.example.com/comments' # Hard-coded comments URL
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2
|
|
|
|
|
|
|
|
Support for a comments URL per feed item was added through the
|
|
|
|
``item_comments`` hook.
|
2006-06-19 09:38:06 +08:00
|
|
|
|
2005-11-12 11:44:53 +08:00
|
|
|
The low-level framework
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Behind the scenes, the high-level RSS framework uses a lower-level framework
|
|
|
|
for generating feeds' XML. This framework lives in a single module:
|
2019-03-29 08:32:17 +08:00
|
|
|
:source:`django/utils/feedgenerator.py`.
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
You use this framework on your own, for lower-level feed generation. You can
|
|
|
|
also create custom feed generator subclasses for use with the ``feed_type``
|
|
|
|
``Feed`` option.
|
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
.. currentmodule:: django.utils.feedgenerator
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
``SyndicationFeed`` classes
|
|
|
|
---------------------------
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
The :mod:`~django.utils.feedgenerator` module contains a base class:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* :class:`django.utils.feedgenerator.SyndicationFeed`
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
and several subclasses:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* :class:`django.utils.feedgenerator.RssUserland091Feed`
|
|
|
|
* :class:`django.utils.feedgenerator.Rss201rev2Feed`
|
|
|
|
* :class:`django.utils.feedgenerator.Atom1Feed`
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
Each of these three classes knows how to render a certain type of feed as XML.
|
2005-11-12 11:48:53 +08:00
|
|
|
They share this interface:
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
:meth:`.SyndicationFeed.__init__`
|
2008-08-12 06:22:26 +08:00
|
|
|
Initialize the feed with the given dictionary of metadata, which applies to
|
|
|
|
the entire feed. Required keyword arguments are:
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``title``
|
|
|
|
* ``link``
|
|
|
|
* ``description``
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
There's also a bunch of other optional keywords:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``language``
|
|
|
|
* ``author_email``
|
|
|
|
* ``author_name``
|
|
|
|
* ``author_link``
|
|
|
|
* ``subtitle``
|
|
|
|
* ``categories``
|
|
|
|
* ``feed_url``
|
|
|
|
* ``feed_copyright``
|
|
|
|
* ``feed_guid``
|
|
|
|
* ``ttl``
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
Any extra keyword arguments you pass to ``__init__`` will be stored in
|
|
|
|
``self.feed`` for use with `custom feed generators`_.
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
All parameters should be strings, except ``categories``, which should be a
|
|
|
|
sequence of strings. Beware that some control characters
|
2018-01-07 21:28:41 +08:00
|
|
|
are `not allowed <https://www.w3.org/International/questions/qa-controls>`_
|
2015-06-22 02:53:01 +08:00
|
|
|
in XML documents. If your content has some of them, you might encounter a
|
|
|
|
:exc:`ValueError` when producing the feed.
|
2008-08-12 06:22:26 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
:meth:`.SyndicationFeed.add_item`
|
2009-02-22 14:07:21 +08:00
|
|
|
Add an item to the feed with the given parameters.
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
Required keyword arguments are:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``title``
|
|
|
|
* ``link``
|
|
|
|
* ``description``
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
Optional keyword arguments are:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``author_email``
|
|
|
|
* ``author_name``
|
|
|
|
* ``author_link``
|
|
|
|
* ``pubdate``
|
|
|
|
* ``comments``
|
|
|
|
* ``unique_id``
|
2015-08-21 17:50:43 +08:00
|
|
|
* ``enclosures``
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``categories``
|
|
|
|
* ``item_copyright``
|
|
|
|
* ``ttl``
|
2013-07-18 03:20:20 +08:00
|
|
|
* ``updateddate``
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
Extra keyword arguments will be stored for `custom feed generators`_.
|
|
|
|
|
2017-01-21 05:04:05 +08:00
|
|
|
All parameters, if given, should be strings, except:
|
2008-08-12 06:22:26 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* ``pubdate`` should be a Python :class:`~datetime.datetime` object.
|
2013-07-18 03:20:20 +08:00
|
|
|
* ``updateddate`` should be a Python :class:`~datetime.datetime` object.
|
2015-08-21 17:50:43 +08:00
|
|
|
* ``enclosures`` should be a list of
|
|
|
|
:class:`django.utils.feedgenerator.Enclosure` instances.
|
2017-01-21 05:04:05 +08:00
|
|
|
* ``categories`` should be a sequence of strings.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
:meth:`.SyndicationFeed.write`
|
2008-08-24 06:25:40 +08:00
|
|
|
Outputs the feed in the given encoding to outfile, which is a file-like object.
|
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
:meth:`.SyndicationFeed.writeString`
|
2008-08-24 06:25:40 +08:00
|
|
|
Returns the feed as a string in the given encoding.
|
2008-08-12 06:22:26 +08:00
|
|
|
|
|
|
|
For example, to create an Atom 1.0 feed and print it to standard output::
|
2005-11-12 11:44:53 +08:00
|
|
|
|
|
|
|
>>> from django.utils import feedgenerator
|
2010-05-09 15:44:39 +08:00
|
|
|
>>> from datetime import datetime
|
2005-11-12 11:44:53 +08:00
|
|
|
>>> f = feedgenerator.Atom1Feed(
|
2014-03-23 04:30:49 +08:00
|
|
|
... title="My Weblog",
|
2015-11-30 00:29:46 +08:00
|
|
|
... link="https://www.example.com/",
|
2014-03-23 04:30:49 +08:00
|
|
|
... description="In which I write about what I ate today.",
|
|
|
|
... language="en",
|
|
|
|
... author_name="Myself",
|
2015-11-30 00:29:46 +08:00
|
|
|
... feed_url="https://example.com/atom.xml")
|
2014-03-23 04:30:49 +08:00
|
|
|
>>> f.add_item(title="Hot dog today",
|
2015-11-30 00:29:46 +08:00
|
|
|
... link="https://www.example.com/entries/1/",
|
2010-05-09 15:44:39 +08:00
|
|
|
... pubdate=datetime.now(),
|
2014-03-23 04:30:49 +08:00
|
|
|
... description="<p>Today I had a Vienna Beef hot dog. It was pink, plump and perfect.</p>")
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(f.writeString('UTF-8'))
|
2008-10-05 14:18:41 +08:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
2008-08-12 06:22:26 +08:00
|
|
|
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
|
|
|
|
...
|
|
|
|
</feed>
|
2005-11-12 11:44:53 +08:00
|
|
|
|
2011-03-04 06:20:54 +08:00
|
|
|
.. currentmodule:: django.contrib.syndication
|
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
Custom feed generators
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
If you need to produce a custom feed format, you've got a couple of options.
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
If the feed format is totally custom, you'll want to subclass
|
|
|
|
``SyndicationFeed`` and completely replace the ``write()`` and
|
|
|
|
``writeString()`` methods.
|
|
|
|
|
|
|
|
However, if the feed format is a spin-off of RSS or Atom (i.e. GeoRSS_, Apple's
|
|
|
|
`iTunes podcast format`_, etc.), you've got a better choice. These types of
|
|
|
|
feeds typically add extra elements and/or attributes to the underlying format,
|
|
|
|
and there are a set of methods that ``SyndicationFeed`` calls to get these extra
|
|
|
|
attributes. Thus, you can subclass the appropriate feed generator class
|
|
|
|
(``Atom1Feed`` or ``Rss201rev2Feed``) and extend these callbacks. They are:
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
.. _georss: http://georss.org/
|
2020-04-20 14:10:13 +08:00
|
|
|
.. _itunes podcast format: https://help.apple.com/itc/podcasts_connect/#/itcb54353390
|
2008-08-12 06:22:26 +08:00
|
|
|
|
2017-12-29 04:07:29 +08:00
|
|
|
``SyndicationFeed.root_attributes(self)``
|
2008-08-12 06:22:26 +08:00
|
|
|
Return a ``dict`` of attributes to add to the root feed element
|
|
|
|
(``feed``/``channel``).
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
``SyndicationFeed.add_root_elements(self, handler)``
|
|
|
|
Callback to add elements inside the root feed element
|
2011-09-05 05:17:30 +08:00
|
|
|
(``feed``/``channel``). ``handler`` is an
|
|
|
|
:class:`~xml.sax.saxutils.XMLGenerator` from Python's built-in SAX library;
|
|
|
|
you'll call methods on it to add to the XML document in process.
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
``SyndicationFeed.item_attributes(self, item)``
|
|
|
|
Return a ``dict`` of attributes to add to each item (``item``/``entry``)
|
|
|
|
element. The argument, ``item``, is a dictionary of all the data passed to
|
|
|
|
``SyndicationFeed.add_item()``.
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
``SyndicationFeed.add_item_elements(self, handler, item)``
|
|
|
|
Callback to add elements to each item (``item``/``entry``) element.
|
|
|
|
``handler`` and ``item`` are as above.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
If you override any of these methods, be sure to call the superclass methods
|
|
|
|
since they add the required elements for each feed format.
|
|
|
|
|
|
|
|
For example, you might start implementing an iTunes RSS feed generator like so::
|
|
|
|
|
|
|
|
class iTunesFeed(Rss201rev2Feed):
|
2008-11-10 03:01:34 +08:00
|
|
|
def root_attributes(self):
|
2017-01-22 14:57:14 +08:00
|
|
|
attrs = super().root_attributes()
|
2008-08-24 15:45:35 +08:00
|
|
|
attrs['xmlns:itunes'] = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
|
2008-08-12 06:22:26 +08:00
|
|
|
return attrs
|
2009-02-22 14:07:21 +08:00
|
|
|
|
2008-08-12 06:22:26 +08:00
|
|
|
def add_root_elements(self, handler):
|
2017-01-22 14:57:14 +08:00
|
|
|
super().add_root_elements(handler)
|
2008-08-12 06:22:26 +08:00
|
|
|
handler.addQuickElement('itunes:explicit', 'clean')
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
There's a lot more work to be done for a complete custom feed class, but the
|
|
|
|
above example should demonstrate the basic idea.
|