[Zope] Problems with External Script

Paul Winkler pw_lists@slinkp.com
Thu, 31 Jan 2002 21:25:24 -0500


On Thu, Jan 31, 2002 at 04:37:05PM -0500, Thomas B. Passin wrote:
> [Ben Ocean]
> 
> >
> > Okay, gotta be close by now :/
> > Here's my script:
> >
> > import string
> >
> > def myaffiliateprogram(REQUEST,RESPONSE):
> >   user_id, banner_id, page =
> > REQUEST['user_id'],REQUEST['banner_id'],REQUEST['page']
> >   redir =
> >
> "http://www.myaffiliateprogram.com/u/%(user_id)s/t.asp?id=%(banner_id)s&p=%(
> page)s"
> > % locals()

(snip)
 
> This isn't proper python, so I don't see how it would have worked at all.
> If you want to interpolate variables into a string, you have to use "%s",
> not "%(something else)s".  If that's not what you are trying to do, the
> expression % locals() isn't valid.

Actually, that part *is* proper python.

$ python
Python 2.1.2 (#1, Jan 18 2002, 18:05:45) 
[GCC 2.95.4  (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> foo=1
>>> bar = 2
>>> "foo is %(foo)d, and bar is %(bar)d" % vars()
'foo is 1, and bar is 2'
>>> "foo is %(foo)d, and bar is %(bar)d" % locals()
'foo is 1, and bar is 2'

See for more info:
http://www.python.org/doc/current/lib/typesseq-strings.html


Anyway, as for the other attempts:

> > # location=RESPONSE.redirect("%(redir)s\n % locals()")

Close, but no cigar. First of all, you just want to call
RESPONSE.redirect(). You don't want to store the return value.
Secondly, the string interpolation has the quotes in the wrong
place. should look like this:

"%(redir)s\n" % locals()

> > # location-RESPONSE.redirect("http://thewebsons.com")

You're subracting the result of RESPONSE.redirect() from location.
Definitely not what you want!

> > # location=redirect("http://thewebsons.com")

You're getting colder, you have to give a namespace to find the
redirect method in. It lives in the RESPONSE object.

> >   location: redirect("http://thewebsons.com")

Colons have a limited number of meanings in python, and that's not one
of them. :)

I get the feeling you don't really grok python. I suggest spending a
little time with one of the tutorials. For instance:
http://www.python.org/doc/current/tut/tut.html

It's a great little language, and once you get the hang of it, Zope
will open for you like an oyster with a great big shiny pearl inside
(no pun intended).

> > All of those options (including the ones commented out) return a bad
> server
> > request (Apache error #400).
> >

I guess you're running behind Apache and it's helpfully hiding your
traceback errors? That's not so helpful.

Here's how to do a redirect. You don't use a return value from the
redirect method; you just call it as the last step in your script or
external method.

  RESPONSE.redirect("http://www.someplace.com")

or

  REQUEST.RESPONSE.redirect("http://www.someplace.com")

I've also seen it like this in the CMF sources, not sure why they
write it this way, but it does the same thing.

  REQUEST['RESPONSE'].redirect("http://www.someplace.com")


-- 

paul winkler
home:  http://www.slinkp.com
music: http://www.reacharms.com
calendars: http://www.calendargalaxy.com