So I'm trying to make a program using Flask that will allow users to upload a .txt file. The program will then print out this file. Using some tutorials I've found, I have the following code in a file called site.py:
from flask import Flask, render_template, request
from werkzeug import secure_filename
app = Flask(__name__)
@app.route('/')
def upload():
return render_template('upload.html')
@app.route('/uploader', methods = ['GET', 'POST'])
def uploader():
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
content = f.read()
return render_template("book.html", content=content)
if __name__ == '__main__':
app.run(debug = True)
Inside of my 'upload.html' file I have the following:
<html>
<body>
<form action = "http://localhost:5000/uploader" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>
and inside of my book.html file I have the following:
<pre>
{{ text }}
</pre>
The file uploader is definitely working - the files are being saved in the correct directory in my computer, but the text in the files isn't being displayed, namely the last two lines of my site.py:
content = f.read()
return render_template("book.html", content=content)
don't seem to be doing anything.
How can I fix this problem?
Thanks!