【问题标题】:How to redirect a page to another page in Python 3 cgi如何在 Python 3 cgi 中将页面重定向到另一个页面
【发布时间】:2018-02-08 01:47:41
【问题描述】:

我最近正在学习 Python 并且是初学者。我无法将逻辑设置为,如果帖子数据不符合条件,则帖子页面将被重定向到原始页面。

经过一番研究,我发现了这个how to redirect one page to another,但仍然对重定向页面感到困惑。

这是我的 html 代码:

<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> 这是我的python代码:

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

在这里,表头代码和所有人员在哪里放置以及如何从帖子页面重定向到原始页面,如何设置所有内容?请帮助我了解流程是如何进行的?

【问题讨论】:

  • 当您提供如此有限的细节时,很难不投反对票。为什么只显示html?您提到的所有内容都发生在 Python 代码中。
  • 等一下,我给你py代码
  • wrt 到您的“如何将一个页面重定向到另一个”链接:它与您的问题几乎没有关系(您链接到的问题是关于 HTTP 客户端代码,而不是关于 HTTP 服务器代码你在做什么)。此外,如果您希望进行任何 Web 编程,则需要了解 HTTP 协议(​​请求和响应部分)。
  • @burno desthuilliers ,这真的需要了解将表单发布数据发送到 CGI py 页面的 HTTP 协议吗?
  • 请任何人,需要一点帮助。

标签: python html cgi forms http-post


【解决方案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)

我做了很多研发,终于找到了这个,这将是迄今为止完美的解决方案。如果你觉得不合适,请纠正它。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2014-09-29
    • 2014-08-10
    相关资源
    最近更新 更多