浏览器显示Python代码,而不是执行它。

问题描述 投票:1回答:1

我用python建立了一个数独解算器,今天我设置了一个网页,让用户把数字输入到一个看起来像数独的网格中,然后点击解算。这是用一个表单完成的,"解 "是提交按钮。因为数独页面比较长,而且问题也不具体,所以我在下面放了一些简化的html表单代码作为测试。

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
    <form id="sudoku" action="Test.py" method="post">
      <label for="num-input"> Enter a Number: </label>
        <input type="number" name="num-input">
      <label for="num-input"> Enter another Number: </label>
        <input type="number" name="num-input">
      <label for="num-input"> Enter a third Number: </label>
        <input type="number" name="num-input">
      <button type="submit" name="submit" form="sudoku"> Submit </button>
    </form>
  </body>
</html>

当表单提交后,应该将其发送到Test.py中,以便进行操作。在网上我找到了使用CGI和Flask的例子,但两次都遇到了同样的问题。

改编自CGI代码 将html表单值发布到python脚本中:

import cgi
form = cgi.FieldStorage()
numbers =  form.getvalue('num-input')

改编自Flask代码 https:/opentechschool.github.iopython-flaskcoreform-submission.html。:

from flask import request, redirect 

@app.route('/Test.py', methods = ['POST'])
    def signup():
        nums = request.form['num-input']
        print(nums)
        return redirect('/index.html')

我已经尝试了两种解决方案和许多其他的测试表格,但我一直有一个反复出现的问题。当我点击提交时,浏览器将我重定向到一个页面,在那里显示原始的Python代码,但没有执行任何代码(或者看起来是这样)。表单不应该有问题;我通过简单地将方法属性切换为 "get "并检查我被重定向的页面的url来验证它是否在输出数据。所以问题一定是出在浏览器上,或者是出在python代码上?我在CGI和Flask方面都不是很有经验,事实上,我在编码方面也是个初学者,但考虑到互联网上有很多类似的将html表格数据发送到python文件的解决方案,我认为这应该是个很简单的问题。

另外,如果我还没有把python代码的输出放在另一个html页面中,那么它的输出应该放在哪里呢?

先谢谢你的帮助。

python html forms browser
1个回答
0
投票

我真的很想帮助你,你似乎犯了一些初学者的错误,所以希望这个回答能对你有所帮助。

对于处理表单提交的代码,你可能想创建一个单一的视图函数,根据HTTP请求方法,要么渲染表单,要么返回结果。

app.py

from flask import Flask, request, render_template 
app = Flask(__name__)

@app.route('/', methods = ['GET','POST'])
def index():
    if request.method == 'POST':
        num_input = request.form['num-input']
        return render_template('index.html', num = num_input)

    elif request.method == 'GET':
        return render_template('index.html')
  • 如果这段代码接收到一个GET请求(你点击了 / 端点在浏览器中),那么 index.html 模板被渲染。
  • 如果这段代码接收到一个POST请求(表单已提交),那么这个 num_input 变量,然后再传回模板作为 num.

现在,对于一个模板来说,根据是否是 num 参数被传递给 render_template 将显示该数字,或者显示HTML表格。

一定要把这个放在 templates/ 子目录。

templatesindex.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
  {% if num %}
    The number you entered was: {{num}}
  {% else %}
    <form action="/" method="POST">
      <label for="num-input"> Enter a Number: </label>
        <input type="number" name="num-input" />
      <input type="submit" value="Submit"> 
    </form>
  {% endif %}
  </body>
</html>

在开发服务器上运行这个 flask run然后你就可以看到开发服务器启动了。

 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

在浏览器中打开该URL,应该会出现一个输入框,然后会显示提交。

The number you entered was: 42 

要扩展这个功能,你可以在表单中添加更多的输入字段,只需确保每个字段都有一个独特的 "输入框"。name 属性。

<label for="num-input-two"> Enter another Number: </label>
<input type="number" name="num-input-two">

0
投票

@v25以下是我遇到的问题的相关截图。

1. 试图在flask上运行应用程序

Flask error --> Internal Server Error. The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

2. 从Test.html表格中运行应用程序

HTML if statement displayed. Form code is executed

3. 3. 提交表格

Redirection to my archive

© www.soinside.com 2019 - 2024. All rights reserved.