0

I am trying to call a regular function into RadioField - choices[()]. when i select a radio button on my web page and click submit button, i should get the result from from function. my function says to print("Hi"). currently it print's the data on given value like 'value_one' when i select description.

So, i need a way to do call function to choices[('')]. below is my code

from flask import Flask, render_template
from flask_wtf import Form
from wtforms import RadioField, SubmitField

app = Flask(__name__)
app.config.from_object(__name__)
app.secret_key = 'password'


def print_1():
    print("Hi")


class SimpleForm(Form):

    example = RadioField('Label', choices=[('value_one','description'),('value_two','whatever')])


@app.route('/',methods=['post','get'])
def hello_world():
    form = SimpleForm()
    if form.validate_on_submit():
        print(form.example.data)
    else:
        print(form.errors)
    return render_template('form.html',form=form)

if __name__ == '__main__':
    app.run(debug=True)

Below is my html code:

<form method="post">
    {{ form.hidden_tag() }}
    {{ form.example }}
    <input type="submit" value="submit">
</form>
venkat
  • 27
  • 8
  • You cannot "call" python function from html. Call it after `form.validate_on_submit()` – Adrian Krupa May 10 '19 at 07:10
  • can you show me an example. – venkat May 10 '19 at 07:17
  • `if form.validate_on_submit(): print_1()` – Adrian Krupa May 10 '19 at 07:23
  • Thanks. this works. i will ping you if i need help. really appreciate that. – venkat May 10 '19 at 07:28
  • i think this is only working as a single option. i want add another function likes say ``` def print_2(): print("Hello") ```. if i select other option it should print second function which is not. so how could set or call a different function for each radio button? – venkat May 10 '19 at 07:37
  • test for which option is selected like [this](https://stackoverflow.com/questions/31662681/flask-handle-form-with-radio-buttons) – Andrew Allen May 10 '19 at 17:07
  • thanks. i can get the value when i select each radio button. But what i am trying is to call a function to 'value' attribute and get the result from function when select radiobutton and click submit. i see only string or static data is given in 'value' attribute. @AndrewAllen – venkat May 10 '19 at 17:19
  • 1
    not 100% sure what you mean. if its what i think you mean then after `if form.validate_on_submit():` test for value of radiobutton and call whatever function you like if matches value, – Andrew Allen May 10 '19 at 18:13

0 Answers0