I want to open index.html,
But can not find pic.html
How could i do
pic.html and index.html are all in the templates
index.html
<body>
<iframe src="pic.html"></iframe>
</body>
flaskdemo.py
from flask import Flask,render_template
app = Flask(__name__)
#app.route('/')
def hello_world():
return render_template("index.html")
if __name__ == '__main__':
app.run()
Flask doesn't serve files from its templates directory. You would need to set up a route for /pic.html and render the appropriate template as part of that:
from flask import Flask,render_template
app = Flask(__name__)
#app.route('/')
def hello_world():
return render_template("index.html")
#app.route('/pic.html')
def pic():
return render_template("pic.html")
if __name__ == '__main__':
app.run()
Related
I am getting a template not found error while trying to run a flask app using blueprints. The template directory is located at the root directory as expected at the same level as the app directory. I am not very sure why this is happening.
The directory structure
root/
app/
blueprint1/
routes.py
__main__.py
templates/
base.html
index.html
routes.py
from flask import Blueprint, render_template
blueprint = Blueprint(
"blueprint", __name__, template_folder="templates")
#blueprint.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
Error
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html
main.py
from flask import Flask
from app.blueprint1.routes import blueprint
def create_app():
app = Flask(__name__)
app.register_blueprint(blueprint)
return app
def _main():
daemon_app = create_app()
daemon_app.run(debug=True)
if __name__ == "__main__":
_main()
This works, I edited it a little
routes.py
from flask import Blueprint, render_template
blueprint = Blueprint("blueprint", __name__, template_folder="../../templates")
#blueprint.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html")
__main__.py
from flask import Flask
from routes import blueprint
def create_app():
app = Flask(__name__)
app.register_blueprint(blueprint)
return app
def _main():
daemon_app = create_app()
daemon_app.run(debug=True)
if __name__ == "__main__":
_main()`
Based on your file structure, your blueprint renders html files from the main template folder. It might work if you remove the template_folder argument from routes.py file.
I copied your codes and tried to emulate the error but it works for me. A year ago I have the same problem and somehow I solved it. Since I can't emulate the error I can't solve your problem.
Instead, I will just give you advise. You are making an effort to create blueprints in their own package, and yet you are using the main template folder. When you have a lot of blueprints managing the template folder will become a challenge. Thus, I recommend putting the html file within the blueprint package itself.
Here's the file structure:
root/
app/
blueprint1/
pages/blueprint1/index.html
routes.py
__main__.py
templates/
base.html
and routes.py should be like this
blueprint = Blueprint(
"blueprint", __name__, template_folder="pages")
#blueprint.route("/", methods=["GET", "POST"])
def index():
return render_template("blueprint1/index.html")
How do I run flask app directly to 0.0.0.0/input page instead of "/"?
Is this possible?
Otherwise, what is best way to exclude "/" from showing?
#app.route("/")
def home():
return 200
if __name__ == '__main__':
app.run(host='0.0.0.0/input', port=int(os.getenv("FLASK_PORT", "8100")), debug=True)
Redirect your root url:
from flask import Flask, redirect, url_for
app = Flask(__name__)
#app.route("/")
def home():
return redirect(url_for("hello"))
#app.route("/hello")
def hello():
return "<p>Hello</p>"
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
from flask import Flask,render_template
app = Flask(__name__, template_folder='home/root13/webapp.html/staic/')
#app.route("/index.html")
def hello():
return render_template('index.html')
#app.route("/home")
def hello_world():
return render_template('post.html')
if __name__ == "__main__":
app.run(debug=True)
Flask will try to find the HTML file in the templates folder, in the same folder in which this script is present.
-Application folder
-Hello.py
-templates
-hello.html
You should put all HTML files inside a folder named templates
I am refreshing my browser...after running but,
changes are not affected in browser
from flask import Flask, send_file, render_template, send_file
app = Flask(__name__)
#app.route('/')
def hello_world():
return send_file('templates/create_conf_view.html')
if __name__ == '__main__':
app.run(debug=True, use_reloader=True, host='0.0.0.0', port=1672,threaded=True)
What i am doing wrong ?
According to your code you need to have a directory structure as follows:
app
|_
run.py
templates/
|_create_conf_view.html
According to docs you should not pass templates when rendering a template.
Change your original file to and see if it works:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def hello_world():
return render_template('create_conf_view.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=1672, thread=True)
Also be sure to access your application at specified port!
from flask import Flask, render_template
app = Flask(__name__, static_url_path='')
#app.route('/')
def index():
return render_template('index.html')
#app.route('/page/<path:page>')
def article(page):
return render_template('page.html')
if __name__ == "__main__":
app.run()
Work just fine. But if i change the second route to #app.route('/<path:page>'), then any access to URL like /path/to/page yields 404.
Why doesn't #app.route('/<path:page>') work?
Related questions, that don't answer the question however:
Flask: Handle catch all url different if path is directory or file
Custom routing in Flask app
Capture arbitrary path in Flask route
static_url_path conflicts with routing. Flask thinks that path after / is reserved for static files, therefore path converter didn't work. See: URL routing conflicts for static files in Flask dev server
works flawless for me:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/page/<path:page>')
def article(page):
return render_template('page.html')
if __name__ == "__main__":
app.debug = True
app.run()
I can access: http://localhost/ -> index and http://localhost/page/<any index/path e.g.: 1> -> article