将输入从 html 传递到 python 并返回

新手上路,请多包涵

我需要为作业制作一个网页,它不必上传到网络,我只是使用本地 .html 文件。我做了一些阅读并提出了以下 html 和 python:

 <!DOCTYPE html>
<html>
    <head>
        <title>
            CV - Rogier
        </title>
    </head
    <body>
        <h3>
            Study
        </h3>
        <p>
            At my study we learn Python.<br>
            This is a sall example:<br>
            <form action="/cgi-bin/cvpython.py" method="get">
                First Name: <input type="text" name="first_name">  <br />
                Last Name: <input type="text" name="last_name" />
                <input type="submit" value="Submit" />
            </form>
        </p>
    </body>
</html>

Python:

 import cgi
import cgitb #found this but isn't used?

form = cgi.FieldStorage()

first_name = form.getvalue('first_name').capitalize()
last_name  = form.getvalue('last_name').capitalize()

print ("Content-type:text/html\r\n\r\n")
print ("<html>")
print ("<head>")
print ("<title>Hello - Second CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Your name is {}. {} {}</h2>".format(last_name, first_name, last_name))
print ("</body>")
print ("</html>")

然而,这只是将印刷品作为文本提供,而不是作为我想要的第 1 行的正确 html 文件。

原文由 RnRoger 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

您是否有正在运行的像 apache 安装程序这样的 Web 服务器?如果你不这样做,我不确定这是否有效,所以你可能想看看 Mamp 要允许它执行你的 python 脚本,你还需要编辑 httpd.conf 文件

从:

 <Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>

至:

   <Directory "/var/www/cgi-bin">
  Options +ExecCGI
  AddHandler cgi-script .cgi .py
  Order allow,vdeny
  Allow from all
  </Directory>

或者

如果您只是想在不设置服务器的情况下制作一个实际的 HTML 文件,那么一种非常基本但粗略的方法就是将所有内容简单地写入您创建的 HTML 文件中,例如:

 fo.write("Content-type:text/html\r\n\r\n")
fo.write("<html>")
fo.write("<head>")
fo.write("<title>Hello - Second CGI Program</title>")
fo.write("</head>")
fo.write("<body>")
fo.write("<h2>Your name is {}. {} {}</h2>".format("last_name", "first_name", "last_name"))

fo.write("</body>")
fo.write("</html>")

fo.close()

这将在与您的 python 项目相同的目录中创建一个名为 yourfile.html 的 HTML 文档。

我不建议这样做,但我意识到由于这是一项作业,您可能无法选择使用库。以防万一,更优雅的方法是使用 yattag 之类的东西,这将使它更易于维护。

从他们的网站复制 Hello World 示例。

 from yattag import Doc

doc, tag, text = Doc().tagtext()

with tag('h1'):
    text('Hello world!')

print(doc.getvalue())

更新:

如果您没有本地网络服务器设置,另一种选择是使用 Flask 作为您的网络服务器。您需要像这样构建项目:

     /yourapp
    basic_example.py
    /static/
        /test.css
    /templates/
        /test.html

Python:

 __author__ = 'kai'

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('test.html')

@app.route('/hello', methods=['POST'])
def hello():
    first_name = request.form['first_name']
    last_name = request.form['last_name']
    return 'Hello %s %s have fun learning python <br/> <a href="/">Back Home</a>' % (first_name, last_name)

if __name__ == '__main__':
    app.run(host = '0.0.0.0', port = 3000)

HTML:

 <!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="static/test.css">
        <title>
            CV - Rogier
        </title>
    </head>
    <body>
        <h3>
            Study
        </h3>
        <p>
            At my study we learn Python.<br>
            This is a sall example:<br>
            <form action="/hello" method="post">
                First Name: <input type="text" name="first_name">  <br />
                Last Name: <input type="text" name="last_name" />
                <input type="submit" name= "form" value="Submit" />
            </form>
        </p>
    </body>
</html>

CSS(如果你想设计你的表单样式?)

 p {
    font-family: verdana;
    font-size: 20px;
}
h2 {
    color: navy;
    margin-left: 20px;
    text-align: center;
}

根据你的问题 在这里 做了一个基本的例子希望这能帮助你走上正轨,祝你好运。

原文由 Kai 发布,翻译遵循 CC BY-SA 3.0 许可协议

首先检查浏览器 url,它看起来像 file:///C:/xampp/htdocs/cgi_python/form.html

这意味着您的代码是正确的,没有错误,但输出显示为 python 脚本代码,因为“代码在控制台而不是在浏览器中工作”

也就是说你直接打开 htdocs 文件夹中的文件并单击 filename.html 并执行然后以文本格式输出 For open a browser

解决方案是

  1. 打开浏览器输入url
  2. 本地主机/文件路径例如。 http://localhost/cgi_python/form.html 然后得到答案

原文由 Lalit Thaware 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题