2017-10-10 162 views
-2

我想添加自定义404页面。flask python add 404页面

我有这样的代码:

#404 page 
@app.errorhandler(404) 
def page_not_found(e): 
    return render_template('/404.html'), 404 

,但它不工作 我仍然得到

二○一七年十月一十日09:24:04983:文件 “在/ usr /本地在get_source /lib/python2.7/dist-packages/flask/templating.py”,第61行,2017年10月10日 09:24:04983:提高 TemplateNotFound(模板)

UPD: 我在我的模板文件夹中有404.html文件! enter image description here

+1

它找不到模板文件,它没有任何与404错误。当你没有意见时,这个错误就会出现。 – senaps

+0

'render_template'从'template /'目录获取文件。创建一个模板目录并将文件放入其中或以可以提供完整文件路径的方式进行。 –

+0

@ Vj-查看更新 – user2950593

回答

0

它看起来像模板不是你期望找到它的地方。

您可以尝试放入/static并从那里读取?

@app.errorhandler(404) 
def page_not_found(e): 
    return send_from_directory("static", "404.html"), 404 
2

它像模板无法找到

@app.errorhandler(404) 
def page_not_found(e): 
    return render_template('path_folder/404.html'), 404