views:

712

answers:

1

Absolute beginner question:

I have a template file index.html that looks like this:

...
<FRAMESET ROWS="10%, *">
    <FRAME SRC="/top_frame">
    <FRAME SRC="{{ bottom_frame_url }}">
</FRAMESET>
...

And a request handler for /top_frame that looks like this:

class TopFrame(webapp.RequestHandler):
    def get(self):
        ...
        bottom_frame_url = self.request.get('bottom_frame_url')
        ...

As you can see I would like to have the value {{ bottom_frame_url }} that was used to generate my index.html, but how to I pass this value to my TopFrame request handler?

Thanks!

Edit: I am rendering index.html from another request handler:

class MainPage(webapp.RequestHandler):
def get(self):
    ...
    bottom_frame_url = Site.qql("WHERE Category = :1 ORDER BY date DESC",category_name)
    ...
    args = {
      ...
      'bottom_frame_url': bottom_frame_url,
      ...
    }
    index_path = os.path.join(os.path.dirname(__file__), 'index.html')     
    self.response.out.write(template.render(index_path, args))

But when we encounter "/top_frame" in the index.html template my TopFrame request handler is called:

class TopFrame(webapp.RequestHandler):
def get(self):
    ...
    bottom_frame_url = self.request.get('bottom_frame_url')
    ...
    args = {
        'bottom_frame_url': bottom_frame_url
    }
    self.response.out.write(template.render('topframe.html', args))

But self.request.get('bottom_frame_url') does not seem to be reading any value. I know the value has been set in my index.html template because when I render the page the bottom frame shows as the website I set, but the top frame displays a traceback ending in the following error:

NameError: global name 'bottom_frame_url' is not defined

It looks like I have to put some more code in my index.html template to explicitly pass the value of bottom_frame_url to my TopFrame request handler. Is this correct?

The other thing I can think of is that although I am passing bottom_frame_url as an argument to render index.html, when I try to render topframe.html this argument isn't available yet, because I haven't rendered the bottom frame yet. Could this be the case?

+2  A: 

It's a parameter to the call to render the template:

args = {
    bottom_frame_url: self.request.get('bottom_frame_url')
}
self.response.out.write(template.render('index.html', args))

See http://code.google.com/appengine/docs/python/gettingstarted/templates.html

Edit: I'm sorry, I misread the the question. I thought that the request handler containing the code you showed was the request handler for the frameset, but I see now that it isn't.

self.request.get can only get the parameters for the current HTTP request. Your setup will result in three HTTP requests: one for the frameset (with whatever URL that handler is), one for the top frame (with the URL "/top_frame") and one for the bottom frame (with the URL specified by the bottom_frame_url parameter to the first HTTP request). The second and third won't necessarily occur in that order, and there may be other requests for images and so on. But the order has no affect on what the top frame handler sees in self.request.get. Each request results in your app.yaml being consulted, and the appropriate Python script run, and the appropriate handler within it being called. Each request's parameters are only what is set in the query string or POST params for that one request.

So, if you want the handler for the top frame to see the bottom_frame_url via self.request.get, then you have to add it as a URL parameter for the request which causes the top frame handler to be run. Change the template to:

<FRAMESET ROWS="10%, *">
    <FRAME SRC="/top_frame?bottom_frame_url={{bottom_frame_url}}">
    <FRAME SRC="{{ bottom_frame_url }}">
</FRAMESET>

Now when the browser requests the top frame, you will be able to access it in that handler, with self.request.get('bottom_frame_url'), just as you did in the handler for the frameset.

Always remember when doing this kind of thing that users can change URL parameters to any values they like. If you can, it might be better to pull it from the database again in the other handler.

Steve Jessop
Please see my edit.
Imran
Yes, that works. Thank you!
Imran