Wtforms the HTML form control <select> Selectfield Wrapper to implement the drop-down list.
There are two cases of using a drop-down list
The first direct drop-down selects the set value and then reads the selected value into the store. This is a relatively simple situation,
1. First, define the form in form.py and the Selectfield drop-down list
App\main\form.pyclass smformadmin (form): status = Selectfield (' Query by Type ', validators=[required ()], choices=[(' 0 ', ' All '), (' 1 ', ' pending Review '), (' 2 ', ' certified Success '), (' 3 ', ' certification failure ')] ) submit = Submitfield (' Submit ')
The Selectfield instance must have the options set in its Choices property. The option must be a tuple of tuples that contain two elements: an identifier for the option and a text string to display in the control.
2. Read the value of the form in the route and use it directly or into the database
App\main\views.py@main.route ('/sm_admin ', methods=[' GET ', ' POST ']) @login_requireddef sm_admin (): user = User.query.filter_by (Email=current_user.email). First () if user.user_role > 0: <strong>form = Smformadmin () </strong> if Request.method = = ' POST ': if Form.validate_on_submit (): <strong> Sm_status = form.status.data</strong> #从表单中读取选择的值 ......... ......... The read value is used directly here, using the procedure to omit return render_template (' sm_admin.html ', Result=result, Form=form, pagination=pagination) return render_template (' sm_admin.html ', form=form) else: return Redirect (Url_for ('. index '))
The
second need to synchronize the database, such as students fill in information, the drop-down list of the college should be the database in the table data.
1. The data model is defined as follows:
app\models.py
Class Department (db. Model): __tablename__ = ' departments ' ID = db. Column (db. Integer, primary_key=true) Department = db. Column (db. String (100))
<pre name= "code" class= "python" >user_department = db. Table (' user_department ', db. Column (' user_id ', db. Integer, Db. ForeignKey (' users.id '), primary_key=true), db. Column (' department_id ', db. Integer, Db. ForeignKey (' departments.id '), primary_key=true))
Class User (Usermixin, DB. Model): __tablename__ = ' users ' id = db. Column (db. Integer, Primary_key=true) ..... ..... ................... Other columns Omit departments=db.relationship (' Department ', secondary=user_department, Backref=db.backref (' users ', lazy= ' Dynamic '), lazy= ' dynamic ')
Here the student because can turn the college, moreover the college so the student and the college is many to many relations, the general student and the college should be many to one relationship is relatively simple. (Many-to-many relationships are explained later
2. Define the form, you need to assign a value to the choices in the initialization function
App\main\form.pyclass smform (Form): .......... .............. Other form types omitted pre_department = Selectfield (' Original Academy: ', coerce=int) cut_department = Selectfield (' Current college: ', Coerce=int) submit = Submitfield (' Submit ') <strong> def __init__ (self, user, *args, **kwargs): super (Smform, Self). __init__ (*args, **kwargs) self.pre_department.choices = [(Pre_department.id, Pre_department.department) For pre_department in Department.query.order_by (department.department). All ()] Self.cut_department.choices = [(Cut_department.id, cut_department.department) for cut_department in Department.query.order_by (department.department). All ()] Self.user = user</strong>
The choices list is set in the form's constructor, and its value is obtained from the Department model, using a query to arrange all colleges in the order of the Academy ID , The identifier in the tuple is the ID of the Academy, because this is an integer, so the Coerce=int parameter is added to the Selectfield constructor to convert the value of the field to an integer.
3. Write the route function.
Because the original route function is very complex, it is not affixed, only a few key words:
@main. Route ('/sm ', methods=[' GET ', ' POST ') @login_required @main.errorhandler (404) def sm (): user = User.query.filter_by (Email=current_user.email). First () form = smform (user) <pre name= "code" class= "Python" > if Form.validate_on_submit (): .......... ................ Omit other <pre name= "code" class= "Python" > <strong>user.departments.append (Department.query.get ( Form.pre_department.data)) user.departments.append (Department.query.get (form.cut_department.data)) </ Strong><pre name= "code" class= "Python" > db.session.add (User) db.session.commit () Flash (' You have submitted a real name Certification request ') return redirect (Url_for ('. sm_success ')) return render_template (' sm.html ', form=form)
Flask drop-down list to synchronize with database