django-links
I spent some time yesterday afternoon working on my personal site. I wanted a way to add “related links” for my projects, without hand-editing individual lists in HTML. That’s boring, after all. Surprisingly, I didn’t find an existing django app that did anything like that, and had to build one myself.
My requirements were pretty simple: My site uses codehosting
to host project home pages, including file release management, and RSS
feeds for updates. I wanted to include related links on the project
description pages, so they could link to discussions of the projects
on my blog (or other sites).
Rejected Solutions
My first attempt at solving the problem was to modify codehosting to manage the links directly. That worked OK, but felt wrong. I knew I was going to want to have links for other purposes on another, as yet undeveloped, part of the site, and having to manage the links separately would be a pain.
I prototyped a version using JavaScript to render an RSS feed from
del.icio.us, but didn’t like the results. I wanted to embed the links
instead of requiring a separate iframe. Next, I briefly considered
building something combining del.icio.us feeds and feedcache
,
which would fetch the feeds when the project page was hit, but in-line
the results as the template was being rendered. It seemed like
(potentially) loading the feed each time anyone hit my projects pages
would hurt site response time too much. Storing the links in the
database seemed like the best way to go.
Fruitless Search
Even though it was a simple problem, I went looking for an existing project that did something similar already. I was surprised that I couldn’t find one. I didn’t spend more than 30-45 minutes searching, but I looked at the list of Django-powered sites, which besides full sites includes links to various django-related packages. I also looked around on a few of the blogs I know cover django programming, and of course did a google search. When I found nothing, I assumed that meant the problem was so simple that no one thought they needed to release their code.
django-links
It is a simple problem, so I built something simple to solve it (and
not being shy about releasing code, am making it available for the
next person who comes along). The django-links
app defines a trivial
model to hold the links, with title, description, and publication
date. It provides no UI or syndication features since I didn’t need
them, yet. All of the data is entered through the admin interface. It
uses django-tagging to
define tags for the links, so it is easy to search for links to be
associated with a particular codehosting project.
The next step was to update the project details page for codehosting to show the links. That turned out to be fairly simple as well, using a little bit of code like this:
{% tagged_objects project_tag in djangolinks.Link as related_links %}
Related Links
{% endifequal %}
{% for link in related_links %}
{{ link.title }} ({{link.release_date}})
{% if link.description %}
{{link.description}}
{% endif %}
{% endfor %}
I also updated codehosting to include a TagsField
, so the details
page can show related projects as well as links. I found instructions
for how to build a list of items tagged with all of a set of tags,
but there didn’t seem to be an easy way to build a list of objects
tagged with at least one of a set of tags using only template
language. I suppose that’s the sort of thing that really should be
done in a view, so I’ll work that out in Python.
What next?
The next thing I want to do is build a small CLI app to read a del.icio.us feed and add the links to the django-links database. That way I can use del.icio.us instead of the django admin UI to manage the links.
And, of course, I should add at least a minimal view to show all of the available links and a tag cloud. django-tagging includes tools to build those sorts of UI features, so it should be fairly straightforward.