嗨,我是Pythonanywhere的新手,我正在尝试将所有带有特定单词的行打印到a中,我很幸运能够打印完整的文档,但是我无法通过缝合来实现这一点。 / p>
目标是在输入表单中键入特定单词,然后在我的txt文件中搜索特定单词。在找到包含该单词的所有行后,将其发布到textarea。
Python代码:
from flask import *
import os, sys, codecs
app = Flask(__name__)
reload(sys)
sys.setdefaultencoding('latin-1')
@app.route('/search')
def display_search():
strWelcome = "Welcome to the website!"
return render_template("search.html", welcome=strWelcome)
@app.route('/searching', methods=['GET','POST'])
def display_searching():
if request.method == "POST":
search = request.form['txt_Search']
text_file = open(os.path.join(app.root_path, "data/logTest.txt"), "r")
# text_file = codecs.open(os.path.join(app.root_path, "data/logTest.txt"), "r", "latin-1", "ignore")
all_lines = text_file.readlines()
text_file.close()
for line in all_lines:
if search in line:
log = line
else:
print "Sorry"
return render_template("search.html", projTitle=strProjectHeading, pressedSubmit="True", all_lines=all_lines, log=log, search=search)
else:
return render_template("search.html", projTitle=strProjectHeading)
HTML code:
<label>Search for a string: </label><input name="txt_Search" type="text" id="txt_Search" title="Search"><input name="btn_Search" type="button" id="btn_Search" title="Search" value="Search" onclick="window.location.href='http://MySite.pythonanywhere.com/searching'" \>
<textarea rows="27" cols="119" name="description">{% for eachLine in log %}{{ eachLine }}{% endfor %}</textarea>
页面正在加载,我在错误日志中看不到任何错误。
答案 0 :(得分:0)
您的模板中没有任何地方可以将搜索结果放在页面中。