This question already has an answer here:
Why use Flask's url_for?
(1 answer)
Closed 3 years ago.
im new to flask and boostrap,im trying to make a button that link to another html
Project layout
-Norman
-args
-static
-css
-bootstrap.min.css
-style.css
-img
-wall.jpg
-templates
-index.html
-form.html
-__init__.py
-routes.py
-.flaskenv
-readme.txt
-book.py
route.py:
from args import app # init.py
from flask import render_template
#app.route('/')
def index():
return render_template('index.html')
#app.route('/form')
def form():
return render_template('form.html')
__init__.py:
from flask import Flask
from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
from args import routes
here is the code from index.html:
<div>
<button class="submit-btn">Check availability</button>
check form
</div>
im trying to link the button to open form.html with href but it says the requested url was not found on the server. opening localhost:5000 worked but the second route doesnt working
In your index.html: Replace /form.html to /form as you have registered the handle /form which when triggered, would render form.html
<div>
<button class="submit-btn">Check availability</button>
check form
</div>
Related
This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 1 year ago.
when im trying to run the main.py i got TemplateNotFound: index.html
project file structure
app
templates : base.html, index.html
static
views.py
## viwes.py
from flask import render_template, request
from flask import redirect, url_for
def base():
return render_template('base.html')
def index():
return render_template('index.html')
def faceapp():
return render_template('faceapp.html')
##main.py
from flask import Flask
from app import views
app = Flask(__name__)
#### URL
app.add_url_rule('/base', 'base',views.base)
app.add_url_rule('/','index', views.index)
app.add_url_rule('/faceapp','faceapp', views.faceapp)
### RUN
if __name__ == "__main__":
app.run(debug=True)```
app = Flask(__name__, template_folder='path/to/templates/dir')
You can also explicitly define the path to the template directory as above.
You can also refer Flask Template for more details.
This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 2 years ago.
I have been following a YouTube tutorial made by Corey Schafer using Flask. I have reached the 2nd tutorial about using html templates, but that is the only place I have reached. This is the program I am running called hello.py:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
app.run()
This is the HTML file I have been using, called home.html:
<!DOCTYPE html>
<html>
<head>
<title>Flask Template Example</title>
</head>
<body>
<div>
<p>{{ message }}</p>
</div>
</body>
</html>
Whenever I try to run my code, I always get the error jinja2.exceptions.TemplateNotFound: template.html. I've tried to look at all possible solutions, but none have seemed to work. How could I fix this? I'm on a Windows 64-bit machine.
By default un Flask, the template folder is templates/. If home.html is in the same directory as app.py, you need to set template_folder.
Here is how to fix your app.py:
from flask import Flask, render_template
app = Flask(__name__, template_folder='./')
#app.route('/')
def home():
return render_template('home.html')
app.run()
To use the default template location (which is recommended), this is the file structure you would need to have:
app.py
templates
└── home.html
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})
I want to show an image in the homepage of a python web application. So far I wrote the following program:
My directories and files
myWebApp/
app/
__init__.py
views.py
templates/
home.html
static/
Desert.jpg
run.py
__init__.py
from flask import Flask
app = Flask(__name__)
from app import views
views.py
from app import app
from flask import render_template
from flask import url_for
#app.route('/')
def root():
imag = url_for ('static', filename = 'Desert.jpg')
tle = "Hey"
return render_template('home.html', imag,tle)
home.html
<html>
<title>{{ tle }}</title>
<body>
<img src="{{ imag }}"/>
</body>
</html>
run.py
from app import app
if __name__ == "__main__":
app.run()
And when I run the run.py, I receive the following Internal Server Error:
What's wrong?
That's not the correct syntax for the render_template function. You need to use keyword arguments:
return render_template('home.html', imag=imag, tle=tle)
I'm running a simple test site on PythonAnywhere using Flask. When I run the script, the initial site (index.html) appears, and everything seems fine. However, when I click on any of the links (like signup.html), I get a 404 error:
Not Found
The requested URL was not found on the server.
If you entered the URL manually please check your spelling and try again.
However, the HTML files are all in the templates folder, along with index.html. Why can't they be found on the server?
Here is the Python code that runs the app:
from flask import Flask
from flask import render_template
app = Flask(__name__)
#app.route('/')
def runit():
return render_template('index.html')
if __name__ == '__main__':
app.run()
And here is the HTML portion of index.html that holds the link:
<a class="btn btn-lg btn-success" href="signup.html">Sign up</a>
You need to create another route for your signup URL, so your main webapp code needs to add a route for '/signup.html', i.e.
from flask import Flask
from flask import render_template
app = Flask(__name__)
#app.route('/')
def runit():
return render_template('index.html')
#app.route('/signup.html')
def signup():
return render_template('signup.html')
if __name__ == '__main__':
app.run()
If you want your URLs to be a little cleaner, you can do something like this in your Python:
#app.route('/signup')
def signup():
return render_template('signup.html')
And change your link code to match.
<a class="btn btn-lg btn-success" href="signup">Sign up</a>
The main Flask documentation has a good overview of routes in their Quickstart guide: http://flask.pocoo.org/docs/quickstart/