如何在Python 3 CGI中将页面重定向到另一个页面 [英] How to redirect a page to another page in Python 3 cgi

查看:111
本文介绍了如何在Python 3 CGI中将页面重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Python,并且是一个初学者。我无法设定逻辑,因为如果发布数据不符合条件,则发布页面将被重定向到原始页面。

I am studying Python recently and a beginner. I am unable set a logic to as, if the post data does not qualify, post page will be redirected to origin page.

经过研究,我发现此如何将一个页面重定向到另一个页面,但是在重定向页面方面仍然存在困惑。

After some research I found this how to redirect one page to another, but still having confusion regarding redirecting page.

这是我的html代码:

Here is my html code :

< form action = generate_timing_data.py method = POST>
< p>从列表中选择一个运动员与之合作:
< / p><输入type = radio name = which_athlete value = Mikey McManus> Mikey McManus< br />
< input type = radio name = which_athlete value = Julie Jones>朱莉琼斯< br />
< input type = radio name = which_athlete value = James Lee> James Lee< br /><输入type = radio name = which_athlete value = Sarah Sweeney>莎拉·斯威尼(Sarah Sweeney)< p>
< / p><输入类型=提交值=选择>
< / form>

这是我的python代码:

<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?

推荐答案

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

这篇关于如何在Python 3 CGI中将页面重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