Rishav Sharan
Rishav Sharan

Reputation: 2932

refreshing just a div on http post

In this code;

def post(self):
    chtml = displayhtml(public_key, error = None)
    template_values = {'captchahtml': chtml}
    path = os.path.join(os.path.dirname(__file__), 'main.html')
    self.response.out.write(template.render(path, template_values))

when the http post request is made, i can refresh the entire page with the given template elements.

my question is if its possible to refresh just a div or iframe instead of the entire page?

Upvotes: 0

Views: 376

Answers (1)

Cheeso
Cheeso

Reputation: 192477

Yes, it is possible to refresh part of a page during a request.

But it requires some application or UI logic on the browser side, to facilitate that.

One way is to use a dedicated iframe. Think of the iframe as just a browser-window-within-a-browser-window. You can insert any content you like into an iframe, and you can refresh the iframe independently of the content surrounding it in the page. The iframe approach is often used in the case where the content for the surrounding page and the content for the iframe itself, are sourced from different places. For example a facebook "like widget" uses content from facebook. For a long time Facebook made available a way for people to embed in their pages, an iframe containing just the like widget. In this way facebook was able to maintain control over its imagery, branding, look and feel, and so on, regardless of what kind of page the widget was embedded into.

When the content for the frame and the content for the surrounding bit are from the same source -like your appengine instance, then you don't need an iframe. You can use a simple div, and update it asynchronously.

This approach is sometimes referred to as AJAX - referring to Asynchronous Javascript and XML. The basic idea implies an async request (out of the normal page refresh cycle) made from the browser to the server, to get content; the browser logic implemented in javascript can then update the UI in any way it chooses.

Contrary to the name, It is not imperative to send out XML as a response from such an async request. In fact a more common approach is to use JSON as the data format. But certainly, when using this design pattern, your Python code on the server would generally not return formatted html. It would return data in some reasonable format (XML, JSON, other), and then Javascript logic running in the browser would render that data as HTML, in the appropriate place.

In the "standard" web browser model, there is one HTTP request sent per page request (although additional requests may be sent for .css files, images and so on), and each additional request implies a full page refresh. In the AJAX model, the browser requests the web page initially, as before, but can send out additional HTTP requests asynchronously, without refreshing the page fully.

Upvotes: 1

Related Questions