Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择字段中的动态选择_Python_Flask_Flask Wtforms - Fatal编程技术网

Python 选择字段中的动态选择

Python 选择字段中的动态选择,python,flask,flask-wtforms,Python,Flask,Flask Wtforms,我正在尝试使用FlaskForms将userID变量传递给WTForms。首先,我将展示运行良好的代码,然后是我需要修改的内容(我不知道如何修改)。我正在添加与某个组关联的新名称 烧瓶形式型号: class AddName(FlaskForm): name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)]) groupID = SelectField('Payload Ty

我正在尝试使用FlaskForms将userID变量传递给WTForms。首先,我将展示运行良好的代码,然后是我需要修改的内容(我不知道如何修改)。我正在添加与某个组关联的新名称

烧瓶形式型号:

class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', choices=[(1,"Group1"),(2,"Group2")], validators=[InputRequired])
@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"
              <form method="POST" action="/dashboard/addname">
                  <h2>Add name</h2>
                  {{ form.hidden_tag() }}
                  {{ wtf.form_field(form.name) }}
                  {{ wtf.form_field(form.groupID) }}
                  <button type="submit">Add name</button>
              </form>
@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    available_groups=db.session.query(Groups).filter(Groups.userID == currend_user.userID).all()
    #Now forming the list of tuples, so it's ok for SelectField
    groups_list=[(i.groupID, i.groupName) for i in available_groups]
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"
class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', coerce=int, validators=[InputRequired])
查看模型:

class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', choices=[(1,"Group1"),(2,"Group2")], validators=[InputRequired])
@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"
              <form method="POST" action="/dashboard/addname">
                  <h2>Add name</h2>
                  {{ form.hidden_tag() }}
                  {{ wtf.form_field(form.name) }}
                  {{ wtf.form_field(form.groupID) }}
                  <button type="submit">Add name</button>
              </form>
@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    available_groups=db.session.query(Groups).filter(Groups.userID == currend_user.userID).all()
    #Now forming the list of tuples, so it's ok for SelectField
    groups_list=[(i.groupID, i.groupName) for i in available_groups]
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"
class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', coerce=int, validators=[InputRequired])
模板:

class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', choices=[(1,"Group1"),(2,"Group2")], validators=[InputRequired])
@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"
              <form method="POST" action="/dashboard/addname">
                  <h2>Add name</h2>
                  {{ form.hidden_tag() }}
                  {{ wtf.form_field(form.name) }}
                  {{ wtf.form_field(form.groupID) }}
                  <button type="submit">Add name</button>
              </form>
@app.route('/dashboard/addname', methods=['GET', 'POST'])
def addname():
    available_groups=db.session.query(Groups).filter(Groups.userID == currend_user.userID).all()
    #Now forming the list of tuples, so it's ok for SelectField
    groups_list=[(i.groupID, i.groupName) for i in available_groups]
    form=AddName()
    if form.validate_on_submit():
        name=Name(form.name.data,form.groupID.data)
        db.session.add(name)
        db.session.commit()
        return "New name added"
class AddName(FlaskForm):
    name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
    groupID = SelectField('Payload Type', coerce=int, validators=[InputRequired])
  • 如何将我的组列表传递到表单?我试图在FlaskForm模型中实现成形过程,但它没有看到当前的用户对象
  • 当我需要像元组一样将groupID传递给SelectField时,是否需要将groupID转换为string,然后再转换回int

  • 这里的主要思想是在实例化之后将选项列表分配给字段。为此,您需要使用参数
    强制=int
    。强制关键字arg到SelectField表示我们使用int()强制表单数据。默认强制是unicode()

    正确的表单模型:

    class AddName(FlaskForm):
        name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
        groupID = SelectField('Payload Type', choices=[(1,"Group1"),(2,"Group2")], validators=[InputRequired])
    
    @app.route('/dashboard/addname', methods=['GET', 'POST'])
    def addname():
        form=AddName()
        if form.validate_on_submit():
            name=Name(form.name.data,form.groupID.data)
            db.session.add(name)
            db.session.commit()
            return "New name added"
    
                  <form method="POST" action="/dashboard/addname">
                      <h2>Add name</h2>
                      {{ form.hidden_tag() }}
                      {{ wtf.form_field(form.name) }}
                      {{ wtf.form_field(form.groupID) }}
                      <button type="submit">Add name</button>
                  </form>
    
    @app.route('/dashboard/addname', methods=['GET', 'POST'])
    def addname():
        available_groups=db.session.query(Groups).filter(Groups.userID == currend_user.userID).all()
        #Now forming the list of tuples, so it's ok for SelectField
        groups_list=[(i.groupID, i.groupName) for i in available_groups]
        form=AddName()
        if form.validate_on_submit():
            name=Name(form.name.data,form.groupID.data)
            db.session.add(name)
            db.session.commit()
            return "New name added"
    
    class AddName(FlaskForm):
        name =StringField('Device name', validators=[InputRequired(),Length(min=4, max=30)])
        groupID = SelectField('Payload Type', coerce=int, validators=[InputRequired])
    
    正确视图:

    @app.route('/dashboard/addname', methods=['GET', 'POST'])
    def addname():
        available_groups=db.session.query(Groups).filter(Groups.userID == currend_user.userID).all()
        #Now forming the list of tuples for SelectField
        groups_list=[(i.groupID, i.groupName) for i in available_groups]
        form=AddName()
        #passing group_list to the form
        form.groupID.choices = groups_list
        if form.validate_on_submit():
            name=Name(form.name.data,form.groupID.data)
            db.session.add(name)
            db.session.commit()
            return "New name added"
    

    有没有更好的办法?比如使用元类之类的?