can't get LANGUAGES to work in the base template

6 views
Skip to first unread message

Baurzhan Ismagulov

unread,
Oct 8, 2006, 9:06:49 AM10/8/06
to django...@googlegroups.com
Hello all,

I'm using django 0.96 r3709. I have base.html, page1.html, and
page2.html templates. The latter two extend the former one. base.html
creates a <select> box with all supported LANGUAGES. This works as
expected in page1, but not in page2. page1 is a verdjn templatepage
(rendered with HttpResponse(t.render())), page2 -- a template that I
render with render_to_response. What is the difference? How can I debug
the problem?

Thanks in advance,
Baurzhan.

Ivan Sagalaev

unread,
Oct 8, 2006, 9:54:27 AM10/8/06
to django...@googlegroups.com
Baurzhan Ismagulov wrote:
> I'm using django 0.96 r3709. I have base.html, page1.html, and
> page2.html templates. The latter two extend the former one. base.html
> creates a <select> box with all supported LANGUAGES. This works as
> expected in page1, but not in page2. page1 is a verdjn templatepage
> (rendered with HttpResponse(t.render())), page2 -- a template that I
> render with render_to_response. What is the difference?

All the extra information (such as {{ user }} or {{ LANGUAGES }}) is
passed to templates by a RequestContext that processes a number of
context processors defined in TEMPLATE_CONTEXT_PROCESSORS in settings.

I suspect that your first template is rendered not just with t.render()
but with t.render(context) and this context _is_ a RequestContext
instance. On the contrary render_to_response doesn't use RequestContext
by default, it uses simple Context instead that doesn't know anything
about requests or context processors. To get it working for your second
template you should tell render_to_response to use ReuqestContext with
your request:

return render_to_response(
'template.html',
{ ... },
context_instance=RequestContext(Request)
)

Baurzhan Ismagulov

unread,
Oct 8, 2006, 10:22:48 AM10/8/06
to django...@googlegroups.com
Hello Ivan,

On Sun, Oct 08, 2006 at 05:54:27PM +0400, Ivan Sagalaev wrote:
> All the extra information (such as {{ user }} or {{ LANGUAGES }}) is
> passed to templates by a RequestContext that processes a number of
> context processors defined in TEMPLATE_CONTEXT_PROCESSORS in settings.
>
> I suspect that your first template is rendered not just with t.render()
> but with t.render(context) and this context _is_ a RequestContext
> instance. On the contrary render_to_response doesn't use RequestContext
> by default, it uses simple Context instead that doesn't know anything
> about requests or context processors. To get it working for your second
> template you should tell render_to_response to use ReuqestContext with
> your request:
>
> return render_to_response(
> 'template.html',
> { ... },
> context_instance=RequestContext(Request)
> )

Thanks much, this worked! I suspect I'll have to read more about
contexts.

With kind regards,
Baurzhan.

Baurzhan Ismagulov

unread,
Oct 8, 2006, 1:17:29 PM10/8/06
to django...@googlegroups.com
On Sun, Oct 08, 2006 at 06:27:38PM +0400, Ivan Sagalaev wrote:
> If I correctly suspect that we speak the same language then I can
> recommend my blog post about it:
> http://softwaremaniacs.org/blog/2006/01/12/context-processors/

This is great, it describes first the use case rather than the
functionality, so I think it is worth to have the link in the archives
(the text is in Russian). The size and the coverage are just right, now
I understand Django docs :) . Thanks again!

With kind regards,
Baurzhan.

Reply all
Reply to author
Forward
0 new messages