Setting URLS for REST API using Flask [duplicate] - python

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 6 years ago.
I have two files - app.py which contains the code for a REST API and another page index.html which displays some contents of the API using AngularJS. Currently, I have to open the index.html file from the browser to get it working (after the server http://localhost:5000 is deployed).
I tried
#app.route('/')
def index():
return render_template('/index.html',
title='Home')
It is giving the error "TemplateNotFound: /index.html". Currently, app.py and index.html are in the same directory. How do I change the format to let Flask know that it has to render the page index.html on this URL.

In flask, templates should be located in a directory called templates
Your app should look like :
./app.py
./templates/index.html
http://flask.pocoo.org/docs/0.10/tutorial/templates/ :
Put the following templates into the templates folder

Try removing the slash from the template path. It is not necessary when the files are in the same directory.
return render_template('index.html',
title='Home')

Related

jinja2.exceptions.TemplateNotFound: index.html error [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 1 year ago.
I am trying to render the file index.html, but I keep getting
jinja2.exceptions.TemplateNotFound: index.html
when I try to render it. Why can't Flask find my template
app=Flask(__name__,template_folder='templates')
#app.route("/")
def index():
return render_template('index.html')
and my file location is like (i chance Tamplates to tamplates)
so you must define the template folder path like this.
if you named your template folder: Tamplates
Then use this
app = Flask(__name__, template_folder='Tamplates')

Integrating a web template with Flask [duplicate]

This question already has answers here:
How to serve static files in Flask
(24 answers)
Application not picking up .css file (flask/python) [duplicate]
(13 answers)
Closed 4 years ago.
I have downloaded a website template and I want to integrate it with flask. Basically, this template contains asset folder which contains the css and js files. I know that by using render_template('index.html'), I can call index.html on localhost. But when I do so, the webpage is not rendered properly. I think flask is not able to load the js, css, etc files in that folder (templates folder).
My template folder has index.html along with assets folder and othe folders. I am using the code shown below to actually run the index.html.
from flask import Flask, render_template
import os
app = Flask(__name__)
#app.route('/')
def homepage():
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True)
So, index.html is loading but it is not loading the css files, etc. Because if that is not loaded, my webpage looks dull. Its simply some text on white screen. I think the problem is with loading the static files because I get an error saying that there is no folder assets on localhost.
Thanks in advance.
You need to reference the static files inside your template like this:
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
And ensure you put the stylesheets in the static directory of your project. You can also configure which directory contains static assets with something like:
app = Flask(__name__, static_folder="static_dir")

Flask cannot find template file - jinja2.exceptions.TemplateNotFound [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(14 answers)
Closed 5 years ago.
I tried to render an "html" file using Flask(Jinja2) in Python, but it showed me an error.
My controller ".py file":-
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/profile/<name>")
def profile(name):
return render_template('profile.html', name=name)
if __name__ == "__main__":
app.run(debug=True)
My template "profile.html" file:
<h1>Welcome To Your Profile, {{ name }}</h1>
When I ran the flask app, it gave me the following exception:-
jinja2.exceptions.TemplateNotFound: template\profile.html
The error message says:
jinja2.exceptions.TemplateNotFound: template\profile.html
The templates folder should be named templates (in plural) and saved in the root path of the application. That is how Flask works by default.
Try this:
app = Flask(__name__, template_folder={path of template directory})

Flask can't find template when specifying templates folder [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(14 answers)
How do I create a link to another html page?
(2 answers)
Closed 4 years ago.
I created a template in the templates folder next to my Flask app. When I try to render this template, I get a 404 error in the browser. Why can't I render the template?
from flask import Flask, render_template
app = Flask(__name__, template_folder='/templates')
#app.route('/')
def index():
return render_template('index.html')
You don't need to specify a template folder for the app, it's automatically set to 'templates'. As that default shows, it's a relative path.
You've provided an absolute path though, /templates is "the templates folder at the root of the filesystem". Your machine is unlikely to have a templates folder with your templates at the root.
Remove the template_folder parameter, or remove the leading /.

Flask Template Not found [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 7 years ago.
Implementing a simple static site from flask, but the browser says template not found, the shell returned 404
jinja2.exceptions.TemplateNotFound
TemplateNotFound: template.html
The main python code:
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def template_test():
return render_template('template.html', my_string="Wheeeee!", my_list=[0,1,2,3,4,5])
if __name__ == '__main__':
app.run(debug=True)
I have the following file structure:
flask_new_practice
|--template/
|--template.html
|--run.py
By default, Flask looks in the templates folder in the root level of your app.
http://flask.pocoo.org/docs/0.10/api/
template_folder – the folder that contains the templates that should
be used by the application. Defaults to 'templates' folder in the root
path of the application.
So you have some options,
rename template to templates
supply a template_folder param to have your template folder recognised by the flask app:
app = Flask(__name__, template_folder='template')

Categories

Resources