How to redirect a page to another page in python 3

2019-07-22 14:36发布

Hello I am studying python recently and a newbie ,I am unable set a logic to as, if the post data does not qualify,post page will be redirected to origin page. After a long time search I found this how to redirect one page to another ,But still having confusion regarding redirecting page. here is my html code :

<form action="generate_timing_data.py" method="POST"> <p>Select an athlete from the list to work with: </p><input type="radio" name="which_athlete" value="Mikey McManus"> Mikey McManus<br /> <input type="radio" name="which_athlete" value="Julie Jones"> Julie Jones<br /> <input type="radio" name="which_athlete" value="James Lee"> James Lee<br /><input type="radio" name="which_athlete" value="Sarah Sweeney"> Sarah Sweeney<br /><p> </p><input type=submit value="Select"> </form> And this is my python code :

import cgi
import athletemodel
import yate
form_data = cgi.FieldStorage()
if form_data['which_athlete'].value != '':
   //calculation stuff
else :
   //redirect stuff

In here, where to put header code and all the staff in form and how to redirect to origin page from the post page, how to set all the things? please help me to understand how the process flow?

And a kind request please don't downvote,I am newer in python.

1条回答
We Are One
2楼-- · 2019-07-22 15:07
#! /usr/bin/python3
import cgi
form_data=cgi.FieldStorage()
print('Content-type:text/html\n\n')
if 'username' not in form_data or 'password' not in form_data:
   redirectURL = "http://localhost:8081/"
   print('<html>')
   print('  <head>')
   print('    <meta http-equiv="refresh" content="0;url='+str(redirectURL)+'" />') 
   print('  </head>')
   print('</html>')
else:
   print(form_data['username'].value)
   print(form_data['password'].value)

I did lot of R&D and finally found this and would be the perfect solution so far.If you feel not appropriate ,please correct it.Thank you

查看更多
登录 后发表回答