【发布时间】:2022-01-08 06:46:58
【问题描述】:
我一直在开发一个烧瓶中的程序,该程序允许您搜索数据库。我没有对数据库中的实际查找内容或任何内容进行编码,但我现在不需要,并且数据库现在不会影响任何内容。我一直无法理解如何将用户在表单中输入的内容传递给 python 程序。它运行没有错误,但是当我检查收到的内容时,我得到了无。有什么我做错了吗?这是我的代码,很乱,只有一个文件。
main.py
from flask import Flask, render_template
from flask import request
import pdfkit, time
def go(letters):
data = open('data.txt','r')
return letters
app = Flask(__name__)
@app.route("/path/", methods=['GET','POST'])
def search():
time.sleep(1)
data=request.get_data()
print(data)
return go(data)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)
app.run(debug=True)
模板/index.html
<!Doctype html!>
<html land=en>
<h1>Welcome!</h1><br>
<p>Type to search the database.</p>
<br><form name='this' onsubmit='letsgo()' class='text' action='/path/' method='post'><input id='hey' type='text'> <input type='submit' value='search'></form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var test = document.getElementById('hey').value;
const xhr = new XMLHttpRequest();
function letsgo() {
const data = document.getElementById("hey").value
alert(data)
$.ajax({
type : 'POST',
url : "{{'https://file-encrypter.ashwinchera.repl.co/path/'}}",
dataType: 'data',
datas : {'data':data}
});
};
</script>
我也在和一个朋友一起工作,所以我不知道这些东西是用来做什么的。有人可以告诉我如何发送这些数据吗?我一直在尝试其他问题,但它们不起作用。 提前谢谢!
【问题讨论】:
-
当前代码到底有什么问题?您是否遇到错误,或者“不工作”究竟意味着什么?
-
@ggorlen,我没有收到错误,但是当我检查什么是字母时,(我收到的)我没有。
-
您在不使用
render_template的情况下将字符串视为模板。这不会很好地结束——如果你打开 JS 控制台,你应该会看到错误。 JS 中的alert(url_for(view./path/))不使用 Jinja 模板注入 curlies,因此尝试在浏览器中将 Python 代码作为 JS 运行肯定会出错。 -
另外,你确定jQuery已经导入到前端了吗?如果是这样,请显示在哪里。
const xhr = new XMLHttpRequest();被创建并且从未使用过,所以代码闻起来就像是在没有太多理解或处理的情况下拼凑在一起的,如果你能原谅我的坦率。 -
我对烧瓶很新,(我两天前开始使用),每次我尝试 render_template 时都会出现错误
jinja2.exceptions.TemplateNotFound: index.html,之前有很多错误。而另一种方式有效,所以我一直在使用它。
标签: javascript python html flask messaging