Inspired by Five Web Files That Will Improve Your Website, I decided this morning to implement OpenSearch on the Indieflix Website. (It's not up yet, we're still beta'ing it, and it's along with a massive list of changes that still need testing, so don't go looking for it.) OpenSearch is a way to turn your site's search feature into an RSS feed: you define for (other) search engines how and what you search on your site, and it automagically creates relationships so that your site's search can be included in remote results. Normally, the results would be returned in an XML container for RSS or Atom, but HTML's fine for some applications.

As a trivial example, I'm going to add your website's search engine to Firefox's drop-down of search engines. I'm going to assume that you already have a search engine enabled on Django. Haystack, or Djapian, or Merquery, or something along those lines.

First, you need a file called opensearch.xml. I put this into my template base directory:

<?xml version="1.0" encoding="UTF-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
  <ShortName>Search My Website</ShortName>
  <Description>Search for my brilliance.</Description>
  <Image width="16" height="16" type="image/png">http://mywebsite.com{{ MEDIA_URL }}img/favicon.png</Image>
  <Url type="text/html" template="http://mywebsite.com/search/?q={searchTerms}"/>
</OpenSearchDescription>

Obviously, there are things to modify here: your real short name, description, image and search paths.   You know where those go.

This goes into your base urls.py (if you haven't imported direct_to_template, now is the right time):

url(r'^opensearch.xml$', direct_to_template,
    { 'template': 'opensearch.xml',
      'mimetype': 'application/opensearchdescription+xml' },
    name="opensearch"),

Again, if you changed the names or locations of the template, you have no one but yourself to blame if stuff blows up.   The name opensearch.xml is not particularly important or required, in either the template (obviously) or in the deployed URL.   To make external spiders and browsers know about your website, this goes into your base.html or site_base.html, somewhere in the headers with your metainformation and style sheets and so forth.

<link rel="search" type="application/opensearchdescription+xml"          
    title="Indieflix Search"           
    href="{% url opensearch %}" />

And that's it.

To test this, load up your home page in Firefox, then click on the search bar's drop-down. You should see your "short name" offered as a search plug-in. Select it and type some search terms, and Firefox will know to use your search engine, and the results will be from your site.

Pretty cool.