I am fairly new to flask and am trying to create a personal website as an exercise. I'm looking to build a project page, I wanted to ask the following questions regarding file and project url path:
- I am trying to put all my project pages in the
/projects
folder. How do Irender_template
from a different folder so thatproj1()
function is pulling from/projects/proj1.html
instead of/tempates/.proj1.html
? Is there a better way of specifying a different folder location? - In
app.py
, I have@app.route("/projects/project1")
hard-coded in forproj1.html
. What is a better way to change this so that it can create a path for other projects likeproj2.html
andproj3.html
? I believe I may need to write something like@app.route("/projects/<project>")
and pass in the project name somehow?
Here is the folder structure:
/project
app.py
/templates
home.html
about.html
layout.html
proj1.html
projects.html
/projects
proj1.html
proj2.html
proj3.html
My app.py
looks like this:
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
@app.route("/home")
def home():
return render_template('home.html')
@app.route("/projects/")
def projects():
return render_template('projects.html', title='Projects')
@app.route("/projects/project1")
def proj1():
return render_template('proj1.html')
if __name__ == '__main__':
app.run(debug=True)
Inside projects.html
I have the following:
<h2><a href="project1">Project Link</a></h2>
Thank you! There's still a lot more work to be done, but I would set up the project page for now.