【问题标题】:I cant seem to get my flask app to display my Form- flask_wtf我似乎无法让我的烧瓶应用程序显示我的 Form-flask_wtf
【发布时间】:2020-05-31 23:16:29
【问题描述】:
@app.route('/contact', methods=['GET', 'POST'])
def contact():
    form = ContactForm()
    if request.method == 'POST':
        return render_template('contact.html',form=form)
                if form.validate_on_submit():
                      msg=Message(request.form['subject'], sender = request.form['name'],request.form['email'],
                      recepients=[request.form['email']]) 
                      msg.body='This is the body'
                      mail.send(msg)
                     flash('Message sent successfully')
                return redirect(url_for('/'))


我一直在尝试在我的烧瓶应用程序中运行一个简单的 Flask 表单,但是它不会显示该表单。我一直在网上搜索,我似乎无法找到我得到错误的地方。 这是我的 html 代码和 form.py:

{% extends "base.html" %}
{% block main %}
{% block content %}
<h1>Contact Form</h1>
<p> Fill in this form to contact the site owners</p>
<br>
<form action= "{{url_for('/contact')}", method= "post">

    <div class="form-group">
            <label for="name"> Name</label>
            <input type="text" class="form-control" name="name" id="name"/>
    </div>
    {{form.name.label}}  {{form.name}} <br>
    <br>
   <div class="form-group">
            <label for="email">E-mail</label>
            <input type="email" class="form-control" name="email" id="email" placeholder="jdoe@example.com" />
    </div>
  {{form.email.label}}{{form.email}}<br>
    <div class="form-group">
             <label for="subject">Subject</label>
             <input type="text" class="form-control" name="subject" id="subject"/>
   </div>                                                                                 
  {{form.subject.label}}{{form.subject}}<br>
     <div class="form-group">
       <label for="message">Message</label>
       <input type="text" class="form-control" name="message" id="message"/>
   </div>

  {{form.message.label}}<br>
  {{form.message}}
  {{form.csrf_token}}


<button type ="submit">Send</button>

</form>
{% endblock %}

【问题讨论】:

    标签: python html forms flask flask-wtforms


    【解决方案1】:

    您过早地渲染模板。试试吧:

    @app.route('/contact', methods=['GET', 'POST'])
    def contact():
        form = ContactForm()
        if form.validate_on_submit():
            msg = Message(request.form['subject'], 
                          sender=request.form['name'],
                          request.form['email'],
                          recipients=[request.form['email']]) 
            msg.body='This is the body'
            mail.send(msg)
            flash('Message sent successfully')
            return redirect(url_for('/'))
        return render_template('contact.html',form=form)
    

    【讨论】:

    • 谢谢你,我试过了,但没有解决问题
    • @Beginner-Coder:您收到任何错误消息吗?我测试了一个类似的例子,表单显示出来了。
    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多