Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i make login in python useing html?

Status
Not open for further replies.

miona

Programmer
Jan 9, 2005
1
DK
Hello people
I'm a new python user and i don't know much about it.
The thing I have to do is make a simple log in with one valid username and password. When those two are done, the submit button should link this file to the next one. Don't know how to do that :(


my file looks like this so far:

#<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

print 'Content-type: text/html\n\n'


print '''<html>

<head>

<title>private bibliotek</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>



<body bgcolor="#0066CC">

<p>&nbsp;</p><table width="46%" height="66" border="0" align="center">

<tr>

<td><h1><strong>PRIVAT BIBLIOTEK</strong></h1></td>

</tr>

</table>

<table width="49%" border="0" align="center">

<tr>

<td width="41%"><font color="#000000"><strong>User:</strong> </font> <input type="text" name="Name" size="10" maxlength="40">

</td>

<td width="59%"><strong><font color="#000000">Password:</font></strong> <input type="password" name="password" size="10" maxlength="10">

</td>

</tr>

</table>

<table width="12%" height="75" border="0" align="center">

<tr>

<td height="45"><font color="#000000"><em>

<input type="submit" name="Submit" value="Log in here">

</em></font></td>

</tr>

<tr>

<td height="24"><font color="#000000"><em><u>Ny bruger </u></em></font></td>

</tr>

'''

import cgi



form = cgi.FieldStorage()



username = form.getvalue('Name','')

password = form.getvalue('password','')

if username == 'user' and password =='1234':
## here i have to add that on click to submit button -> open next page
## otherwise return alert box




print '''
</table>
</body>

</html> '''

Thank you for your help :)
 
I don't know which OS you are working with. This is how it works with Windows and IIS. I am assuming that Linux/Apache do something similar. If you are using Apache the substitute the /scripts/ for /cgi-bin/.

Firstly, you need to create two files. The first with the HTML content in your web server root, either or wherever. I notice that you are lacking a FORM element in your code. You need a FORM element to let the input boxes know where to send the information, i.e

<form method='post' action='/scripts/somescript.py'>
<table><input /><input /></table>
</form>

Secondly, create the /somescript.py file and put it into your /scripts/ or /cgi-bin/ folder.

The following is probably ok

import cgi

#bring the form data in as dictionary - easier to work with

f = cgi.InterpFormContentDict()

if f['user'] == 'user' and f['password'] == '1234':
print "Content-Type: text/plain"
print
print "<script language='Javascript' type='text/javascript'>"
print "window.location='" + newfile + "'"
print "</script>"
else:
#put your alert screen here

Save it then try and run it.

Provided you set up the input tags in the html ok it should give you an output. You may need to play with the if statement a bit - I haven't tried it, just based it on something that I have already done that works fine. If it doesn't work, comment out the if statement and use

print 'Content-Type: text/plain'
print f

This will print the contents of the dictionary to your browser and help you identify the keys to the dictionary. or split the if at the 'and' and use two separate lines to check this.

I hope that this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top