繁体   English   中英

如何在python中将值从一个HTML传递到另一个

[英]How can pass value from one HTML to another in python

有 2 个 html 文件 mypage.html 和 page.html ,想要将一些值从一个 html 传递到另一个 html 并在另一个 html 中如何获取这些值

正在使用的代码:

 with open('page.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<table>')
    myFile.write('<tr>')
    myFile.write('<td> Interface </td>')
    myFile.write('<td> Global </td>')
    myFile.write('</tr>')
    myFile.write('<tr>')

    myFile.write('<td> <a href="interface.html">Interface</a></td>')
    myFile.write('<td> <a href="global.html">Global</a></td>')
    myFile.write('</% print c /%>')
    #myFile.write('<var>params</var>')
    myFile.write('</tr>')
    myFile.write('</table>')
    myFile.write('</body>')
    myFile.write('</html>')

#render HTML page
with open('mypage.html', 'w') as myFile:
    myFile.write('<html>')
    myFile.write('<body>')
    myFile.write('<table>')
    myFile.write('<tr>')
    myFile.write('<td> Host Name </td>')
    myFile.write('<td> Result </td>')
    myFile.write('</tr>')
    myFile.write('<tr>')
    c = Cookie.SimpleCookie()
    c['mycookie'] = 'cookie_value'        
    myFile.write('<td> <a href="page.html?params="'+final_dict.keys()[0]+ '>'+final_dict.keys()[0]+'</a></td>')
    myFile.write('<td> Fail</td>')
    myFile.write('</tr>')
    myFile.write('</table>')
    myFile.write('</body>')
    myFile.write('</html>')

您有一个生成 html 的 python 脚本。 那没关系。

您正在将该 html 保存到一个文件中,然后呢? 提供文件? 我猜你生成了一次文件,然后你就有了静态文件。 当用户实际请求一个 url 时,不会执行任何 python 代码。 你应该彻底改变这个概念。

你想要发生的是:

  1. 用户在浏览器中输入一个 url
  2. 浏览器向服务器发送请求
  3. 服务器执行一些生成 html 的 python,但不将其保存到文件中。 它只是将其发送回用户
  4. 用户看到生成的 html

使用这种方法,在第 3 步中,每次都会执行 python 脚本,并且您有机会读取请求参数。

现在,要做到这一点,您需要一台服务器。

您可以采用绝对低级的方法,通过将 python 作为 CGI 脚本执行,配置 Apache、IIS 或其他任何东西来提供 .py 文件。 这与您目前正在做的非常相似。 如果你这样做,你会学到很多关于 HTTP 是如何工作的,但这是一种艰难的方式,并不是最佳的性能

更好的选择

你可以运行一个 python web 服务器,它调用 python 函数来为每个配置的 url 提供服务。 看看CherryPyFlask Flask 非常好,但 CherryPy 可能更适合初学者。

我建议你应该从CherryPy开始。 按照文档,寻找教程...

您可以使用 php 中的内置数组$_GET获取 URL 参数。

要从 URL 检索参数,您可以将此 PHP 代码插入网页的 html 源代码(您使用 URL 中的数据打开的那个):

<?php
 echo $_GET['key'] // Will print out the data of the given key
?>

当然,您可以使用 PHP 对这些数据进行任何操作。 这仅在您在启用了 PHP 的 Web 服务器上运行网页时才有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM