Reputation: 89
I have this function:
def run():
if request.method == 'POST':
print(request.form['id_num'])
# fix later
else:
return render_template("apprun.html")
Right now it is always directing to the else statement, but the render_template function is not working correctly. Instead of a fully formatted HTML page, I'm getting the code as if it were a string. Specifically, my webpage is showing the html, instead of reading the html and displaying it properly:
<!DOCTYPE html>
<html>
<head>
<title>Rips Lab</title>
</head>
<body>
<style type="text/css">
html {
background-color: #E4E0EE;
}
body {
font-family: "Helvetica","Arial";
font-size: 20px;
color: #000000;
}
</style>
<form>Enter participant ID number: <input type="number" name="id_num" pattern="^[0-9]*$" required></form>
<br><br>
<p name="data"></p>
</body>
</html>
The folder hierarchy is correct; I have a folder called "templates" stored in the same place as the python file I'm running.
Any idea why it's not formatted correctly?
Upvotes: 0
Views: 5420
Reputation: 1
def run():
if request.method == 'POST':
print(request.form['id_num'])
# fix later
else:
response = make_response(html_content)
response.headers["Content-Type"] = "text/html"
return render_template("apprun.html")
Change the content-type in headers to text/html when returning response and browser will render it as html then.
Upvotes: 0
Reputation: 21
from flask import make_response,render_template
def run():
if request.method == 'POST':
print(request.form['id_num'])
# fix later
else:
return make_response(render_template("apprun.html"))
Upvotes: 1
Reputation: 12322
I was just having this issue while using Flask-RESTPlus. It turned out to be happening because I had the route defined as part of my API, which was setting the content-type
header to application/json
instead of HTML.
Upvotes: 1
Reputation: 89
The error was because at some point, I had opened the file in my computer's default text editor, which I guess corrupted it somehow. I'm not really sure why, because I've opened .py files in it before with no issue.
I fixed the issue by creating a new file and editing it with emacs, and just retyping it out (it wasn't that large)
Upvotes: 0