On Jul 21, 7:13 pm, huydo <contactm...@gmail.com> wrote:
> On Jul 22, 2:17 am, Wyatt Baldwin <wyatt.lee.bald...@gmail.com> wrote:
>
>
>
>
>
> > On Jul 21, 5:56 am, huydo <contactm...@gmail.com> wrote:
>
> > > Hi,
>
> > > Just trying to do a direct from the __call__ method in my base.py file
> > > and this is the following exception which I am getting.
>
> > >   redirect(url(controller='redirect', action='index'))
> > > File 'c:\\apps\\xbits\\ktrack\\python\\lib\\site-packages\\pylons-1.0-
> > > py2.5.egg\\pylons\\controllers\\util.py', line 208 in redirect
> > >   raise exc(location=url).exception
> > > HTTPFound: 302 Found
> > > Content-Type: text/html; charset=UTF-8
> > > Content-Length: 0
> > > Location: /redirect/index
>
> > > Adding the following line to my middleware.py file (just after the
> > > #CUSTOM MIDDLE HERE) fixed it.
>
> > > app = httpexceptions.make_middleware(app)
>
> > > Is this a bug ? or am I doing something wrong ?
>
> > I'd say it's more that you're doing something wrong. ;) redirect works
> > by raising an exception (the one you're seeing). Normally, this would
> > be caught by the base WSGIController class (in _inspect_call) and
> > converted into an HTTP response, but because __call__ is a setup
> > function and not part of the "action context", the exception isn't
> > caught, presumably by design.
>
> > In other words, you should only use redirect from within the "action
> > context"--either in __before__, a particular action method, or
> > __after__. If you really want to redirect from __call__, you can
> > create an instance of HTTPFound and return that from __call__.
>
> Hi Wyatt,
>
> Thanks for the pointer. It does appear redirecting in __call__ does
> not work.
> I have moved all my logic which was in __call__ to __before__ and
> redirect works in there.
>
> It would be nice to be able to return a response in __before__ as well
> rather then only be allowed to raise an exception.

It's a bit of a hack and probably not recommended, but you could do
this:

def __before__(self, ...):
    response = some response
    response._exception = True
    return response

But this pretty much what Pylons does for you when you use the
redirect function or raise any other subclass of
webob.exc.HTTPException in __before__. I think if you are returning
default content under some set of conditions that redirecting is the
"proper" thing to do anyway. Or did you have a different use case in
mind?

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

Reply via email to