开发者

Are Django template tags cached?

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 templat开发者_运维百科e. 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!


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