簡體   English   中英

瀏覽器顯示 Python 代碼而不是執行它

[英]Browser displaying Python code rather than executing it

我已經使用 python 構建了一個數獨求解器,今天我設置了一個 web 頁面,供用戶將數字輸入到看起來像數獨的網格中,然后單擊求解。 這是通過表單完成的,“解決”是提交按鈕。 因為數獨頁面很長,而且問題不是特定於頁面,所以我在下面放了一些簡化的 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 以便進行操作。 在 web 上,我找到了使用 CGI 和 Flask 執行此操作的示例,但兩次我都遇到了同樣的問題。

改編自將 html 表單值發布到 python 腳本的 CGI 代碼:

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

Adapted Flask Code from 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 代碼的頁面,而不執行任何代碼(或者看起來如此)。 形式不應該是問題; 我通過簡單地將方法屬性切換為“get”並檢查我被重定向的頁面的 url 來驗證它正在輸出數據。 所以問題一定出在瀏覽器上,或者可能出在 python 代碼中? 我在 CGI 或 Flask 方面都不是很有經驗,事實上我是一般編碼的初學者,但考慮到互聯網上用於將 html 表單數據發送到 Z23EEEB4347BDD26BFC6B7EE9A3B5 的類似解決方案的數量是一件很簡單的事情。

As an added on question, where is the output of the python code meant to go if I didn't yet place it in another html page?

提前感謝您的幫助。

我真的很想幫助你,你似乎犯了一些初學者的錯誤,所以希望這個答案有一些用處:

對於處理表單提交的代碼,您可能希望創建一個單一視圖 function,這取決於 HTTP 請求方法,要么呈現表單,要么返回結果。

應用程序.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/子目錄中:

模板/索引.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">

@v25 這里是與我遇到的問題相關的屏幕截圖。

1.嘗試在flask上運行應用程序

燒瓶錯誤 --> 內部服務器錯誤。服務器遇到內部錯誤,無法完成您的請求。服務器過載或應用程序出錯。

2. 從 Test.html 表單運行應用程序

顯示 HTML if 語句。執行表單代碼

3. 提交表格

重定向到我的存檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM