views:

108

answers:

3

I have gone through the (painful) process of writing a custom template tag for use in Django. It is registered as an inclusion_tag so that it renders a template. However, this tag breaks as soon as I try to change something.

I've tried changing the number of parameters and correspondingly changing the parameters when it's called. It's clear the new tag code isn't being loaded, because an error is thrown stating that there is a mismatch in the number of parameters, and it's evident that it's attempting to call the old function.

The same problem occurs if I try to change the name of the template being rendered and correspondingly change the name of the template on disk. It continues to try to call the old template. I've tried clearing old .pyc files with no luck.

Overall, the system is acting as though it's caching the template tags, likely due to the register command. I have dug through endless threads trying to find out if this is so, but all could find it James Bennett stating here that register doesn't do anything. Please help!

A: 

In case anybody else encounters this: the exact caching mechanism is unclear, but restarting the django dev server fixes the problem.

thebossman
+1  A: 

I have gone through the (painful) process of writing a custom template tag for use in Django

I agree that the process for writing the template tag in django is more elaborate than it needs to be.

But let me point you towards some of the third party apps, that when installed, a template tag is just another python function (or class).

http://github.com/alex/django-templatetag-sugar

http://github.com/codysoyland/django-template-repl

Lakshman Prasad
+1  A: 

Firstly, I can't imagine what's complicated about inclusion tags. Writing a completely custom tag from scratch, yes: that's complicated. But inclusion tags are simply three lines of code and a template - what's complicated about that?

Secondly, all Python code in your Django project is loaded once by the server [*], and remains until it is restarted. The dev server usually detects changes and restarts itself to reload the code, but this doesn't always work. You should get into the habit of checking the console to see that it does restart, and doing it manually if necessary. Note that this has nothing whatsoever to do with caching.

[*] strictly speaking, once per process, but the dev server is single-process anyway.

Daniel Roseman
The process was painful because I encountered the error described here under "magic," which took a long time to debug: http://www.b-list.org/weblog/2007/dec/04/magic-tags/ It turns out that the so-called ImportError was being glossed over because my newly minted templatetags/ directory had an empty __init__.py file. It needs at least one character. Who knew. The tag is indeed three lines, which only added to the frustration. Code that simple shouldn't break!You're absolutely right about the Django dev server. It reloads changes so consistently that I didn't consider it as a failure point.
thebossman