On my website, I want to be able to give the user better feedback on a 404 error than "page not found". If a view raises a 404 error, I want to pass an extra parameter to the template that describes what the view shows. For instance:
views.py
def get_or_404(model, attribute, value, template, **kwargs):
try:
return session.query(model).filter(attribute == value).one()
except NoResultFound:
abort(404)
@app.errorhandler(404)
def page_not_found(error):
return render_template("404.html"), 404
@app.route("project/<project_id>")
def project_show(project_id):
"""
Show the files contained in a specific project.
:param int project_id: The ID of the desired project.
"""
project = get_or_404(Project, Project.id, project_id, "item_not_found.html", item="project")
# Other stuff
If the user attempts to go to a project that does not exist, then they are sent to a 404 page. If I can provide the item kwarg (from project_show) to the template 404.html, then the template will say, for instance, "Project not found" instead of just "page not found".
What is the best way to do this?