I'm working on a page (index.html) where visitors can enter some text and submit a message. This message is written to a file (messages.html). On the same page (index.html) I want to show the content of messages.html.
Most of this works already. Problem is, I can't find a way to wrap the messages in html, so the browser shows the content properly.
Any suggestions?
This is my python file (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)
And here's the template:
<!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>