0

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?

Josh K
  • 79
  • 1
  • 12
  • 2
    I am not sure if I get your question right. Do you mean you see this output as is in you browser? If yes can you show the HTTP headers that go with the response to the browser? – webwurst Mar 03 '19 at 22:47
  • @webwurst sorry for the confusion: yes, I see the code as output in my browser. Also, I’ve tried setting the headers manually as seen here: https://stackoverflow.com/a/19316089 but it still doesn’t work – Josh K Mar 03 '19 at 23:12
  • Can you check with some debug/log statement that the "else" part is really executed? Just to make sure there is not another way your template is handed out. – webwurst Mar 03 '19 at 23:21
  • @webwurst tried it, still not working correctly – Josh K Mar 04 '19 at 00:38

4 Answers4

1

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.

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
1

Try below changes, it will work

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"))

Prakash
  • 11
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '23 at 11:19
0

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)

Josh K
  • 79
  • 1
  • 12
0
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.