On Mon, 2011-03-07 at 23:02 -0800, Erik Goldman wrote:
> doh, I'm an idiot and don't know python =)
> 
> that said, are flash messages the best system for this?  I don't need
> them in a session queue or stored locally, I just need them for my
> view code so I can stash them somewhere and have them show up to the
> templating system in one shot.  I don't need them persisting between
> calls, for example, it would just be nice to unify my error handling
> code so I don't have to keep adding errors to the dictionary that I
> hand over to mako.

If you just need your template to see some values, could you just start
off by passing them in directly?  E.g.

@view_config(renderer='whatever.mako')
def theview(request):
    the_errors = []
    ... collect up any errors  and put them in the_errors ...
    return {'errors':the_errors, ....}

There are sometimes good reasons why this isn't sufficient, but is it
insufficient for you?

> that's why I thought I'd add a property to the request object.. it
> seemed like it was created before my view code ran and destroyed only
> after my templating system got a look at it.  but now I'm still not
> seeing errors when I do:
> 
> class UserRequest(Request):
>   def __init__(self, environ):
>     super(UserRequest, self).__init__(environ)
>     self.errors = {} # this is apparently too aggressive and we don't
> see errors... why?
> 
> and doing stuff like request.errors['username'] = 'Username in use'
> 
> On Mon, Mar 7, 2011 at 9:44 AM, Daniel Nouri <[email protected]> wrote:
> > 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