Roy Tang

Programmer, engineer, scientist, critic, gamer, dreamer, and kid-at-heart.

Blog Notes Photos Links Archives About Now

Using Django Pingback


I actually had some trouble using django-pingback on my custom blog engine; the django-pingback documentation is mostly fine, but there were some caveats that I had to discover myself through a bit of debugging:

  • The URL specified for the XML-RPC endpoint in the HTML head needs to be a full absolute url including domain, i.e. http://roytang.net/xmlrpc/, which gave me trouble when I was trying to test using localhost pinging to an online server. I eventually just decided to set it up, deploy on webfaction and test it online before I redirected the domain name.
  • The documentation mentioned that the pingback handler needs to have the same arguments as the detail view for the post, so I wrote it as follows:

          <td class="code">
            <div class="source">
              <pre><span class="c"># exactly same arguments as 'details' view.</span>
    

    def pingback_blog_handler(catslug, year, month, slug, **kwargs): cat = Category.objects.get(slug=catslug) return Post.pub_objects.get(date__year=year, date__month=month, slug=slug, category=cat, published=True)

    1
    
    
    

    2 3 4

    But it wasn't exactly working for me, for some reason the function wasn't being called. After some debugging I found that I needed to use named parameters for catslug, year, month, slug in the URLConf for the post permalink in order for this to work. I'm not sure if this was because I was doing it wrong or it's some sort of undocumented requirement.</li> 
    
      * The pingback client was throwing a KeyError whenever a post I made would have anchor tags without an href attribute. Granted that there isn't much point in having an <a>anchor tag without the href</a>, but the code shouldn't just choke on it. I modified the following line in client.py: <div class="hl_wrap">
          <table class="sourcetable">
            <tr>
              <td class="linenos">
                <pre>1</pre>
              </td>
              
              <td class="code">
                <div class="source">
                  <pre><span class="n">links</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="s">'href'</span><span class="p">]</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">soup</span><span class="o">.</span><span class="n">findAll</span><span class="p">(</span><span class="s">'a'</span><span class="p">)</span> <span class="k">if</span> <span class="n">is_external</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="s">'href'</span><span class="p">],</span> <span class="n">url</span><span class="p">)]</span>
    

    Adding the check for the href attribute:
    
    <div class="hl_wrap">
      <table class="sourcetable">
        <tr>
          <td class="linenos">
            <pre>1</pre>
          </td>
          
          <td class="code">
            <div class="source">
              <pre><span class="n">links</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="s">'href'</span><span class="p">]</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">soup</span><span class="o">.</span><span class="n">findAll</span><span class="p">(</span><span class="s">'a'</span><span class="p">)</span> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="s">'href'</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_external</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="s">'href'</span><span class="p">],</span> <span class="n">url</span><span class="p">)]</span>

Those are all the changes I made to get pingbacks working. I hope they're still working now, as I haven't received any pingbacks since the django version of this blog went live >.
Posted by under post at / Tags: #royondjango #django #python #software development / 0 / 410 words

See Also

If you enjoyed my content for some reason, I'd love to hear from you! Here are some options:

Roy Tang is a:

roytang.net is a personal site, an E/N site, and kind of a commonplace book; I post about a random assortment of topics that interest me including software development, Magic the Gathering, pop culture, gaming, and tech life. This site is perpetually under renovation.