On Oct 29, 9:04 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Oct 29, 5:32 pm, Marco <marc.schneide...@gmail.com> wrote:
>
>
>
> > Hi all,
>
> > I going a little bit crazy that's why I'm posting here ...
>
> > I have a view :
>
> > def index(request):
> > request.session['hello'] = 'world !!!'
> > print "hello!!!"
> > return render_to_response('index.html')
>
> > in this page index.html I have
> > <h1>value : {{ request.session.hello }}</h1>
>
> > But nothing is displayed!!!
>
> > 1) I am sure the index view is called (I can see the print debug
> > output)
> > 2) In settings.py,
> > 'django.contrib.sessions.middleware.SessionMiddleware' is enabled
>
> > So I really don't see where the pb is...
>
> > Any idea?
>
> > Regards,
> > Marc.
>
> Firstly, just to check that you are simply using this as an example,
> and you don't think that this is the way to pass values to a template.
> Of course, the normal way to pass values to a template is via the
> context dictionary, which you can pass as the second parameter to
> render_to_response.
>
> That out of the way, the reason why you aren't getting anything in the
> template is because it doesn't have a reference to the request object
> by default. See here for how to get Context Processors to pass it
> automatically:http://docs.djangoproject.com/en/dev/ref/templates/api/#id1
> --
> DR.
Ok thanks, I really wanted to use a session variable, I know there are
other way to pass values to a template.
Typically I want to store user data in a session and then use it
directly in the template.
So the solution was :
in my view, add a RequestContext object to the render_to_response
method.
return render_to_response('index.html', context_instance=RequestContext
(request))
in my settings.py, add to the default template context processors,
this one :
'django.core.context_processors.request'
as a result :
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---