3

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!

Evan
  • 1,892
  • 2
  • 19
  • 40
  • Are you passing the 'content' variable to 'text' variable? – Alexander Santos Jul 18 '19 at 18:24
  • No, how do I do that? – Evan Jul 18 '19 at 18:24
  • 1
    On the render template method, you passed 'content=content'. You should pass something like 'text=content' – Alexander Santos Jul 18 '19 at 18:25
  • hmm okay that makes sense, I tried changing the line to: return render_template("book.html", text=content) but now I just get the following output, no matter what file I put in: b'' - like instead of printing out the .txt file it just prints out b'' – Evan Jul 18 '19 at 18:36

1 Answers1

1

I think your question should be "How to display a .txt file content inside an html pre tag with Flask" instead of yours, cuz It is little different meaning Although just watching your code, You only have to send the correct context keyword to the template, like @alexander-santos said too:

return render_template("book.html", text=content)
xtornasol512
  • 2,399
  • 1
  • 12
  • 7
  • 1
    I tried this, and though it definitely is a step in the right direction, for some reason when I do this, I get the following output: b'', no matter what the text file is. I tried modifying the book.html file to print something, to make sure it was working, and it did print it. But, for some reason, the line `{{ text }}` doesn't seem to be working properly. Any suggestions? – Evan Jul 18 '19 at 18:52
  • Might be that your file is bad/corrupted, or your operating system has permissions on you media folder, check this out, and tell us what it is wrong! Although It is important to save a file in binary mode, with encoding utf-8. All those details are important in order to render well a document txt – xtornasol512 Dec 19 '19 at 20:15