我正在創建一個 Web 應用程式,用戶將在其中注冊后收到確認郵件。但是我在命令列中收到錯誤Traceback(最近一次呼叫最后一次):檔案“/usr/local/lib/python3.9/site-packages/flask/cli.py”,第 240 行,在 locate_app 匯入( module_name) 檔案 "/home/ubuntu/flask/registration/application.py", line 3, in from flask_mail import Mail, Message ModuleNotFoundError: No module named 'flask_mail' and an error on the web page, Internal Server Error 服務器遇到內部錯誤,無法完成您的請求。服務器過載或應用程式中存在錯誤。
應用程式.py
import os
from flask import Flask, redirect, render_template, request
from flask_mail import Mail, Message
from cs50 import SQL
app = Flask(__name__)
app.config["MAIL_DEFAULT_SENDER"] = os.getenv("MAIL_DEFAULT_SENDER")
app.config["MAIL_PASSWORD"] = os.getenv("MAIL_PASSWORD")
app.config["MAIL_PORT"] = 587
app.config["MAIL_SERVER"] = smtp.gmail.com
app.config["MAIL_USE_TLS"] = True
app.config["MAIL_USERNAME"] = os.getenv("MAIL_USERNAME")
mail = Mail(app)
db = SQL("sqlite:///froshims.db")
REGISTRANTS = {}
SPORT=["Cricket", "Football", "Badminton", "Kho-Kho", "Kabaddi"]
@app.route("/", methods = ["GET", "POST"])
def index():
if request.method == "GET":
return render_template("index.html", sports = SPORT)
email = request.form.get("email")
sport = request.form.get("sport")
if not email:
return render_template("failure.html", message="E-mail not entered")
if not sport:
return render_template("failure.html", message="Sport not selected")
if sport not in SPORT:
return render_template("failure.html", message="Sport not in list. Don't try to hack our website.")
if request.method == "POST":
REGISTRANTS[name]=sport
print("yes")
db.execute("INSERT INTO registrants (name, sport) VALUES (?,?)", name, sport)
message = Message("You are registered!", recipients=[email])
mail.send(message)
return redirect("success")
@app.route("/success")
def success():
print("kaam kar na")
registrants = db.execute("SELECT * FROM registrants")
return render_template("success.html", registrants = registrants)
我無法弄清楚這個問題。我是燒瓶的新手。請指導我。
uj5u.com熱心網友回復:
我在flask_mail匯入時遇到了同樣的問題。pip在虛擬環境中在代碼編輯器上安裝Flask-Mail可能不會導致模塊匯入錯誤。轉到您的命令列進入應用程式目錄此外,請確保您的 venv 環境中的 pip 版本是最新的。
pip install Flask-Mail
這對我有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353814.html
