|
List Info
Thread: Standard python objects in request.session possible?
|
|
| Standard python objects in
request.session possible? |

|
2007-12-15 19:13:36 |
This model works in the shell. It holds messages and pops
items off as
it iterates:
class MessageBox: # also tried with (models.Model)
def __init__(self, sort='neutral'):
self.sort = sort
self.messages = []
def __iter__(self):
return self
def next(self):
if len(self.messages) == 0:
raise StopIteration
return self.messages.pop()
I have this in my view to add a MessageBox object to the
session and
then add a message to it:
if 'good_message_box' not in request.session:
request.session['good_message_box'] =
MessageBox(sort='good')
request.session['good_message_box'].messages.append('Message
Box:
You have successfully logged in.')
I have this in my template to dispaly the messages:
{% if request.session.good_message_box %}
<ul class="errorlist">
{% for m in request.session.good_message_box %}
<li>{}</li>
{% endfor %}
</ul>
{% endif %}
However, unlike the shell behaviour which lists and deletes
messages,
the template lists and retains the messages resulting in
them
displaying repeatedly on every page (the template code is
part of my
base template). Its as if my iterator is being ignored. I
have tried
file based sessions and db based.
Am I missing something or can you not utilise objects in
request.session?
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Standard python objects in
request.session possible? |

|
2007-12-15 22:01:17 |
Hello again,
It seems I hit the wrong spot. I went reading on __iter__
(which I
admit I didn't know) and even though I'm not sure I
understood it
right, I guess your next() method probably _was_ being
executed and it
was all fine. So my new guess is that templates cannot
modify objects
(as in, save them back)
If that is true (which I'm too new to Django to know for
sure, but
makes sense), it would explain why it works on the shell and
not on
templates. You see the emptied object, but the template
cannot save it
back to the session.
So right now what would make sense is to empty the object at
the start
of every request. Not sure how feasible that is (better yet,
how to
automate it, which is something you will obviosuly
want/need), but I'm
sure the nice fellows from this list or IRC channel could
easily point
it out (or at least tell you if it is not possible) or even
provide a
better solution/explanation )
I'll post back if I think of something. Right now, I share
you dead end :-((
-- IdNotFound
On Dec 16, 2007 1:19 AM, itpaul <clearerspace googlemail.com> wrote:
>
> Hello Ed, thanks for the quick response.
>
> Tried putting m.next but i don't think thats allowed in
templates. It
> doesn't work anyway.
>
> {% if request.session.good_message_box %}
> <ul class="errorlist">
> {% for m in request.session.good_message_box
%}
> <li>{{ m.next }}</li>
<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<
<
> {% endfor %}
> </ul>
> {% endif %}
>
>
>
> I can't use the included messaging system because I
need it to:
>
> - work for unauthenticated users
> - differentiate between different sorts of messages
(good, bad,
> error, warning, etc)
>
>
>
>
>
> On Dec 16, 3:33 am, "Eduardo - IdNotFound"
<idnotfo... gmail.com>
> wrote:
> > Sounds like you're not updating/removing items
from the MessageBox
> > object, as I don't see where you are removing
items from the list (I
> > see no calls to MessageBox.next() being made).
> >
> > Alternatively, wouldn't the messaging system in
the Authentication
> > framework fit your needs? Your implementation
highly resembles it...
> >
> > Hope it helps,
> > IdNotFound
> >
>
> > On Dec 15, 2007 11:13 PM, itpaul
<clearersp... googlemail.com> wrote:
> >
> >
> >
> > > This model works in the shell. It holds
messages and pops items off as
> > > it iterates:
> >
> > > class MessageBox: # also tried with
(models.Model)
> >
> > > def __init__(self, sort='neutral'):
> > > self.sort = sort
> > > self.messages = []
> >
> > > def __iter__(self):
> > > return self
> >
> > > def next(self):
> > > if len(self.messages) == 0:
> > > raise StopIteration
> > > return self.messages.pop()
> >
> > > I have this in my view to add a MessageBox
object to the session and
> > > then add a message to it:
> >
> > > if 'good_message_box' not in
request.session:
> > > request.session['good_message_box'] =
MessageBox(sort='good')
> > >
request.session['good_message_box'].messages.append('Message
Box:
> > > You have successfully logged in.')
> >
> > > I have this in my template to dispaly the
messages:
> >
> > > {% if request.session.good_message_box
%}
> > > <ul class="errorlist">
> > > {% for m in
request.session.good_message_box %}
> > > <li>{}</li>
> > > {% endfor %}
> > > </ul>
> > > {% endif %}
> >
> > > However, unlike the shell behaviour which
lists and deletes messages,
> > > the template lists and retains the messages
resulting in them
> > > displaying repeatedly on every page (the
template code is part of my
> > > base template). Its as if my iterator is
being ignored. I have tried
> > > file based sessions and db based.
> >
> > > Am I missing something or can you not utilise
objects in
> > > request.session?
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Standard python objects in
request.session possible? |

|
2007-12-16 01:16:23 |
On Dec 16, 2:13 pm, itpaul <clearersp... googlemail.com> wrote:
> However, unlike the shell behaviour which lists and
deletes messages,
> the template lists and retains the messages resulting
in them
> displaying repeatedly on every page.
Most likely, the session isn't getting saved.
http://www.djangoproject.com/documen
tation/sessions/#when-sessions-are-saved
For the lazy:
request.session.modified = True
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Standard python objects in
request.session possible? |

|
2007-12-16 08:50:45 |
ok. i made a change which includes your modified-true
suggestion and
a possibly relevant solution found here:
http://code.djangoproject.c
om/wiki/NewbieMistakes#Appendingtoalistinsessiondoesntwork
a>
if 'good_message_box' not in request.session:
request.session['good_message_box'] =
MessageBox(sort='good')
mb = request.session['good_message_box']
mb.messages.append('MessageBox: You have successfully
logged in.')
request.session['good_message_box'] = mb
request.session.modified = True
It still doesn't work :( The behaviour is unchanged.
On Dec 16, 8:16 am, SmileyChris <smileych... gmail.com> wrote:
> On Dec 16, 2:13 pm, itpaul <clearersp... googlemail.com> wrote:
>
> > However, unlike the shell behaviour which lists
and deletes messages,
> > the template lists and retains the messages
resulting in them
> > displaying repeatedly on every page.
>
> Most likely, the session isn't getting saved.http://www.djangoproject.com/documentati
on/sessions/#when-sessions-ar...
>
> For the lazy:
> request.session.modified = True
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
| Re: Standard python objects in
request.session possible? |

|
2007-12-16 17:11:43 |
I've given up waiting and trying. I have a hack in the form
of a
template tag:
{% if request.session.good_message_box %}
<ul class="errorlist">
{% for m in request.session.good_message_box %}
<li>{}</li>
{% endfor %}
</ul>
{% empty_messages "good_message_box" %}
<<<<<<
{% endif %}
register.tag()
def empty_messages(parser, token):
try:
# split_contents() knows not to split quoted
strings.
tag_name, message_box = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag
requires a single
MessageBox name." % token.contents.split()[0]
if not (message_box[0] == message_box[-1] and
message_box[0] in
('"', "'")):
raise template.TemplateSyntaxError, "%r tag's
argument should
be in quotes" % tag_name
return EmptyMessagesNode(message_box[1:-1])
class EmptyMessagesNode(template.Node):
def __init__(self, message_box):
self.message_box = message_box
def render(self, context):
try:
s =
SessionStore(context['request'].session.session_key)
if self.message_box in s.keys():
s[self.message_box].messages = []
s.save()
return ''
except template.VariableDoesNotExist:
return ''
Obviously I'd rather not ask my web server to do all this 3
times
(good, bad and neutral messages) for each page view.
If anyone knows anything about the problem I've outlined, or
has a
better suggestion than my hack, please do post.
On Dec 16, 3:50 pm, itpaul <clearersp... googlemail.com> wrote:
> ok. i made a change which includes your modified-true
suggestion and
> a possibly relevant solution found here:
>
> http://code.djangoproject.com/wiki/Newbi
eMistakes#Appendingtoalistins...
>
> if 'good_message_box' not in request.session:
> request.session['good_message_box'] =
MessageBox(sort='good')
> mb = request.session['good_message_box']
> mb.messages.append('MessageBox: You have
successfully logged in.')
> request.session['good_message_box'] = mb
> request.session.modified = True
>
> It still doesn't work :( The behaviour is unchanged.
>
> On Dec 16, 8:16 am, SmileyChris <smileych... gmail.com> wrote:
>
> > On Dec 16, 2:13 pm, itpaul <clearersp... googlemail.com> wrote:
>
> > > However, unlike the shell behaviour which
lists and deletes messages,
> > > the template lists and retains the messages
resulting in them
> > > displaying repeatedly on every page.
>
> > Most likely, the session isn't getting saved.http://www.djangoproject.com/documentati
on/sessions/#when-sessions-ar...
>
> > For the lazy:
> > request.session.modified = True
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|
|
[1-5]
|
|