在将表单添加到html文件后,nginx / flask应用程序抛出500错误

时间:2016-12-12 20:42:06

标签: nginx flask config

我试图解决一个与nginx有关的问题。 Nginx在刷新时抛出500 error

我只是想通过formhtml页面上显示flask

forms.py

from flask_wtf import FlaskForm
from wtforms import TextField, StringField, SubmitField, validators

class EmailForm(FlaskForm):
  email = TextField("Email")
  submit = SubmitField("Send")

routes.py

from flask import Flask, render_template, request
from flask_mail import Mail, Message
from forms import EmailForm

app = Flask(__name__)
app.secret_key = 'xx'

# add mail server config
app.config['MAIL_SERVER'] = 'xxx'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'xx'
app.config['MAIL_PASSWORD'] = 'xx'

mail = Mail(app)
@app.route('/', methods=('GET', 'POST'))
def email():
    form = EmailForm()

    if request.method == 'POST':
        if form.validate() == False:
            return 'Please fill in all fields <p><a href="/contact">Try Again!!!</a></p>'
        else:
            msg = Message("Message from your visitor",
                          sender='xxx',
                          recipients=['xxx'])
            msg.body = """
            From: %s <%s>,
            %s
            """ % (form.email.data)
            mail.send(msg)
            return "Successfully  sent message!"
    elif request.method == 'GET':
        return render_template('index.html', form=form)
if __name__ == '__main__':
    app.run()

的index.html

        <form action="{{ url_for('email') }}" method="post">
            {{ form.hidden_tag() }}

            {{ form.email }}
            {{ form.submit }}
        </form>

我做错了什么?我的网站引发了500 internal server错误。

由于

1 个答案:

答案 0 :(得分:0)

这里有一些错误。首先,您没有使用FlaskForm,而是使用标准的Formcsrf_token不会使用hidden_tag()from flask_wtf import Form这可能是错误的根源。所以在 forms.py

from flask_wtf import FlaskForm替换为class EmailForm(Form):,将class EmailForm(FlaskForm):替换为validators.Email('your@email.com')

接下来,Email()没有任何意义。 validators.Email('That is not an email!')验证器接受的唯一参数是最终用户的错误消息。您应该删除它,或者用from wtforms import Form

之类的内容替换它

编辑

正如大卫所指出的,表格导入很好,只是被贬低了。如果您已完成msg = Message("Message from your visitor" + form.name.data, sender='xxx', recipients=['xxx']) msg.body = """ From: %s <%s>, %s """ % (form.name.data, form.email.data, form.message.data) mail.send(msg) ,那么 会出错。真正的问题似乎在这里:

form.name.data

您继续引用不存在的form.message.dataEmailForm。您的name字段中未定义messageEmailForm字段。如果您不想要名称或消息字段,则应将其删除;如果忘记了,则应将其添加到function render ({ scrollToIndex, ...rest }) { // Convert scrollToIndex to scrollStop const scrollTop = **rowOffset** + // Position of row within List **innerOffset** // Position of inner target within row 课程。