[英]Flask extract variables from a function python
我使用 python 和 html 創建了一個帶有燒瓶的表單。
我想填寫一個表單,然后通過單擊提交按鈕保存這些值,以便將它們用於另一個項目。 但我也想創建另一個按鈕,當我點擊它時,它會自動用默認值填充表單(在我的情況下,我希望到處都是0
)。
這是我的 HTML 代碼form.html
:
<!DOCTYPE HTML>
<html>
<head>
<title>Sample page to test fill_web_form.py</title>
</head>
<body>
<p1><strong>SAMPLE PAGE TO TEST FILL_WEB_FORM.PY</strong></p1>
<!--test form-->
<form method="post" action="/">
Test the motor:<br>
<table>
<tr>
<td> Function number: </td>
<td> <input type="text" name="functionnumber"> </td>
</tr>
<tr>
<td> Angle: </td>
<td> <input type="text" name="angle"> </td>
</tr>
<tr>
<td> Speed: </td>
<td> <input type="text" name="speed"> </td>
</tr>
<tr>
<td> <input type="submit" name="btn" Value="Submit"> </td>
</tr>
<tr>
<td> <input type="submit" name="btn" Value="TURNON"> </td>
</tr>
</table>
</form>
而我的 python 代碼hello.py
:
# sends data from html form to database
from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)
@app.route('/')
def form():
return render_template('form.html')
@app.route('/', methods=['POST'])
def get_parameters():
if request.form["btn"] == "Submit":
form_params = []
form_params.append(request.form['functionnumber'])
form_params.append(request.form['angle'])
form_params.append(request.form['speed'])
else:
form_params = []
form_params.append('0')
form_params.append('0')
form_params.append('0')
return render_template('form.html'), str(form_params)
get_parameters()
print(get_parameters.form_params[0])
最后兩行是當我試圖從函數中提取變量時,因為我需要使用它們(我還沒有實現關於為什么我需要這些變量的代碼)。 我正在打印functionnumber
--> print(get_parameters.form_params[0])
,以查看它是否運行良好。
不幸的是,我收到以下錯誤:
Traceback (most recent call last):
File "C:\Users\Camille\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Camille\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\Camille\Documents\myproject\venv\Scripts\flask.exe\__main__.py", line 7, in <module>
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\cli.py", line 990, in main
cli.main(args=sys.argv[1:])
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\cli.py", line 596, in main
return super().main(*args, **kwargs)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\click\core.py", line 1062, in main
rv = self.invoke(ctx)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\click\core.py", line 1668, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\camille\documents\myproject\venv\lib\site-packages\click\core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\click\core.py", line 763, in invoke
return __callback(*args, **kwargs)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\click\decorators.py", line 84, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\click\core.py", line 763, in invoke
return __callback(*args, **kwargs)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\cli.py", line 845, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\cli.py", line 321, in __init__
self._load_unlocked()
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\cli.py", line 346, in _load_unlocked
self._app = rv = self.loader()
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\cli.py", line 402, in load_app
app = locate_app(self, import_name, name)
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\cli.py", line 256, in locate_app
__import__(module_name)
File "C:\Users\Camille\Documents\myproject\hello.py", line 25, in <module>
get_parameters()
File "C:\Users\Camille\Documents\myproject\hello.py", line 13, in get_parameters
if request.form["btn"] == "Submit":
File "c:\users\camille\documents\myproject\venv\lib\site-packages\werkzeug\local.py", line 422, in __get__
obj = instance._get_current_object()
File "c:\users\camille\documents\myproject\venv\lib\site-packages\werkzeug\local.py", line 544, in _get_current_object
return self.__local() # type: ignore
File "c:\users\camille\documents\myproject\venv\lib\site-packages\flask\globals.py", line 33, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
當我不添加最后兩行時,它可以工作,但仍然是一個問題,因為我無法使用和提取變量。 我想訪問函數外的變量form_params[0]
、 form_params[1]
、 form_params[2]
。
你的問題有兩部分:
<input type="text" name="speed" value="0">
我不確定您為什么希望它出現在單擊按鈕時。 但是如果你真的想要那樣,你需要添加一些 JavaScript 並執行它,如下所示:
HTML
<form id="form">
<input type="text" name="speed">
<input type="text" name="angle">
<form>
<button id="btn"> Add default values </button>
JS (使用 jQuery)
$("#btn").click(function () {
$("#form")
.find("input")
.each(function (i) {
$(this).attr("value", "0");
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.