Answer the question
In order to leave comments, you need to log in
How to read data from a file in Flask?
Task: Book website in Flask. The chapters are loaded in the same html template. Chapters are text files with titles. ch1.txt , ch2.txt , etc.
Chapter page template - ch.html
Task. So that when requesting, say, site_name / ch3.html, the chapter from the ch3.txt file is loaded with template parameters (background color, formatting, etc.) ch.html
No matter how I fight, I can’t find a solution to such a simple task anywhere.
Thanks in advance for your reply!
Answer the question
In order to leave comments, you need to log in
I don’t know what doesn’t work there - everything seems to be simple, even the dumbest schoolboy could do:
@app.route('/<str:name>.html')
def Loader(name):
if os.path.isfile(name+".txt"):
text = open(name+".txt","rb").read()
else:
text = "не найдено"
return render_template('ch.html', text=text)
<pre>{{ text }}</pre>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question