-3

I'm a beginner here in Flask Web Development. Please do not marked this as duplicate because I dont see anything similar to my question.

I want to ask, how can I pass a boolean VARIABLE for example, status = True from JavaScript to Flask ? I know that we can use Flask variable in JavaScript but how can we pass boolean from JavaScript into Flask. I'm thinking of using post but I dont know how to start doing it.

I hope that experts can help me, Thanks.

iyzad
  • 81
  • 2
  • 2
  • 10
  • Are you using any framework? Jquery? If you are using WTF forms, then you can use BooleanField type – Nabin Aug 01 '17 at 05:20
  • Yes I'm using WTF forms and flask, BooleanField type ? I dont get it , I mean how to pass boolean status from JavaScript to Flask with BooleanField type? – iyzad Aug 01 '17 at 05:23

1 Answers1

1

Create a simple form in Flask as follow:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import Email, Length, InputRequired


class LoginForm(FlaskForm):
    email = StringField('Email', validators=[InputRequired(), Length(1, 64), Email()])
    password = PasswordField('Password', validators=[InputRequired()])
    remember_me = BooleanField('Keep me logged in') # Notice this.

Now we can send this form to template from views.py.

@app.route('/test', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        # Get remember_me parameter here using request.form.get('remember_me')
        flash('Invalid username or password.')
    return render_template('your_template.html', form=form)

Finally render the from in template file your_template.html.

<form class="form-horizontal" method="post">
              {{ form.hidden_tag() }}
                    <div class="col-md-offset-2 col-md-10">
                      <div class="checkbox">
                        <label>
                          {{form.remember_me}} Keep me logged in
                        </label>
                      </div>
                    </div>
                <div class="form-group">
                    <div class="col-md-10 col-md-offset-2">
                        <input type="submit" class="btn btn-raised btn-primary" value="Submit">
                    </div>
                </div>
          </form>

Notice the {{form.remember_me}}. When checked, this returns remember_me as y else None in views.py file.

NOTE: You may not be able to directly copy-paste this code, as you will certainly have to make changes based on your requirements. Please take this as reference only and it will surely help you and anyone else who gets here in future.

Nabin
  • 11,216
  • 8
  • 63
  • 98
  • I think you had mistaken my question because your answer is passing the boolean from flask into html while what I'm finding is how to pass the boolean from JavaScript into Flask. – iyzad Aug 01 '17 at 05:43
  • @iyzad Give it a try. This passes the boolean from HTML to flask (boolean reaches to views.py). – Nabin Aug 01 '17 at 05:44
  • Sorry I want to be able to get the boolean without using the BooleanField – iyzad Aug 01 '17 at 05:55
  • Please dont waste my time too – iyzad Aug 01 '17 at 06:01