I have a rather simple informational website that’s being run by Django.
Its’ pages(views) are rendered by a render_to_response shortcut that is used like this:
def index(request):
return render_to_response(**create_render_args('Index.html', request))
def somepage(request):
return render_to_response(**create_render_args('Somepage.html', request, _(u'Some page title'), 'somepage.css'))
# other pages are also rendered in that way
Where create_render_args function is:
def create_render_args(html, request, title = _(u'Default title'), page_css = ''):
return {
'template_name' : html,
'dictionary' :
{
'title' : title,
'page_css' : '' if page_css == '' else '<link rel="stylesheet" href="/css/{}" />'.format(page_css),
# other template variables...
},
'context_instance' : RequestContext(request)
}
Essentialy it’s a simple way I use to avoid extra code, avoid creating extra template blocks and still be able to specify unique CSS and a localized page title for each language the site is translated into (it’s localized into 4). Maybe there are better ways to do this but I must admit that my Django experience is still rather limited on this matter – I would like to hear any objective criticism.
The problem is that when I use ugettext as _, and I navigate / switch languages / reload pages, it seems that sometimes ‘title’ variable that is being used in template gets ‘stuck’ in old language even though the page itself is already translated into the new language.
For example, if the site is in italian, I open the main page, the title is proper.
Then I switch to english (my javascript that handles the language switch reloads the page), the title is still italian even though everything else is in english. I navigate to another page and navigate to main page again, reload the page a few times – title is italian, page is in english. I go to the server console, touch the wsgi file (to reload Django manually) – main page is fully english on the next reload. But then, I switch the language to italian again – the page is in italian, the title is in english.
Sometimes the behavior is even more odd — if I keep reloading the page by pressing F5, I might get its’ title from the previous language 2 times in a row, then the correct title gets out for a single time, and then I get ‘old’ title on the following reloads.
Now what troubles me the most – if I use ugettext_lazy as _, I have no problems at all. The title is always in the same language as the rest of the page.
As far as I know, views are being called on each page view, so something I believe I encounter – create_render_args()‘s output for each given page being cached somewhere and returning the same dictionary all the time – shouldn’t be possible.
What could be the source of this problem? What am I missing?
This is the old Python default arguments gotcha once again.
The default values for the
create_render_argsfunction are evaluated when the function is first imported, which is probably when the server process starts up. Processes in WSGI last for multiple requests, which explains why the value is cached between views, but there are usually multiple processes running at once, which explains why sometimes you see the correct version.I’m not sure why using
ugettext_lazyisn’t the answer for you – that seems to be exactly its use case – but otherwise you could just use the raw text itself as the default and do the_call within the function.