Tyler Sebastian
Tyler Sebastian

Reputation: 9448

Disable Template Cache Jinja2

I'm trying to disable Jinja2's template cache. I've done some looking around, and I've found that there's a cache_size parameter for jinja's environment. I'm using the following:

app.jinja_env = jinja2.Environment(
    cache_size = 0, 
    loader = jinja2.FunctionLoader(utils.load_template)
)

I'm using a custom loader to dynamically load templates based on the domain (the app serves multiple domains). Unfortunately, using this, it looks like it overrides Jinja's default filters and builtin functions - using

@app.route(...)
def page():
    render_template('template') # from flask import render_template

I'm getting a UndefinedError: 'url_for' is undefined error. What's the proper way of doing this?

Upvotes: 18

Views: 14868

Answers (2)

Tomer
Tomer

Reputation: 630

If using FastAPI:

templates = Jinja2Templates(directory='templates', auto_reload=True)

Upvotes: 0

ᅠᅠᅠ
ᅠᅠᅠ

Reputation: 66930

You might want to set app.config['TEMPLATES_AUTO_RELOAD'] = True instead. Instead of disabling the cache, it will reload templates if the cached version no longer matches the template file.

Upvotes: 47

Related Questions