How to use the same variable in two handlers?

22 views
Skip to first unread message

Zeynel

unread,
Jan 22, 2011, 12:04:17 PM1/22/11
to Google App Engine
Hello,

In the Directory handler I have

merchandise_type = self.request.get("type")

How do I use merchandise_type in the DirectorySubmitHandler?

class Directory(webapp.RequestHandler):
def get(self):
....
merchandise_type = self.request.get("type", "")
items = Item.all()
items.filter("type =", merchandise_type)

...

class DirectorySubmitHandler(webapp.RequestHandler):
def post(self):
user = users.get_current_user()
#the following line gives unknown global name error
dir_type = merchandise_type
if user:
item = Item()
item.title = self.request.get("title")
item.url = self.request.get("url")
item.type = self.request.get("dir_type")
item.user_who_liked_this_item = user
item.put()
self.redirect("/dir?type=%s" %
self.request.get("dir_type"))
else:
self.redirect(users.create_login_url(self.request.uri))

Thanks!

djidjadji

unread,
Jan 22, 2011, 12:07:43 PM1/22/11
to google-a...@googlegroups.com
Make merchandise_type a hidden <input> field of the form generated by
Directory() and get the value in the DirectorySubmitHandler().

Zeynel

unread,
Jan 22, 2011, 12:36:42 PM1/22/11
to Google App Engine
On Jan 22, 12:07 pm, djidjadji <djidja...@gmail.com> wrote:

> Make merchandise_type a hidden <input> field of the form generated by
> Directory() and get the value in the DirectorySubmitHandler().


Ok. I have the hidden field in the form in Directory like this:

<input type="hidden" name="dir_type" value="merchandise_type">

but how do I get that

the value of the "merchandise_type" in the DirectorySubmitHandler?

self.request.get("dir_type") is "merchandise_type" not the url
parameter?

Thanks!

djidjadji

unread,
Jan 22, 2011, 12:53:03 PM1/22/11
to google-a...@googlegroups.com
<input type="hidden" name="dir_type" value="###">

instead of ### print the value of the variable merchandise_type

2011/1/22 Zeynel <azey...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Zeynel

unread,
Jan 22, 2011, 1:01:27 PM1/22/11
to Google App Engine
On Jan 22, 12:53 pm, djidjadji <djidja...@gmail.com> wrote:
> <input type="hidden" name="dir_type" value="###">
>
> instead of ### print the value of the variable merchandise_type
>
Thanks, but I don't understand. The value of the url parameter is
given by

self.request.get("type", "")

I cannot put this value in the value="###" field. Then what is in the
value field?

Robert Kluin

unread,
Jan 22, 2011, 2:00:57 PM1/22/11
to google-a...@googlegroups.com
Hi Zeynel,
You'll have to show us how you are rendering the form. If you're
using template, just pass the value you get for type to your template
and print the value, like djidjadji said.

Robert

Zeynel

unread,
Jan 22, 2011, 2:55:17 PM1/22/11
to Google App Engine
On Jan 22, 2:00 pm, Robert Kluin <robert.kl...@gmail.com> wrote:

>   You'll have to show us how you are rendering the form.  If you're
> using template, just pass the value you get for type to your template
> and print the value, like djidjadji said.

Sorry, I am confused about this. The form is not rendered;
DirectorySubmitHandler handles the form submission to write the
submission to the database and then redirects to Directory which pulls
the relevant merchandise_type from database and renders it.

The url parameter is saved in Directory as

merchandise_type = self.request.get("type")

So, Python will not let me use merchandise_type in
DirectorySubmitHandler.

I don't know how I can pass merchandise_type to the form. Please let
me know what I am missing here.

<input type="hidden" name="dir_type" value="I don't know what should
go here">

I am not using templates.

I need to use merchandise_type in DirectorySubmitHandler to redirect
to Directory.

Maybe I should handle the form with Directory too, but not sure if I
can do that.

I copy the handlers below from my first post. If more information is
needed please let me know:
Thanks again.

Ernesto Karim Oltra

unread,
Jan 22, 2011, 4:31:27 PM1/22/11
to Google App Engine
Hi,

"render" is a common word meaning build the form. You are sending the
user a page, with the form. How do you build that page or how do you
print the HTML you've written?

PD: Have you seen the Python tutorial?
http://code.google.com/intl/en/appengine/docs/python/gettingstarted/

djidjadji

unread,
Jan 22, 2011, 4:53:12 PM1/22/11
to google-a...@googlegroups.com
urllib.urlencode() should be used to quote the parameter

If Directory() is processed after DirectorySubmitHandler() you can
NEVER get the merchandise_type variable.
And it is possibly handled in another app instance.

dir_type = merchandise_type # remove this line

dir_type is never used in DirectorySubmitHandler()

------------------------------------
import urllib

