浏览器显示Python代码而不执行代码

时间:2020-05-01 21:37:27

标签: python html forms browser

我已经使用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来执行此操作的示例,但是两次我都遇到了相同的问题。

来自Posting html form values to python script的自适应CG​​I代码:

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

来自https://opentechschool.github.io/python-flask/core/form-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代码而不执行任何代码(或似乎没有执行任何代码)。表格不应该是问题;我通过短暂地将method属性切换为“ get”并检查重定向到的页面的URL来验证它正在输出数据。所以问题一定在浏览器中,或者可能在python代码中?我在CGI或Flask方面都不是很有经验,事实上,我通常是编码的初学者,但是鉴于互联网上用于将html表单数据发送到python文件的类似解决方案的数量,我假设这是为了相当简单。

作为一个附加的问题,如果我还没有将python代码的输出放在另一个html页面中,那该去哪里呢?

在此先感谢您的帮助。

2 个答案:

答案 0 :(得分: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/子目录中:

templates / index.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">

答案 1 :(得分:0)

@ v25是与我遇到的问题相关的屏幕截图。

1。尝试在烧瓶上运行该应用程序

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。提交表单

Redirection to my archive