Flask CGI app与print语句有关

时间:2016-09-29 10:11:32

标签: python flask

我使用在共享托管服务器上运行的CGI + Apache部署了一个简单的烧瓶应用程序。

该应用程序在Python 2.6上运行Flask 0.11.1以及Flask-Mail 0.9.1。

应用程序中的一个页面有一个联系表单,可以发送电子邮件并重定向回同一页面。

联系表单对' / sendmail'进行了POST操作。在Flask控制器中定义如下 -

@app.route("/sendmail", methods=['GET','POST'])
def send_mail():
    print "Sending Email"
    mail = SendMail(app)
    mail.send_mail(request.form['name'], request.form['mail'], request.form['phoneNo'], request.form['message'])
    return render_template('contact.html')

这是问题 -

  1. 使用上面的代码,该应用程序成功向我发送了一封电子邮件,然后发出错误' / sendmail'未找到。模板无法呈现。
  2. 如果我从代码段中删除了print语句,则应用会在发送电子邮件后成功呈现contact.html。
  3. 什么解释了片段中print语句的行为?考虑到执行是顺序的,不应该在print语句本身发生块失败而不发送电子邮件而不是在呈现模板期间失败吗?

1 个答案:

答案 0 :(得分:0)

Print语句不应该创建错误,因为它是其他语句之一。相反,因为您没有检查// config/passport.js // load all the things we need var LocalStrategy = require('passport-local').Strategy; var FacebookStrategy = require('passport-facebook').Strategy; // load up the user model var User = require('../app/models/user'); // load the auth variables var configAuth = require('./auth'); module.exports = function(passport) { passport.use('facebook', new FacebookStrategy({ clientID : configAuth.facebookAuth.clientID, clientSecret : configAuth.facebookAuth.clientSecret, callbackURL : configAuth.facebookAuth.callbackURL, profileFields: ["emails", "displayName"] }, // facebook will send back the tokens and profile function(access_token, refresh_token, profile, done) { // asynchronous process.nextTick(function() { // find the user in the database based on their facebook id User.findOne({ 'id' : profile.id }, function(err, user) { // if there is an error, stop everything and return that // ie an error connecting to the database if (err) return done(err); // if the user is found, then log them in if (user) { return done(null, user); // user found, return that user } else { // if there is no user found with that facebook id, create them var newUser = new User(); // set all of the facebook information in our user model newUser.fb.id = profile.id; // set the users facebook id newUser.fb.access_token = access_token; // we will save the token that facebook provides to the user newUser.fb.firstName = profile.name.givenName; newUser.fb.lastName = profile.name.familyName; // look at the passport user profile to see how names are returned newUser.fb.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first // save our user to the database newUser.save(function(err) { if (err) throw err; // if successful, return the new user return done(null, newUser); }); } }); }); })); } ,所以这应该在您的get请求中创建并抛出错误。要重定向到同一页面request.method=='POST'请勿忘记从此return redirect("/sendmail")

中导入