我在一个页面(index.html)上工作,访问者可以在其中输入一些文本并提交消息。此消息将写入文件(messages.html)。 在同一页面(index.html)上,我想显示messages.html的内容。
其中大部分已经有效。问题是,我找不到用html包装邮件的方法,因此浏览器会正确显示内容。
有什么建议吗?
这是我的python文件(index.py):
from flask import Flask, request, url_for, redirect, render_template
from flask_wtf import FlaskForm
from wtforms import TextAreaField
from wtforms.validators import DataRequired
from flask import Response
import datetime
app = Flask(__name__)
app.config.from_object('config')
class MyForm(FlaskForm):
name = TextAreaField('name', validators=[DataRequired()])
@app.route('/')
def index():
form = MyForm()
with app.open_resource('messages.html') as g:
content = g.read()
return render_template('index.html',
content = content,
form = form)
@app.route('/p1')
def p1():
return render_template('p1.html')
@app.route('/p2')
def p2():
return render_template('p2.html')
@app.route('/entry', methods=['GET', 'POST'])
def submitmsg():
if request.method=='POST':
t = datetime.datetime.now()
strT = t.strftime('%Y-%m-%d')
with open("messages.html","a") as f:
f.write("Bericht op: " + strT + "<br>")
f.write(request.form["name"] + "<br>")
f.write("============================================================================<br>")
f.close()
form = MyForm()
with app.open_resource('messages.html') as g:
content = g.read()
return render_template('index.html',
content = content,
form = form)
if __name__ == "__main__":
app.run(host='0.0.0.0',debug=True)
以下是模板:
<!doctype html>
<html>
<body>
<table width="100%" border="1">
<tr align="center">
<td>
<a href="{{ url_for('p1') }}">Foto's</a>
</td>
<td>
<h1>berichten</h1>
</td>
<td>
<a href="{{ url_for('p2') }}">Webcam</a>
</td>
</tr>
</table>
<table width="100%" border="1">
<tr>
<td width="50%"><h1>berichten lezen</h1></td>
<td align="right"><h1>bericht schrijven</h1></td>
</tr>
<tr>
<td width="50%">{{ content }}</td>
<td align="right">
<form action="/entry" method="POST">
Schrijf hier je bericht:<br>
{{ form.name(rows='20',cols='60') }}
<br>
<input type="submit" value="Bericht versturen">
</form>
</td>
</tr>
</table>
</body>
</html>