Using kid with django

Ryan posted a comment asking how I'm using kid with django. It's not too hard to integrate it in, I needed just one custom patch to the template_file module in django in order to re-use django's template finding logic, and even then that's just a convenience.

For the blogs I wanted to use kid to generate the various feeds, so I just needed to implement a view which passed entries into a template.

translucentcode/apps/blog/views/blog.py:

import kid

# See http://code.djangoproject.com/ticket/579 for my custom patch
from django.core import template_file
from django.utils.httpwrappers import HttpResponse


def blog_feed(request, blog_name=None, limit=15,
        template="atom.kid", **kwargs):

    latest_entries = entries.get_list(blog__name__exact=blog_name,
        pub_state__exact="published",
        order_by=['-pub_date'], limit=limit)

    filename = template_file.find_template(template)

    blog = blogs.get_object(name__exact = blog_name)
    author = blog.get_owner()

    t = kid.Template(filename,
        context=Context(request),
        entries=latest_entries,
        blog=blog,
        author=author,
    )

    # Can use application/atom+xml instead for atom pages
    return HttpResponse(t.serialize(), mimetype="application/xml")

Then in my urls I needed to map the atom (or rss) urls to the feed generator, along with some parameters.

translucentcode/apps/blog/urls/blog.py:

entries = {
    "app_label": "blog",
    "module_name": "entries",
    "date_field": "pub_date",
}

urlpatterns = patterns('translucentcode.apps.blog.views.blog',
    (r'^(?P<blog_name>\w+)/atom/$', 'blog_feed', entries),
    (r'^(?P<blog_name>\w+)/rss/$', 'blog_feed', dict(entries, template="rss20.kid")),
)

Finally in the template I could write normal kid:

translucentcode/templates/atom.kid:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:py="http://purl.org/kid/ns#">

  <title>${blog.title}</title>

  <link href="${blog.get_absolute_url()}"/>

  <updated>${entries[0].pub_date.isoformat()}</updated>

  <author>
    <name>${author.get_full_name() or author.username}</name>
  </author>

  <id>${blog.get_absolute_url()}</id>

  <entry py:for="entry in entries">
    <title>${entry.title}</title>
    <link href="${entry.get_absolute_url()}"/>
    <id>${entry.get_absolute_url()}</id>
    <updated>${entry.pub_date.isoformat()}</updated>
    <content type="html">${entry.render()}</content>
  </entry>

</feed>

That's all there was to it. Since I like generating XML with kid, I found it preferable to using django's feed generation code, which also appears not to generate atom. Generating xml via a template in a django view seemed a more natural fit too.

Comments

Nico Poppelier

Your custom patch was not accepted by the Django project. Could you rewrite your example using the new template functionality they've added? Thanks!

Nico (n dot poppelier at xs4all dot nl)

Matthias Urlichs

Django has changed a bit since last year (understatement alert ;-)

Do you have a current version of this?

Post a comment

Comment posting temporarily disabled, too much spam.