On Mon, Mar 7, 2011 at 9:52 AM, Erik Goldman <[email protected]> wrote:
> I'm designing a simple web app and I'd like a unified system for
> storing errors to show in templates.
>
> What I did initially was to create a custom request factory:
>
>
> class UserRequest(Request):
>  errors = {}
>
> and then I simply do request.errors[name] = error_text and read from
> that dictionary on the template side.
>
> The problem is that these errors seem to stick around... if I do
> something that triggers an error, I get the page back with the error
> text (great!) but then I surf to another page and type that URL back
> into the address bar... and I get the error again.
>
> So then I tried:
>
>
> class UserRequest(Request):
>  errors = {}
>
>  def __init__(self, environ):
>    super(UserRequest, self).__init__(environ)
>    self.errors = {}
>
> but now I *never* see errors!!
>
> what's going on?  how do I fix this?

You need to learn about the difference between class and instance
variables in Python.  Check out
http://docs.python.org/tutorial/classes.html

I can only guess what went wrong when you set 'errors' on the
instance, as with your second example.  I guess that you did not set
and read the error in that order in the same request.  And that's
perfectly fine.  What you want is use the Flash Messages API [1] and
use the 'queue' argument as your 'name'.

[1] 
http://docs.pylonsproject.org/projects/pyramid/1.0/narr/sessions.html#flash-messages

-- 
http://danielnouri.org

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to