gpt4 book ai didi

python - 我似乎无法让我的 flask 应用程序显示我的表单-flask_wtf

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:08 26 4
gpt4 key购买 nike

@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 应用程序中运行一个简单的 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 %}

最佳答案

您渲染模板的时间太早了。尝试一下:

@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)

关于python - 我似乎无法让我的 flask 应用程序显示我的表单-flask_wtf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60255408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com