Caching Parsed Django Templates

Standard Django rendering parses a template every time it is rendered. Storing the parsed template can be a nice little speed up for your Django site. This code snippet does a great job of doing that for all templates. One drawback to this approach though is that the server needs restarted whenever a template changes. I’ve taken a slightly different approach to this where I create a separate method for caching and rendering templates. I only call this when rendering several templates in a view and the speed up is absolutely necessary. Some places you might consider doing this is when sending out bulk emails or when rendering a long list of objects and each object uses the same template.

First create a file called template.py in your application. Then we create a method that will cache a template when called and return the rendered template.

from django.template import Context, loader

template_cache = {}
def render_cached(template_name, dictionary=None):
    global template_cache

    t = template_cache.get(template_name, None)
    if not t or settings.DEBUG:
        template_cache[template_name] = t = loader.get_template(template_name)

    dictionary = dictionary or {}
    context_instance = Context(dictionary)
    return t.render(context_instance)

This method takes two arguments the template name and a dictionary to use for the template context. It first gets the template out of the cache. It then looks if the template was retrieved or if your debug setting is on. If either of these cases exist, it uses the base django loader to retrieve the parsed template and store it in the cache dictionary. Then we create a context instance for the dictionary and return the rendered code.

To be able to use the method you just import and call it.

from template import render_cached
html = render_cached("sometemplate.html", {})

KwiClick 2.4.1

KwiClick 2.4.1 was released on AMO this week. This is a pretty large update from the last AMO release so make sure to check it out. Here is a list of what is new in this release:

  • New Search Clovers for selection based searching functionality
  • Updated Bing provider
  • Updated compatibility for ff 3.6betas
  • Improve sqlite performance
  • Trim leading and trailing spaces from searches to improve accuracy
  • Updated kwiclick skin
  • Replace getBoxObjectFor with getBoundingClientRect
  • Clover preferences to fine tune functionality
  • Turn on/off clover from statusbar button
  • Updated wikipedia provider increases accuracy of searching and results

Here is the video for the 2.4 release which was submitted to the extend firefox contest. This is a pretty good explanation of the things you can do with KwiClick.

Halloween Prank Gone Too Far

Halloween is not just for treats, it’s about tricks too. Most everybody I know loves a good Halloween prank, there are even sites dedicated to them. Today in my hometown was an example of Halloween pranks gone too far. The Indianola high school is closed today because vandals damaged the radiators on 14 buses last night. You can read more about it here. My Facebook feed has been consumed with the news today. Not only does this disrupt peoples schedules, having to find last minute baby sitting, etc… but the day will have to be made up at the end of the year. That’s not to mention the cost of parts for the buses and the overtime paid to repair them.

I feel sorry for the parents of the kids that did this. If the vandals are caught, what do you think is an appropriate punishment?

Yip Yip Martians tries Gangsta Rap

Another one of those videos that combine my two favorite things. Sesame Street and Gangsta Rap. You all know how much I love mashups, so here is another good one for you.

FAIL: libIDL on Mac OS X 10.6 (Snow Leopard)

Since I upgraded my laptop to Snow Leopard, I’ve been trying to setup my development environment so that I can compile idl files. I need to do this for one of the Firefox extension I work on. I’ve been able to put this off for a while since my interfaces haven’t changed in a while. I’ve recently changed an interface so now I need to get the idl compiling setup. When I’ve set this up in the past, I haven’t had any problems. I install the gecko sdk, install libidl, and I’m ready to roll. This time I can’t make it work so I’m turning to you, the interweb, to help me figure this out. Here is what I’ve go so far.

I install the Gecko 1.9.1 SDK from developer.mozilla.org. I then install libidl using the following commands in MacPorts:

sudo port selfupdate
sudo port sync
sudo port install libidl

Now, when I run my build script I get the error:

     [exec] dyld: Library not loaded: /opt/local/lib/libIDL-2.0.dylib
     [exec]   Referenced from: /Developer/xulrunner-sdk-1.9.1/bin/xpidl
     [exec]   Reason: no suitable image found.  Did find:
     [exec] 	/opt/local/lib/libIDL-2.0.dylib: mach-o, but wrong architecture

Figuring that libidl is compiled as 64 bit and the gecko sdk is 32 bit, I go searching the web. This leads me to a forum post on compiling Camino on Snow Leopard.

