Flask app NameError: name 'Markup' is not defined

Flask app NameError: name 'Markup' is not defined

我真的被这个问题难住了,离开这个网络游戏 dev/python 有一段时间了。我有一个以前工作的 Azure 应用程序服务 运行 这个网站,我们做了一个小的 HTML 拼写更改并重新部署。我假设某些依赖项已更新,现在已损坏。我不知道这是不是打包版本问题,或者我的 Flask 应用程序是否缺少导入。

我在尝试加载静态 html 页面时遇到 NameError: name 'Markup' is not defined 错误。我的应用程序启动正常,但无法加载实际网页。

完整追溯

Traceback (most recent call last):
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 2095, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 2080, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\St**\PycharmProjects\**Site-portfolio\application.py", line 32, in index
    return render_template("//index.html")
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\templating.py", line 147, in render_template
    ctx.app.update_template_context(context)
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask\app.py", line 756, in update_template_context
    context.update(func())
  File "C:\Users\St**\PythonProjects\**Site-Portfolio\lib\site-packages\flask_recaptcha.py", line 59, in get_code
    return dict(recaptcha=Markup(self.get_code()))
NameError: name 'Markup' is not defined
127.0.0.1 - - [08/Apr/2022 17:02:51] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 304 -
127.0.0.1 - - [08/Apr/2022 17:02:51] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 304 -
127.0.0.1 - - [08/Apr/2022 17:02:51] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 304 -

Process finished with exit code 0

这是我的代码:

from flask import Flask, request, render_template, flash
from flask_mail import Mail, Message
from flask_cors import CORS, cross_origin
from flask_recaptcha import ReCaptcha
import requests
import json
import os

app = Flask(__name__, static_folder='static', static_url_path='')
recaptcha = ReCaptcha(app=app)

app.config['RECAPTCHA_ENABLED'] = True
app.config['RECAPTCHA_PUBLIC_KEY'] = '***********************'
app.config['RECAPTCHA_PRIVATE_KEY'] = '****************************'

@app.route('/', methods=['GET'])
def index():
    return render_template("//index.html")

@app.route('/contact-us', methods=['GET'])
@app.route('/contact', methods=['GET', 'POST'])
def contact():
    if request.method == 'POST':
        r = requests.post('https://www.google.com/recaptcha/api/siteverify',
                          data={'secret': '***********',
                                'response': request.form['g-recaptcha-response']})
        google_response = json.loads(r.text)

        if google_response['success']:
            contact_form = {'name': request.form['name'],
                            'email': request.form['email'],
                            'message': request.form['message']}
            msg = Message(subject='Contact from website',
                          sender=contact_form['email'],
                          recipients=['*************'],
                          body=contact_form['message'])
            mail.send(msg)
            flash('Success, we will respond within at least 24 hours.')
            return render_template('contact.html')

        else:
            flash('failed to submit, please retry or contact us at ************')
            return render_template('contact.html')

    return render_template('contact.html')

if __name__ == '__main__':
    app.run()

我的requirements.txt包版本

Flask>=1.0.2
jinja2>=2.11.3
Flask-Mail>=0.9.1
Flask-Cors>=3.0.9
Flask-Admin>=1.5.2
Flask-ReCaptcha>=0.4.2
Flask-SQLAlchemy>=2.3.2
Flask-WTF>=0.14.2
SQLAlchemy>=1.3.0
requests>=2.20.0
Flask-Login>=0.4.1
Werkzeug>=0.15.3

Flask-ReCaptcha 是一个非常古老的项目。 Flask-ReCaptcha最后一次更新是2016年,你最好不要用

回到错误日志本身,Flask-ReCaptcha 有类似 from jinja2 import Markup 的代码。但是,由于 jinja2==3.1.0, Markup's position has changed. Try pip install jinja2==3.0.0`.

您可能会遇到其他问题,因为 Flask-ReCaptcha 真的很老了。