我有一个带有选择标签的用户注册表格。我希望将所选选项中的值分配给变量(代码如下所示)。
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>
我已使用以下代码将选定的选项值分配给变量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)
但这似乎不起作用。任何帮助深表感谢。谢谢。
答案 0 :(得分:0)
您正在混合单选按钮并选择下拉菜单。
在以下模板中对此进行更改:
<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>
收件人:
<input type="radio" name="Theorist" value="Theorist"><span>Read lecture notes</span><br/>
<input type="radio" name="Theorist" value="Activist"><span>Watch videos</span>
在您的应用中:
learning_style = request.form.get("Theorist")