For the final project, I am trying to implement a flask application where people may write diaries and read others' diaries. I would like to create a unique URL for each user that shows the diaries that user published, however, I keep getting this HTTP 404 Not Found Error as I test-run the application. I think that is (not only) because every time I flask run
the application and open up the web server, it is routed to https://ide50-chanchunhei.cs50.io/ instead of https://ide50-chanchunhei.cs50.io/index/username. If, instead of flask run
the application and opening up the web server, I type http://ide50-chanchunhei.cs50.io/login
into my browser, rather than getting back an HTTP 404 Not Found Error, I get this one: werkzeug.routing.BuildError: Could not build url for endpoint 'index'. Did you forget to specify values ['username']?
. Anyways, my code is as follow:
application.py
@app.route("/index/<username>")
@login_required
def index(username):
"""Cover page: read your own diaries."""
# lock the cover page until writer published first blog entry
diaries = db.execute("SELECT diaries, date FROM diaries WHERE username = :username ORDER by date DESC", username=session.get("user_id"))
if len(diaries) == 0:
return dumbo("Welcome to FoToM!")
return render_template("index.html", diaries=diaries, username=session.get("user_id"))
helpers.py
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/0.11/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect(url_for("login", next=request.url))
return f(*args, **kwargs)
return decorated_function
For your reference, my current code, unlike Problem Set 7, does not have any @app.route("/")
now. And these are some online resources I made references to in implementing unique URLs: Link1, Link2, Link3, and Link4.
Thanks in advance!