0

当我运行下面的文件时,出现此错误:

Traceback (most recent call last):
  File "/Users/msk/bssproject/sendmail.py", line 1, in <module>
    from flask import Flask, render_template, request, flash
ImportError: No module named flask
[Finished in 0.0s with exit code 1]

我已经确认 Flask 已正确安装,所以并不是我没有安装 Flask。

这是文件:

from flask import Flask, render_template, request, flash
import flask.views
from flask.forms import ContactForm
from flask.ext.mail import Message, Mail

mail = Mail()

app = Flask(__name__)

mail.init_app(app)

app.config.update( 
    DEBUG = True, 
    MAIL_SERVER = 'smtp.gmail.com', 
    MAIL_PORT = 465, 
    MAIL_USE_TLS = False, 
    MAIL_USE_SSL = True, 
    MAIL_USERNAME = 'usernname@gmail.com', 
    MAIL_PASSWORD = 'password%')

mail = Mail(app)

def send_email():
    form = ContactForm()

    if request.method == 'POST':
        if form.validate() == False:
            flash('All fields are required')
        return render_template('feedback.html', form=form)
    else:
        msg = Message(form.subject.data, sender='mlkrgr@gmail.com', recipients=['mlkrgr@gmail.com'])
        msg.body = "feedback.html"      
        send (form.name.data, form.email.data, form.message.data)
        mail.send(msg)
    return 'Form posted'

class Submit(flask.views.MethodView):
    def post(self):
        send_email()
        return flask.render_template('thanks.html')

app.add_url_rule('/',
        view_func=Submit.as_view('submit'),
        methods=["POST"])

app.run()
4

0 回答 0