How to redirect a page to another page in python 3 cgi

怎甘沉沦 提交于 2019-12-11 06:44:37

问题


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:


#! /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



来源:https://stackoverflow.com/questions/45958834/how-to-redirect-a-page-to-another-page-in-python-3-cgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!