Bobys
Bobys

Reputation: 677

Each form should be processed from different python function (Flask)

I have a Flask Web page which, includes several Modals. Those Modals include forms. For example one modal, includes a form for changing the user's password. Another modal includes a form for adding a new user. etc

I have also several functions that will process the data that I will get from the forms and update the database.

I can do that with one form, like I did with my login form:

def post(self):
        if 'logout' in flask.request.form:
            flask.session.pop('username', None)
            flask.flash("You've been succsessfuly logged out!")
            return flask.redirect(flask.url_for('login'))
        required = ['username', 'passwd']
        for r in required:
            if r not in flask.request.form:
                flask.flash("Error: {0} is required.".format(r))
                return flask.redirect(flask.url_for('login'))
        username = flask.request.form['username']
        passwd = flask.request.form['passwd']
        if username in users and users[username] == passwd:
            flask.session['username'] = username
            flask.flash("Login Succsessful!")
            userID = database().getID(username)
            flask.session['userID'] = userID
            flask.session['userEmail'] = database().getEmail(userID)
            flask.session['mNumber'] = database().getmNumber(userID)
            return flask.redirect(flask.url_for('index'))
        else:
            flask.flash("Username doesn't exist or incorrect password")
        return flask.redirect(flask.url_for('login'))

But here I have only 2 variables, username and password. Both are required and I know the Inputs will be only those variables.

On the page with the modals, if the user fill the form "Change e-Mail" the only input will be the email address. If the user fill the form "Change Password". the only input will be the password and not the e-Mail.

My question now is, how can I determine in the flask app, which form the user filled out, so I can process the data accordingly?

Upvotes: 1

Views: 367

Answers (1)

Vasily Gladkov
Vasily Gladkov

Reputation: 36

To distinguish the forms user sent to you, you can introduce a field. You can add a field with the same name to all needed forms, name it something like a 'target' or 'form' and store form name in it. Field should be rendered as hidden input element. Then use if/elif/else in your flask code to read the value of the input and decide, which form is it.

There is also much cleaner solution: to use WTForm or Django forms. They do it differently adding form prefix to each input name so there is no more questions which form were sent and who need to process the data. Why don't you use one of them? They have a lot of additional benefits.

To explain first solution, here is the sample form from bootstrap example gallery:

<form class="form-signin" role="form">
    <h2 class="form-signin-heading">Please sign in</h2>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
    <div class="checkbox">
      <label>
        <input type="checkbox" value="remember-me"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

What you can do is to add new hidden field into the form: <input type='hidden' name='target' value='singin'> for sign-in form. Then read this value in your Flask handler flask.request.form.get('target') - if it is 'signin' you can be sure you are processing sign in form. And so on for other types of forms on the page

Upvotes: 1

Related Questions