Why is my jar application returning blank pages

I am new to Python web frameworks and am trying to learn Flask. I went through a textbook and everything was fine. Now I am trying to make my own application to study the structure. I had the following code in the main.py file

@app.route('/') def index(): return render_template('main.html') 

In main.html I have this html

 {% extends "layout.html" %} {% block content %} <div> Foo bar test</div> {% endblock %} 

and then in layout.html I have a basic web layout that looks like

 <!DOCTYPE html> <title>Flaskpad</title> <link href="/static/css/bootstrap.css" rel="stylesheet"> <link href="/static/css/flaskpad.css" rel="stylesheet"> <style type="text/css"> .socials { padding: 10px; } </style> </head> <body> <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <ul class="nav"> <li class="active"> <a class="brand" href="#">Flaskpad/a> </li> <li><a href="#">Login</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </div> </div> </div> <div class="container"> <div class="row fcontent"> <div class="span3"> Empty Space </div> <div class="span6 maincontent"> {% block content %}{% endblock %} </div> <div class="span3"> Empty Space </div> </div> <div class="row ffooter"> <div class="span12"> Made by Bar Foo </div> </div> </div> </body> 

I know that css links do not execute correctly, but I just put them there as placeholders. Now when I run python flask.py and switch to localhost: 5000, the page becomes blank and I cannot understand why. If I put plain text in main.html before the extension to be displayed, so I know that it loads main.html, but doesn't seem to extend the layout. The page is literally empty, because when viewing the source there is nothing. I can’t figure it out.

+4
source share
5 answers

I gave up, I could not get it to work. I created a new directory, copied and pasted the text into new files, and then ran it, and it worked successfully.

I do not know why this does not work in this case.

0
source

It works for me. Do you have your HTML files in a folder called templates relative to your python module?

Here is my complete test code.

 from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return render_template('main.html') if __name__ == '__main__': app.run() 

Change You said: β€œI had the following code in my main flask.py file” - You probably shouldn't call it flask.py, it could be due to an error.

+2
source

Which web browser do you use for testing?

I tried my code and it worked for me using chrome and firefox, but I noticed that you have an incorrectly closed anchor tag in layout.html in the line

 <a class="brand" href="#">Flaskpad/a> 

Maybe this makes the browser show the page blank?

0
source

Are you missing the opening <head> tag in layout.html? I do not see anything.

0
source

The HTML file should be in the templates folder. Perhaps this was a problem? In addition, EVERY function with @app.route should return render_template("the html file to render",**the_data_to_fill_the_html_file_with) , where the_data_to_fill_the_html_file_with looks something like this: {'a_template_variable':the_value_to_fill_it_with}

0
source

All Articles