Python - How do i know when a form button was pressed? (logout)
I'm trying to make a logout button for my website in python CGI. I was wondering how would I know when the button was pressed?
My logout form is as follows:
<form id="logout" method="POST" action="">
<i开发者_StackOverflow社区nput type="submit" name="logout" value="Logout">
</form>
From here, i know that I need to store the form in FieldStorage so I did the following:
form = cgi.FieldStorage()
I've tried doing an if statement such as:
if form.has_key('logout'):
doLogoutFunction()
but it doesn't work. Please help
You need not do handing of logout via POST
and also you need to point out to something in the action field for handing either GET
or the POST
Here is an example which shows how to handle it via GET any form submit. You can simply allow clicking of the logout link to a particular portion in the url /logout
and handle the GET that the location to log off from the application.
If you really want to do via forms, you can have a hidden field for the form and then verify the value.
<html><body>
<form method="get" action="logoff.py">
Name: <input type="hidden" name="hidden">
<input type="submit" value="Logoff">
</form>
</body></html>
And the corresponding CGI Python script as:
import cgi
form = cgi.FieldStorage() # instantiate only once!
name = form.getfirst('name')
if name == 'hidden':
# do your action
精彩评论