Session not working as i had hoped

2010-05-26 Thread grimmus
Hi,

When the user enters a site for the first time they should see a flash
version of the logo. All other times they should see a gif image. My
home view looks like:

if request.session.get('has_visited',True):
visited = True
else:
visited = False

t = loader.get_template('static/home.html')
c = RequestContext(request,{
'visited': visited,
})
return HttpResponse(t.render(c))

request.session['has_visited'] = True

So, i set the value of the session after the page has been served.

Then in my template i have

{% if visited %}
visited already, show gif logo
{% else %}
new user, show flash logo
{% endif %}

It seems the Session is always True, even when i clear all browser
info, restart etc.

Could someone tell me what i am doing wrong ?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: Session not working as i had hoped

2010-05-26 Thread Jirka Vejrazka
Hi,

  a simple hint: try to point out a place in your code where
has_visited does exist and is set to False.

  Also, you probably don't want to have any code in your view after an
unconditional "return" statement - that code would never get executed.

  HTH

Jikra

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: Session not working as i had hoped

2010-05-26 Thread Nuno Maltez
http://docs.djangoproject.com/en/dev/topics/http/sessions/#using-sessions-in-views


>    if request.session.get('has_visited',True):

You're passing True as the default value to the get method  - get(key,
default=None); this means that when 'has_visited' isn't set in your
session (1st visit) it still returns True.

Just replace :

   if request.session.get('has_visited',True):
   visited = True
   else:
   visited = False

with

visited = request.session.get('has_visited', False)

hth,
Nuno

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: Session not working as i had hoped

2010-05-26 Thread daniels
Also move :
  request.session['has_visited'] = True
above the return statement.

On May 26, 4:02 pm, Nuno Maltez  wrote:
> http://docs.djangoproject.com/en/dev/topics/http/sessions/#using-sess...
>
> >    if request.session.get('has_visited',True):
>
> You're passing True as the default value to the get method  - get(key,
> default=None); this means that when 'has_visited' isn't set in your
> session (1st visit) it still returns True.
>
> Just replace :
>
>    if request.session.get('has_visited',True):
>        visited = True
>    else:
>        visited = False
>
> with
>
> visited = request.session.get('has_visited', False)
>
> hth,
> Nuno

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Re: Session not working as i had hoped

2010-05-26 Thread Masklinn
On 2010-05-26, at 15:02 , Nuno Maltez wrote:
> 
> http://docs.djangoproject.com/en/dev/topics/http/sessions/#using-sessions-in-views
> 
> 
>>if request.session.get('has_visited',True):
> 
> You're passing True as the default value to the get method  - get(key,
> default=None); this means that when 'has_visited' isn't set in your
> session (1st visit) it still returns True.
> 
> Just replace :
> 
>   if request.session.get('has_visited',True):
>   visited = True
>   else:
>   visited = False
> 
> with
> 
> visited = request.session.get('has_visited', False)
> 
> hth,
> Nuno


The `False` default isn't even mandatory.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.