我遵循了flask教程,我想使用错误处理404和500来显示自定义网页。我在static/templates文件夹中创建了404.html和500.html。
当我尝试输入错误的url时,例如'http://127.0.0.1:5000/wfefef,我只得到一个空白页面和一个控制台错误:127.0.0.1---[09/jul/2021 14:35:12]“get/wfeef http/1.1”404,而不是给我自定义html页面来处理此类错误。我做错了什么?
文件夹结构图像:
app.py的代码如下所示:
import os
import pickle
from flask import Flask, flash, request, redirect, render_template
from flask_bootstrap import Bootstrap
from sklearn.feature_extraction.text import CountVectorizer
from werkzeug.utils import secure_filename
from ocr import ocr_processing
import werkzeug
UPLOAD_INPUT_IMAGES_FOLDER = '/static/uploads/'
ALLOWED_IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.secret_key = ''
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024
@app.errorhandler(404)
def not_found_error(error):
return render_template('404.html'), 404
@app.errorhandler(werkzeug.exceptions.HTTPException)
def internal_error(error):
return render_template('500.html'), 500
404.html
{% extends "base.html" %} {%block content %}
<h1>File Not Found</h1>
<p><a href="{{ url_for('upload') }}">Back</a></p>
{% endblock %}
我有一个类似的500错误代码
1条答案
按热度按时间ijnw1ujt1#
尝试在 flask 上使用
MVC
图案创建这样的结构!/usr/bin/env python
coding: utf-8
from flask import Blueprint
main = Blueprint('main', name)
from . import views
```
error.py
文件!/usr/bin/env python
coding: utf-8
from . import errors # it is important
@main.route("/", methods=['GET'])
def index():
return render_template('index.html')