0

I have a user registration form which has a select tag. I want the value from the selected option to be assigned to a variable(the code is shown below).

registration.html

<form method="POST" action="/register">
            <div class="form-group">
                {{render_field(form.name, class_="form-control", placeholder="Name")}}
            </div>
            <div class="form-group">
                {{render_field(form.email, class_="form-control",placeholder="Email")}}
            </div>
            <div class="form-group">
                {{render_field(form.username, class_="form-control", placeholder="Username")}}
            </div>
            <div class="form-group">
                {{render_field(form.password, class_="form-control", placeholder="Password")}}
            </div>
            <div class="form-group">
                {{render_field(form.confirm, class_="form-control", placeholder="Re-enter Password")}}
            </div>
             <div class="form-group">
                 <p>I prefer to : </p>
                 <select id= "studenttype" name="studenttype">
                    <input type="radio" name="Theorist" value="Theorist"><span>Read lecture notes</span><br/>
                    <input type="radio" name="Theorist" value="Activist"><span>Watch videos</span>
                 </select>
             </div>
            <input type="submit" class="btn btn-success" value="Submit">
            <p>Register as <a href="/register_instructor">Instructor</a> </p>
        </form>

I have used the following code to assign the selected option value to the variable learning_style.

app.py

@app.route('/register', methods=['GET', 'POST'])
def register():
    learning_style = request.form.get("studenttype")
    form = RegisterForm(request.form)
    if request.method == "POST" and form.validate():
        name = form.name.data
        email = form.email.data
        username = form.username.data
        password = form.password.data
        user_type = 'student'
        print(learning_style)

        ...........

        flash('You are now registered', 'success')
        redirect(url_for('index'))
    return render_template('register.html', form=form)

But this doesn't seem to work. Any help is much appreciated. Thanks.

1 Answer 1

1

You are mixing up radio buttons and select dropdowns.

Change this in your template from:

                  <select id= "studenttype" name="studenttype">
                    <input type="radio" name="Theorist" value="Theorist"><span>Read lecture notes</span><br/>
                    <input type="radio" name="Theorist" value="Activist"><span>Watch videos</span>
                  </select>

to:

                    <input type="radio" name="Theorist" value="Theorist"><span>Read lecture notes</span><br/>
                    <input type="radio" name="Theorist" value="Activist"><span>Watch videos</span>

And in your app:

learning_style = request.form.get("Theorist")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.