self.redirect("/dir?%s" % urllib.urlencode({ 'type' :
self.request.get("dir_type") } )

Zeynel

unread,
Jan 22, 2011, 6:19:16 PM1/22/11
to Google App Engine
On Jan 22, 4:53 pm, djidjadji <djidja...@gmail.com> wrote:

> urllib.urlencode() should be used to quote the parameter
> import urllib
>
> self.redirect("/dir?%s" % urllib.urlencode({ 'type' :
> self.request.get("dir_type") } )

Thanks. But obviously I am missing something here. The value of "type"
in DirectorySubmitHandler

logging.info("type is: %s" % type)

is

type is: <type 'type'>

So, in DirectorySubmitHandler, type is not a variable containing the
url parameter. Or I am not applying what you suggested correctly.

class DirectorySubmitHandler(webapp.RequestHandler):
def post(self):
user = users.get_current_user()
if user:
item = Item()
item.title = self.request.get("title")
item.url = self.request.get("url")
item.type = urllib.urlencode({ 'type' :
self.request.get("dir_type") } )
item.user_who_liked_this_item = user
item.put()
self.redirect("/dir?type=%s" % urllib.urlencode({ "type" :
self.request.get("dir_type") } ))
else:
self.redirect(users.create_login_url(self.request.uri))



djidjadji

unread,
Jan 22, 2011, 8:02:46 PM1/22/11
to google-a...@googlegroups.com
'type' is between quotes ==> a string.
Has nothing to do with a variable or a function object.

Zeynel

unread,
Jan 22, 2011, 8:33:39 PM1/22/11
to Google App Engine
On Jan 22, 8:02 pm, djidjadji <djidja...@gmail.com> wrote:

> 'type' is between quotes ==> a string.
> Has nothing to do with a variable or a function object.

I understand this. If I use this solution the url is always

/dir?type=type=

"type" is not replaced with the merchandise_type. Maybe I am not using
it correctly?

Robert Kluin

unread,
Jan 22, 2011, 10:35:33 PM1/22/11
to google-a...@googlegroups.com
So, if you are getting 'type=' twice, did you think about trying:

self.redirect("/dir?%s" % urllib.urlencode({ "type" :
self.request.get("dir_type") } ))

Zeynel

unread,
Jan 22, 2011, 11:06:09 PM1/22/11
to Google App Engine
On Jan 22, 10:35 pm, Robert Kluin <robert.kl...@gmail.com> wrote:
> So, if you are getting 'type=' twice, did you think about trying:
>   self.redirect("/dir?%s" % urllib.urlencode({ "type" :
> self.request.get("dir_type") } ))

Yes, actually I thought about that but I did not try it because I did
not get

/dir?type=type=tshirt

but just /dir?type=type=

without the url parameter. But I may be wrong so I will try. But now I
am working on trying to make this work with templates as explained
here: http://stackoverflow.com/questions/4769063/how-do-i-pass-url-parameter-to-form-value

Unfortunately, I have an irrational hatred for Django templates so I
am trying to do this with Mako templates. Researching this I found an
old answer of yours: http://stackoverflow.com/questions/3938963/mako-templates-with-google-app-engine/3939208#3939208.
Thanks again for that answer. At this point my template page is
printing as text like this

<�html> <�body> <�p>Render template<�/p> ${greeting} <�/body> <�/html>

so I am going to try to fix that first and then try the urllib option.

Robert Kluin

unread,
Jan 23, 2011, 1:12:50 AM1/23/11
to google-a...@googlegroups.com
If you're trying to do this using Mako templates, here is a fully
functional 'demo' that you should be able to easily adapt to do
exactly what you're asking about.


app.yaml
--------
handlers:
- url: /makodemo.*
script: makodemo.py
login: admin


makodemo.py
-----------
#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from mako.lookup import TemplateLookup
from mako import exceptions

class DemoMakoTemplateHandler(webapp.RequestHandler):
"""Simple demo using mako templates."""
def __init__(self, **kwargs):
webapp.RequestHandler.__init__(self, **kwargs)
self.template_values = {'message': ''}

def render(self, template_name, values=None):
"""Setup search paths, and render the template.

Any values passed in will be combined with
self.template_values, and passed to the template.
"""
directories = ['templates']
lookup = TemplateLookup(directories=directories)
template = lookup.get_template(template_name)
if values:
self.template_values.update(values)
try:
self.response.out.write(template.render(**self.template_values))
except:
self.response.out.write(exceptions.html_error_template().render())

def get(self):
"""Render the template."""
self.render("demo.mako")

def post(self):
"""Get the user's message then render the template again."""
message = self.request.get('message')
self.render("demo.mako", {'message': message})

def main():
application = webapp.WSGIApplication([('/makodemo', DemoMakoTemplateHandler)],
debug=True)
run_wsgi_app(application)

if __name__ == '__main__':
main()


demo.mako
---------
<html>
<head>
<title>Mako Demo</title>
</head>
<body>
<div>Last message was: ${ message }</div>
<form action="/makodemo" method="post">
<input type="text" name="message" value="${ message }"></input>
<input type="submit" name="doit" value="update"></input>
</form>
</body>
</html>

Make sure you've went through the app engine getting started and
really looked at how things work carefully. No matter which template
language you chose, be sure to review their docs too.
http://code.google.com/appengine/docs/python/gettingstarted/introduction.html
http://www.makotemplates.org/docs/


Robert

djidjadji

unread,
Jan 23, 2011, 5:23:28 AM1/23/11
to google-a...@googlegroups.com
With what you want to do there IS NO difference in doing it in Django
templates then in Mako templates.
That will result in a hatred awards Mako too.

2011/1/23 Zeynel <azey...@gmail.com>:

Zeynel

unread,
Jan 23, 2011, 10:35:22 AM1/23/11
to Google App Engine
On Jan 23, 1:12 am, Robert Kluin <robert.kl...@gmail.com> wrote:

> If you're trying to do this using Mako templates, here is a fully
> functional 'demo' that you should be able to easily adapt to do
> exactly what you're asking about.

Thanks! But I decided in principle not to use templates at this stage.
It is better for me to see all the code in one file. I tried the
template solution with the django templates and it works. But then I
found the way to pass the url parameter to the form:

self.response.out.write("""
<form name="submit_form" action="/directorysubmithandler"
method="post" onSubmit="return validate_form()">
title: <input type="text" name="title" size=50><br />
url: <input type="text" name="url" size=50><br />
<input type="hidden" name="dir_type" value="%s")>
<input type="submit" value="submit">
</form>""" % self.request.get("type"))

and this works.

Thanks again for your help.
Reply all
Reply to author
Forward
0 new messages