I tried the method from this question:
use many submit buttons in the same form
When i try this method i got:
if request.form.action == "one":
AttributeError: 'ImmutableMultiDict' object has no attribute 'action'
Can any on tell me what i'm doing wrong?
'''python
from flask import Flask, request, render_template,url_for
from flask_wtf import FlaskForm
from werkzeug.utils import redirect
from wtforms import StringField, SubmitField
app = Flask(__name__)
app.secret_key = 'kable'
class Asd(FlaskForm):
name = StringField('Name')
l_name = StringField('ForName')
test = StringField('Test')
submit = SubmitField('Submit')
@app.route('/', methods=['GET', 'POST'])
def test():
test = False
form = Asd()
if request.method == "POST":
if request.form.action == "one":
test = form.name.data
return redirect(url_for('sub', form=form, test=test))
elif request.form.action == "two":
test = form.l_name.data
return redirect(url_for('sub', form=form, test=test))
elif request.method =='GET':
return render_template('home.html', form=form)
@app.route('/test', methods=['GET', 'POST'])
def sub(test):
form = Asd()
if form.validate_on_submit():
if request.form['submit'] == "one":
form.test.data = test
# send_test = form.name.data
return redirect(url_for('sub'))
elif request.form['submit'] == "two":
form.test.data = test
# send_test = form.l_name.data
return redirect(url_for('sub'))
elif request.method =='GET':
return render_template('home.html', form=form)
if __name__ == '__main__':
app.run()'''
html
{{ form.hidden_tag() }}
{{ form.name.label }}
{{ form.name(class="form-group") }}
<!-- {{ form.submit(value='one') }} -->
<div class="form-group">
<input type ="submit" name="action" value="one">
</div>
<br>
{{ form.l_name.label }}
{{ form.l_name(class="form-group") }}
<!-- {{ form.submit(value='two') }} -->
<div class="form-group">
<input type ="submit" name="action" value="two">
</div>
<br>
{{ form.test }}
</form>
'''
Now it works
from flask import Flask, request, render_template,url_for,session
from flask_wtf import FlaskForm
from werkzeug.utils import redirect
from wtforms import StringField, SubmitField
app = Flask(__name__)
app.secret_key = 'kable'
class Asd(FlaskForm):
name = StringField('Name')
l_name = StringField('ForName')
test = StringField('Test')
submit = SubmitField('Submit')
@app.route('/', methods=['GET', 'POST'])
def test():
# test = False
form = Asd()
if request.method == "POST":
if request.form["action"] == "one":
session['name'] = form.name.data
session['l_name'] = form.l_name.data
session['test'] = form.name.data
return redirect(url_for('sub'))
elif request.form["action"] == "two":
session['name'] = form.name.data
session['l_name'] = form.l_name.data
session['test'] = form.l_name.data
return redirect(url_for('sub'))
elif request.method =='GET':
return render_template('home.html', form=form)
@app.route('/test', methods=['GET', 'POST'])
def sub():
form = Asd()
if request.method == "POST":
if request.form["action"] == "one":
session['name'] = form.name.data
session['l_name'] = form.l_name.data
session['test'] = form.name.data
return redirect(url_for('sub'))
elif request.form["action"] == "two":
session['name'] = form.name.data
session['l_name'] = form.l_name.data
session['test'] = form.l_name.data
return redirect(url_for('sub'))
elif request.method =='GET':
form.name.data = session['name']
form.l_name.data = session['l_name']
form.test.data = session['test']
return render_template('home.html', form=form)
if __name__ == '__main__':
app.run()
'''
{{ form.hidden_tag() }}
{{ form.name.label }}
{{ form.name(class="form-group") }}
<!-- {{ form.submit(value='one') }} -->
<div class="form-group">
<input type ="submit" name="action" value="one">
<!-- {{ form.submit(class="btn", value="one") }} -->
</div>
<br>
{{ form.l_name.label }}
{{ form.l_name(class="form-group") }}
<!-- {{ form.submit(value='two') }} -->
<div class="form-group">
<input type ="submit" name="action" value="two">
<!-- {{ form.submit(class="btn", value="two") }} -->
</div>
<br>
{{ form.test }}
</form>
question from:
https://stackoverflow.com/questions/65938462/multiple-submit-buttons-in-flask