librelist archives

« back to archive

Are templates generally cached?

Are templates generally cached?

From:
Marian Steinbach
Date:
2012-04-23 @ 21:11
Hi!

I just joined the Flask crowd today, installed the current
distribution as described by the installation how-to. Most things work
pretty well so far, but now I have a problem.

I'm building a small web-app in two languages. I know there is
Flask-Babel, but for now I am avoiding a gettext-related workflow for
simplicity reasons. Instead I am using filters like these in my
templates:

   <h2>{{ 'index_create_headline'|i18n }}</h2>

the i18n() filter function looks up the translation for
'index_create_headline' in a dict that is loaded on application start.
According to current session['lang'] it returns the appropriate
translation.

Now when I switch between languages via URL parameters, still the
translation displayed previously shows up. I see from non-appearing
debugging log output that the i18n() filter isn't even called. But I
can confirm that the session['lang'] is changed.

I also have an output of {{ session.lang }} in my templates, and this
changes as expected.

So I am starting to guess that this is due to caching. Could that be
the case? If yes, how can I control which parts are cached and which
aren't? Unfortunately I can't find the answers in the docs.

Here is the code of my filter function:


@app.template_filter('i18n')
def i18n_filter(s):
    """
    Output the key in the user's selected language
    """
    app.logger.debug(session['lang'])
    app.logger.debug(s)
    app.logger.debug(i18n[s][session['lang']])
    if s not in i18n:
        return 'Key "' + s + '" is invalid'
    if session['lang'] not in i18n[s]:
        return 'Key "' + s + '" not available in "' + session['lang'] + '"'
    if session['lang'] == '':
        return 'Key "' + s + '" in "' + session['lang'] + '" is empty'
    return i18n[s][session['lang']]


Thanks!

Marian

Re: [flask] Are templates generally cached?

From:
Lix Xu
Date:
2012-04-24 @ 01:32
I also had this problem before, and I still not find the answer.
Hope this can help you.

1. You can use this ugly way:
    <h2>{{ 'index_create_headline'|i18n(session.lang) }}</h2>

2. Or you can add this to view function (not sure this can resolve the
problem):
def no_cache_header(f):
    @wraps(f)
    def _no_cache_header(*args, **kwargs):
        response = make_response(f(*args, **kwargs))
        if request.user_agent.browser == 'msie':
            response.headers['Pragma'] = 'no-cache'
            response.headers['Cache-control'] = 'no-cache'
            response.headers['Expires'] = -1

        return response

    return _no_cache_header

mod = Blueprint('home', __name__)
@mod.route('/')
@no_cache_header
def index():
    .....
    return render_template('home/index.html')

On Tue, Apr 24, 2012 at 05:11, Marian Steinbach <marian@sendung.de> wrote:

> Hi!
>
> I just joined the Flask crowd today, installed the current
> distribution as described by the installation how-to. Most things work
> pretty well so far, but now I have a problem.
>
> I'm building a small web-app in two languages. I know there is
> Flask-Babel, but for now I am avoiding a gettext-related workflow for
> simplicity reasons. Instead I am using filters like these in my
> templates:
>
>   <h2>{{ 'index_create_headline'|i18n }}</h2>
>
> the i18n() filter function looks up the translation for
> 'index_create_headline' in a dict that is loaded on application start.
> According to current session['lang'] it returns the appropriate
> translation.
>
> Now when I switch between languages via URL parameters, still the
> translation displayed previously shows up. I see from non-appearing
> debugging log output that the i18n() filter isn't even called. But I
> can confirm that the session['lang'] is changed.
>
> I also have an output of {{ session.lang }} in my templates, and this
> changes as expected.
>
> So I am starting to guess that this is due to caching. Could that be
> the case? If yes, how can I control which parts are cached and which
> aren't? Unfortunately I can't find the answers in the docs.
>
> Here is the code of my filter function:
>
>
> @app.template_filter('i18n')
> def i18n_filter(s):
>    """
>    Output the key in the user's selected language
>    """
>    app.logger.debug(session['lang'])
>    app.logger.debug(s)
>    app.logger.debug(i18n[s][session['lang']])
>    if s not in i18n:
>        return 'Key "' + s + '" is invalid'
>    if session['lang'] not in i18n[s]:
>        return 'Key "' + s + '" not available in "' + session['lang'] + '"'
>    if session['lang'] == '':
>        return 'Key "' + s + '" in "' + session['lang'] + '" is empty'
>    return i18n[s][session['lang']]
>
>
> Thanks!
>
> Marian
>

Re: [flask] Are templates generally cached?

From:
Marian Steinbach
Date:
2012-04-24 @ 07:27
I guess, after some hours of sleep, the subject of this thread should
rather have read "Are *views* generally cached?". However.

@Lix Xu: Thanks for your suggestions. As for HTTP headers, it's
definitely not  client-side cache problem. The page is delivered with
status 200, not 304.

But passing the language parameter to the filter function makes a difference:

  <h2>{{ 'index_create_headline'|i18n(session.lang) }}</h2>

Now, when I change the URL from / to /?lang=it, the language is
changed. When I change it to /?lang=en, it is changed again. Great!

My debugging output suggests that both outputs are still cached
though. When I change the language to lang=it once more, the filter
function isn't called any more.

I'd still like to know more on the the rules of that behaviour.

Marian


Am 24. April 2012 03:32 schrieb Lix Xu <xuzenglin@gmail.com>:
> I also had this problem before, and I still not find the answer.
> Hope this can help you.
>
> 1. You can use this ugly way:
>     <h2>{{ 'index_create_headline'|i18n(session.lang) }}</h2>
>
> 2. Or you can add this to view function (not sure this can resolve the
> problem):
> def no_cache_header(f):
>     @wraps(f)
>     def _no_cache_header(*args, **kwargs):
>         response = make_response(f(*args, **kwargs))
>         if request.user_agent.browser == 'msie':
>             response.headers['Pragma'] = 'no-cache'
>             response.headers['Cache-control'] = 'no-cache'
>             response.headers['Expires'] = -1
>
>         return response
>
>     return _no_cache_header
>
> mod = Blueprint('home', __name__)
> @mod.route('/')
> @no_cache_header
> def index():
>     .....
>     return render_template('home/index.html')