First of all, this question is not duplicated, because I looked everywhere on Google, GitHub and here and no one answered this.
I'm creating a CRUD for users of an app and part of the project is to give admins the power of altering users. Users have roles like Common Users or Admins.
When an admin clicks on the button to edit a user's info, a modal appears on the screen with the form.
What I want is to be able to mark an option as selected based on the user's info. If the user is an admin, the selected option that appears on modal opening will be by default admin. And if the user is a common user, I want to the role "Common User" to be selected instead, but for some reason, only Admin role appears selected.
Template:
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<input type="hidden" name="user_id" value="{{ user.user_id }}">
{{ form.username(placeholder=user.username) }}
{{ form.full_name(placeholder=user.full_name) }}
{{ form.e_mail(placeholder=user.e_mail) }}
{{ form.role() }}
{{ form.new_password }}
{{ form.repeated_new_password }}
...
Form:
class AlterUserDataForm(FlaskForm):
user_id = IntegerField(
'User ID')
username = StringField(
'Username',
validators=[Optional()])
full_name = StringField(
'Full name',
validators=[Optional()])
email = StringField(
'Email',
validators=[Optional()])
role = SelectField(
'Role',
choices=[
(1, 'Admin'),
(2, 'User')],
validators=[Optional()]
)
new_password = StringField(
'New password',
render_kw={'placeholder': 'Type the new password'},
validators=[Optional()])
repeated_new_password = StringField(
'Repeat new password',
render_kw={'placeholder': 'Repeat new password'},
validators=[Optional(), EqualTo('new_password')])
submit = SubmitField('Change')
I tried putting default value on form.role
, but it didn't work as I expected.
Can anyone help?
Versions:
- Flask: 1.0.3
- Flask-WTForms: 0.14.2