Complete Newb Question

11 views
Skip to first unread message

fishfin

unread,
Oct 25, 2008, 2:40:05 AM10/25/08
to Google App Engine
I'm coming over from php and am trying to figure out how to do
something.

In php you can put data in the user's address bar (index.php?
data=somedata) and then get it from the address bar really easily (you
just use $_GET and $_REQUEST), so I was wondering what the equivalent
is in python?

loell

unread,
Oct 25, 2008, 11:16:38 AM10/25/08
to Google App Engine
Typically, it should go something like this,

class MainPage(webapp.RequestHandler):
def get(self):
data = cgi.escape(self.request.get('data'))

fishfin

unread,
Oct 25, 2008, 8:20:03 PM10/25/08
to Google App Engine
How would I get that data later on? 'print data' doesn't seem to work,
'print self.data' and 'print MainPage.data' don't seem to work either.

jeremy

unread,
Oct 25, 2008, 8:39:00 PM10/25/08
to Google App Engine
When you say "later on", where exactly do you mean? If you want to
access it from another handler you will probably want to datastore it.

You can write to the output buffer by doing
self.response.out.write(<whatever string>).

fishfin

unread,
Oct 25, 2008, 8:49:07 PM10/25/08
to Google App Engine
My guess is that the code snippet above gets the value of 'data' from
the address bar, what I want to know how to do is to use the value of
'data' later on in my code. When I just try to use the variable 'data'
I get a variable undefined error message.

Hakayati

unread,
Oct 25, 2008, 4:35:10 AM10/25/08
to Google App Engine
You can retrieve URL parameters from the request object like this:

# e.g. www.mysite.com/?my_parameter=hello%20world

class MainPage(webapp.RequestHandler):
def get(self):
my_parameter = ''
for param in self.request.query.split('&'):
if param.startswith('my_parameter'):
my_parameter = param.split('=')[1]

fishfin

unread,
Oct 26, 2008, 4:05:02 AM10/26/08
to Google App Engine
Ok, I don't think I'm being very clear = )

Say a user visits my website with this address bar:
https://www.mywebsite.com/index.htm?data=123xyz

I would like to place the '123xyz' into a variable so that I can
access it whenever I want to. I think that loell's code does that
except that I can't figure out how to get the value of 'data.'

After using this: (and importing cgi and google.appengine.ext.webapp)

class MainPage(webapp.RequestHandler):
def get(self):
data = cgi.escape(self.request.get('data'))

I've tried:
print data
print MainPage.data
print MainPage.self.data
etc.

I can't figure out how to access the '123xyz' from the url. I get the
same error message every time: name 'data' is not defined
Message has been deleted

fishfin

unread,
Oct 27, 2008, 3:44:44 AM10/27/08
to Google App Engine
I've been reading through the documentation a little bit more
carefully and decided to try this:

from google.appengine.ext.webapp import Request

test = Request.get(test)

and I get an error message about how something is insufficient...

On Oct 26, 7:01 pm, jeremy <jeremy.a...@gmail.com> wrote:
> Are you calling those print statements in the same scope as
> MainPage.get? If you could paste your entire handler script, that
> might help us identify what's going wrong.

loell

unread,
Oct 27, 2008, 4:16:19 AM10/27/08
to Google App Engine
you need to enclose that in a class like the examples you've been
reading,

and enclose that line, specifically on a get method. let's say for
example you named the class as "MainPage" in the above example , then
it must be mapped in "/" like

-------

from google.appengine.ext.webapp import Request
from google.appengine.ext import webapp

class MainPage(webapp.RequestHandler):
def get(self):
test = cgi.escape(self.request.get('test')
#eeewww, i'm using print :P
print test


application = webapp.WSGIApplication([('/',MainPage)], debug = True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()

Dan Sanderson

unread,
Oct 27, 2008, 12:55:34 PM10/27/08
to google-a...@googlegroups.com
You shouldn't need cgi.escape(), either:

class MainPage(webapp.RequestHandler):
  def get(self):
    data = self.request.get('123xyz')
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.out.write('Hello world! ' + data)


-- Dan

fishfin

unread,
Oct 30, 2008, 8:39:14 AM10/30/08
to Google App Engine
Stupid, stupid, stupid me. My problem all along was that I was
capitalizing 'Request' when it should be 'request.'

It was soo obvious... I should have caught it right away.

On Oct 28, 12:55 am, "Dan Sanderson" <dansander...@google.com> wrote:
> You shouldn't need cgi.escape(), either:
> class MainPage(webapp.RequestHandler):
>   def get(self):
>     data = self.request.get('123xyz')
>     self.response.headers['Content-Type'] = 'text/plain'
>     self.response.out.write('Hello world! ' + data)
>
> http://localhost:8080/?123xyz=blah%20blah
>
> -- Dan
>
Reply all
Reply to author
Forward
0 new messages