Python Web Beginner: How to redirect to next page

unable to do validation and redirect

Page 1 of 1

0 Replies - 1270 Views - Last Post: 07 May 2009 - 03:18 AM Rate Topic: -----

#1 tks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-May 09

Python Web Beginner: How to redirect to next page

Posted 07 May 2009 - 03:18 AM

Hi I am new to Python. Sadly I have to create a web application urgently. (in Windows OS). Two screens are required as for now:
1) Login
2) Search

Given below is the script that I have written for Login page. It does not throw any error on run but on browsing (the 'login.py' in inetmgr), I am unable to redirect/validate the screen.


#!c:\Python25\python.exe
import cgi
import cgitb; cgitb.enable()

print "Content-type: text/html"
print """
<HTML>
<HEAD>
<TITLE>Login Form</TITLE>
</HEAD>
<BODY BGCOLOR = white>
<TABLE BORDER = 0>

<TR>
	<TH>UserName:</TH>
	<TD><INPUT type = text name = "user"></TD>
<TR>
<TR>
	<TH>Password:</TH>
	<TD><INPUT type = password name = "psw">
	</TD>
</TR>
</TABLE>
<INPUT TYPE = hidden NAME = "action" VALUE ="display">
<INPUT TYPE = SUBMIT NAME = "login" VALUE = "Login" onsubmit = validate() >

</BODY>
</HTML>"""


def validate():
	form = cgi.FieldStorage()
	txt_usr = form.getfirst("user")
	txt_psw = form.getfirst("psw")
	b_login = form.getfirst ("login")
	if(txt_usr !="" and txt_psw !=""):
		try:
			
			db = MySQLdb.connect(host='<host_ip>', user='<username>' , passwd='<pasword>', db='<db_name>')
			curs = db.cursor()
			curs.execute("SELECT query.. = %s;", txt_usr)
			pswrd = curs.fetchone()
			for pwd in pswrd:
				if pwd == txt_psw:
					print "Content-type: text/html"
					print """
							<HTML>
								Second page
							</HTML>
							"""
				else:
					print "invalid password"
		except:
			print "Username incorrect! Enter valid username"					
	else:
		 print "Enter username and password"




I know that we can redirect to next page by including following in the login HTML :

<FORM METHOD = POST ACTION = "\Python\Search.py">

but this again goes to next page without validation..
I checked out few sites, which talks about 'Sessions' and 'Cookies'. It looks greek and latin to me.

It would be great if anyone can tell me

1) the changes that are required in this script
2) In Python how can i validate a page on Submit button instead of <Form> (any onsubmit property?)
3) Do I require to use Sessions and cookies.

Help!
Thanks..

Is This A Good Question/Topic? 0
  • +

Page 1 of 1