templeate not found in flask - python

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

Related

Template error while using Flask with blueprints

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

Python: js code not updating in flask

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!

flask html js css img reference 404 error

I have a large website with hundreds of href=".html, href="css/, href="img/ and src="js/ references throughout the various html pages. I've placed all the css, js, img, sound, etc... folders in the "static" folder and all the html files in the "template folder".
I do not want to go through all my html files and manually edit each href using Flask's "url_for" function.
Is there a way that I can leave my existing html files alone and tell flask to use the css/, js/, and image paths defined in these html files?
Right now the following code leads to a bunch of 404 errors
import os
from flask import Flask, render_template
PROJECT_PATH = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__,
template_folder=os.path.join(PROJECT_PATH, 'templates'),
static_folder=os.path.join(PROJECT_PATH, 'static')
)
#app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
Okay, based on Martijn's script, I got my site running using the following code. However, to catch my many href=someHTMLfile.html links in my index.html file, I added #app.route('/'). I'm wondering if there is a better way to do this?
from flask import Flask, render_template, safe_join, send_from_directory
app = Flask(__name__)
#app.route('/<any(css, js, img, fonts, sound):folder>/<path:filename>')
def toplevel_static(folder, filename):
filename = safe_join(folder, filename)
cache_timeout = app.get_send_file_max_age(filename)
return send_from_directory(app.static_folder, filename, cache_timeout=cache_timeout)
#app.route('/<path:filename>')
def public(filename):
return render_template(filename)
# #app.route('/')
# def index():
# return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
You can register additional routes to serve files from your static folder:
from flask import app, safe_join, send_from_directory
#app.route('/<any(css, img, js, sound):folder>/<path:filename>')
def toplevel_static(folder, filename):
filename = safe_join(folder, filename)
cache_timeout = app.get_send_file_max_age(filename)
return send_from_directory(app.static_folder, filename,
cache_timeout=cache_timeout)
#app.route('/<path:htmlfile>.html')
def toplevel_static(htmlfile):
filename = htmlfile + '.html'
cache_timeout = app.get_send_file_max_age(filename)
return send_from_directory(app.template_folder, filename,
cache_timeout=cache_timeout)
This replicates what the static view does, but for routes starting with /css/, /img/, /js/ or /sound/ instead. HTML files are loaded from your template folder instead.

How to open html containing framset using flask framework

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

Flask: why having path converter in root route doesn't work?

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

Categories

Resources