Mac OS 10.6 is 64-bit as native, while Camino is far from 64-bit ready. So cross-compiling to 32-bit is required.
First hurdle was to cross-compile and install the required libIDL library, in 32-bit. Luckily MacPorts contain a universal libIDL, so after I got the hint about “versions” I got that going using the command – “sudo port install libIDL +universal”.

Alright, so now I’m trying to install the universal libIDL file and I hit another error.

Command output:       _IDL_inhibit_get in libIDL_2_la-parser.o
      _IDL_queue_new_ident_comment in libIDL_2_la-parser.o
      _IDL_file_set in libIDL_2_la-parser.o
      ___IDL_do_pragma in libIDL_2_la-parser.o
      _IDL_ns_scope_levels_from_here in libIDL_2_la-ns.o
      _IDL_tree_properties_copy in libIDL_2_la-util.o
      _IDL_tree_walk2 in libIDL_2_la-util.o
      _IDL_tree_to_IDL in libIDL_2_la-util.o
      _IDL_tree_get_scope in libIDL_2_la-util.o
      _IDL_tree_remove_inhibits in libIDL_2_la-util.o
      _IDL_tree_property_remove in libIDL_2_la-util.o
      _IDL_tree_property_get in libIDL_2_la-util.o
      _IDL_tree_property_set in libIDL_2_la-util.o
  "_g_log", referenced from:
      _IDL_check_type_cast in libIDL_2_la-util.o
      _IDL_tree_walk_real in libIDL_2_la-util.o
      _IDL_tree_walk_real in libIDL_2_la-util.o
      _IDL_emit_IDL_literal in libIDL_2_la-util.o
      _IDL_tree_get_node_info in libIDL_2_la-util.o
      _IDL_tree_get_scope in libIDL_2_la-util.o
      _IDL_tree_remove_empty_modules in libIDL_2_la-util.o
      _IDL_tree_remove_inhibits in libIDL_2_la-util.o
      _IDL_tree_process_forward_dcls in libIDL_2_la-util.o
      _load_inhibits in libIDL_2_la-util.o
      _IDL_tree_free in libIDL_2_la-util.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [libIDL-2.la] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

Here is where I’m stuck. I have no idea what symbols are missing. Please, leave me a comment if you have any ideas on how to make this work.

Noah Dancing

Rachel’s mom stopped by for a visit the other day and she left us a disc of photos she has taken over the past couple of years. We ran across this gem of Noah playing and dancing at Andrew and Holly’s wedding.




Noah Dancing

Originally uploaded by richwklein

Reservoir Turtles

Reservoir Dogs is one of my favorite movies of all times. withonea has put together a great mashup of the Reservoir Dogs trailer and Teenage Mutant Turtles footage. Check it out below.
Continue Reading »

20 Week Ultrasound

Last week we had Rachel’s twenty week ultrasound. We found out we are having another boy. We got some cool pictures, I thought I would post.

Baby’s Profile

It’s a Boy

His Feet

This time around, we’ve decided to do something kind of fun. We are going to keep the name a suprise :)

The Guild – Do You Wanna Date My Avatar


Great video from “The Guild”

State of Broadband in Rural Iowa

I work full time for AOL, LLC and telecommute to work. Because of this I need to have broadband in my home. We currently live in the smallish town of Indianola, Iowa and we use Mediacom for our broadband access. The current high speed package we have is $50 a month (7MB download, 512KB upload, and no usage cap). I typical average about 300MB up and 2GB down a day. We’ve been in the process of building a new home less than ten miles away, about 5 miles outside of the city limits. I’ve been trying to find out what kind of broadband options are available in the area so I can figure out what carrier we would have for the home office. This has lead me to discover just how abysmal coverage is in rural areas and just how difficult it is to even find out what’s available in my area. Here is what I’ve been able to find out:

  • Mediacom is not available in my area.
  • Qwest dial-up service is the only thing available.
  • Verizon is not available in my area.
  • Verizon mobile broadband is available for $59.99 a month with a maximum 5GB per month.
  • Iowa Telecom needs a phone number to find out if it’s available.
  • Hughes Net costs 7 times as much for a comparable package.
  • Wild Blue offers a package for $79.95 that is 1/5th the speed and has a maximum 17GB per month.


View House Locations in a larger map

An interesting side note: I started working on this post over the weekend. OM Malik has two stories today about the state of broadband in the US.

« Newer Entries - Older Entries »