Calling Python function from HTML page

2,370 views
Skip to first unread message

Alan Harris-Reid

unread,
Feb 1, 2010, 12:33:43 PM2/1/10
to cherrypy-users
Hi,

I have a 'Delete' button on a HTML form frin which I want to run a
method to delete a record from a table. Then I want to refresh the
current page which to show the deleted record is no longer in a list.

So the URL calls are something like...
/MainPage # generates page containing Delete button
/MainPage/DeleteRec # calls DeleteRec() method (no HTML returned)
/MainPage # refresh main page

The Delete button currently has onclick="DeleteRec()", and the
DeleteRec Javascript function is something like...
if (confirm("Are you sure you want to delete this record?"))
window.location = "/mainpage/deleterec"
window.location = "/mainpage"
But won't the browser create a wait-state when the deleterec window is
launched?

My question is - how can I call a Python method where there is no HTML
returned and hence no web-output to be displayed? (I don't want to
display a blank page that has to be closed manually - it looks
silly!) I guess it has to be via. a URL (is there any other way?) If
there is no-way around the problem, could I direct a blank page to a
'hidden' browser window, then close it so that control moves onto
redisplaying /mainpage?

For the record, I am using CP 3.2.0,

Any help would be appreciated.
Alan Harris-Reid

Vivek Khurana

unread,
Feb 1, 2010, 12:55:20 PM2/1/10
to cherryp...@googlegroups.com
On Mon, Feb 1, 2010 at 11:03 PM, Alan Harris-Reid
<aharr...@googlemail.com> wrote:
>
> My question is - how can I call a Python method where there is no HTML
> returned and hence no web-output to be displayed? (I don't want to
> display a blank page that has to be closed manually - it looks
> silly!)  I guess it has to be via. a URL (is there any other way?)  If
> there is no-way around the problem, could I direct a blank page to a
> 'hidden' browser window, then close it so that control moves onto
> redisplaying /mainpage?
>

You have two options here

1) Use ajax call to delete the record and update the user with a
message whether the record was deleted successfully or not.
2) In the DeleteRec function use cherrypy.InternalRedirect to
internally redirect cherrypy to listing page. This will keep the url
in the browser unchanged (as /mainpage/deleterec) but internally call
listing page and display the content of listing page.

regards
Vivek

--
The hidden harmony is better than the obvious!!

Tim Roberts

unread,
Feb 1, 2010, 3:48:54 PM2/1/10
to cherryp...@googlegroups.com
Alan Harris-Reid wrote:
> Hi,
>
> I have a 'Delete' button on a HTML form frin which I want to run a
> method to delete a record from a table. Then I want to refresh the
> current page which to show the deleted record is no longer in a list.
>
> So the URL calls are something like...
> /MainPage # generates page containing Delete button
> /MainPage/DeleteRec # calls DeleteRec() method (no HTML returned)
> /MainPage # refresh main page
>

Every HTTP request has to generate HTML. Every HTTP request really has
two parts -- what does it DO, and what does it RETURN? In this case,
you want the DeleteRec request to delete a record, and then return the
MainPage HTML. There are several ways to do that.

One way is to have the DeleteRec request just call the MainPage handler
in your code, so that it returns the basic HTML. The downside of this
is that it leaves the URL in the browser set to "/MainPage/DeleteRec".
You would have to just whether that was good or bad.

Another way is to have the DeleteRec handler do a redirect to /MainPage,
which returns a minimal HTML page that forces the browser to make a
brand new request for /MainPage.

You'll have to judge which one is acceptable in your situation.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Paweł Stradomski

unread,
Feb 1, 2010, 4:13:01 PM2/1/10
to cherryp...@googlegroups.com
W liście Vivek Khurana z dnia poniedziałek 01 lutego 2010:

You could also:
3) use external redirect - after deleting, send a redirect to the listing
page
4) Use 204 or 205 status codes - the latter is probably better, as it tells
the browser to refresh current page. Niether requires any content.


Personally I'd go with 1 (ajax request) or 3 (external redirect).

--
Paweł Stradomski

Alan Harris-Reid

unread,
Feb 2, 2010, 6:57:39 AM2/2/10
to cherrypy-users

Thanks to all who replied to this thread so far.

I think I will go for the redirection option at this stage - I'm just
getting to grips with Python, CherryPy, HTML, JavaScript, etc. so I
don't want to confuse my brain with AJAX at this stage.

However, Vivek suggested an internal redirect, but Pawel recommends an
external redirect. Are they both correct? What is the difference
between internal/external redirection? Any clarification would be
appreciated.

Alan

Paweł Stradomski

unread,
Feb 2, 2010, 8:25:47 AM2/2/10
to cherryp...@googlegroups.com
2010/2/2 Alan Harris-Reid <aharr...@googlemail.com>:

>
> Thanks to all who replied to this thread so far.
>
> However, Vivek suggested an internal redirect, but Pawel recommends an
> external redirect.  Are they both correct?  What is the difference
> between internal/external redirection?  Any clarification would be
> appreciated.

External redirect (or just "redirect") means the servers sends a
response to the client that tells him
to fetch another page. So, the dialog would look like (C - client, S -Server:

Request one:
C: POST /delete_a_thing HTTP/1.1
C: <headers go here, then content>
S: HTTP/1.1 303 See Other
S: Location: http://yourhost.com/some/path/

Request two:
C: GET /some/path HTTP/1.1
C: <headers go here>
S: HTTP/1.1 200 OK
S: <headers, then content>

In that case the browser will display /some/path in the url bar.
Also, refreshing the page will not try to perform the same action
again.

Internal redirect never leaves the server - it just instructs it to
start request processing from scratch - but using new url. So the chat
looks like:

C: POST /delete_a_thing HTTP/1.1
C: <headers go here, then content>
S: HTTP/1.1 200 OK
S: <headers, then content>

Just one request here. But, the browser will display /delete_a_thing
in the url bar. Also, refreshing will probably try to send the POST
again, which is undesirable.

I don't like internal redirects (at least for that purpose), as they
lead to a situation where resource is displayed from unrelated urls,
which can cause problems with bookmarks etc (user might bookmark
/delete_a_thing page and hope it will lead to the list of things,
which is at /some/path in the above example).

--
Paweł Stradomski

Alan Harris-Reid

unread,
Feb 2, 2010, 10:06:26 AM2/2/10
to cherrypy-users

On Feb 2, 1:25 pm, Paweł Stradomski <pstradom...@gmail.com> wrote:


> I don't like internal redirects (at least for that purpose), as they
> lead to a situation where resource is displayed from unrelated urls,
> which can cause problems with bookmarks etc (user might bookmark
> /delete_a_thing page and hope it will lead to the list of things,
> which is at /some/path in the above example).
>
> --

Thanks for that Pawel,

Looks like external direction is the answer to my problem, because the
page containing the delete button is about 4 pages into a sequence
(home page, item select page, item display page, item-delete page), so
I wouldn't want a user being issued with the item-delete page at the
start of a sequence as if it was the home-page (it would crash very
quickly!)

Regards,
Alan

Reply all
Reply to author
Forward
0 new messages