亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

表單值未添加到 Flask 中的數據庫中

表單值未添加到 Flask 中的數據庫中

明月笑刀無情 2023-04-25 17:07:15
我之前發布了這個問題,但它隨后被鏈接到一個類似的問題,該問題沒有提供所需的解決方案,然后被關閉以供回答。因此,我創建了一個 Flask 應用程序,它跟蹤產品從一個位置到另一個位置的移動,同時我通過 Flask 應用程序進行移動,表單沒有得到驗證我嘗試添加并添加到從用戶那里獲取輸入的 html{{ form.hidden_tag() }}文件{{ form.csrf_token }}。如果我從終端在我的命令行上運行這個應用程序,表單會被驗證并被添加到數據庫中,但是如果我運行 flask 應用程序并在瀏覽器中提交表單,它不會。這是我的代碼class MovementForm(FlaskForm):    to_location = SelectField('To Location', coerce=int)    from_location = SelectField('From Location', coerce=int)    product = SelectField('Product')    quantity = IntegerField('Quantity')    add_movement = SubmitField('Add Movement')@app.route('/movements',methods=["GET","POST"])def add_movements():    form = MovementForm()    form.to_location.choices = [(location.id, location.location_name) for location in Location.query.all()]    form.from_location.choices = [(location.id, location.location_name) for location in Location.query.all()]    form.product.choices = [(product.id, product.product_name) for product in Product.query.all()]    form.from_location.choices.insert(0, (0, 'None'))       if form.validate_on_submit():        new_movement = Movement(to_location_id=form.to_location.data, from_location_id=form.from_location.data, product_id=form.product.data, quantity=form.quantity.data)        db.session.add(new_movement)        db.session.commit()        flash('Product has been moved!', 'success')        return redirect(url_for('add_movements'))       return render_template('add_movements.html', form=form)這里出了什么問題?
查看完整描述

1 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

嘗試在 HTML 表單中刪除更改表單的操作。


 <form action="" method="post">

    {{ form.hidden_tag() }}

    {{ form.csrf_token }}

    <div class="row">

        <div class="form-group col">

            {{ form.from_location.label(class="form-control-label") }}

            {{ form.from_location(class="form-control form-control-lg") }}

        </div>

        <div class="form-group col">

            {{ form.to_location.label(class="form-control-label") }}

            {{ form.to_location(class="form-control form-control-lg") }}

        </div>

    </div>

    <div class="row">

        <div class="form-group col">

            {{ form.product.label(class="form-control-label") }}

            {{ form.product(class="form-control form-control-lg") }}

        </div>

        <div class="form-group col">

            {{ form.quantity.label(class="form-control-label") }}

            {{ form.quantity(class="form-control form-control-lg") }}

        </div>

    </div>

    <div class="form-group">

            {{ form.add_movement(class="btn btn-outline-info") }}

    </div>

</form> 

這能解決問題嗎?


此外,我建議您將 Flash 消息添加到 HTML 中,因為我看到一旦提交表單,它就會返回到“add_movements”函數。因此添加:


 <div>

     {% for msg in get_flashed_messages%}

         <h1>{{msg}}</h1>

     {% endfor %}

 </div>

 <form action="" method="post">

{{ form.hidden_tag() }}

{{ form.csrf_token }}

<div class="row">

    <div class="form-group col">

        {{ form.from_location.label(class="form-control-label") }}

        {{ form.from_location(class="form-control form-control-lg") }}

    </div>

    <div class="form-group col">

        {{ form.to_location.label(class="form-control-label") }}

        {{ form.to_location(class="form-control form-control-lg") }}

    </div>

</div>

<div class="row">

    <div class="form-group col">

        {{ form.product.label(class="form-control-label") }}

        {{ form.product(class="form-control form-control-lg") }}

    </div>

    <div class="form-group col">

        {{ form.quantity.label(class="form-control-label") }}

        {{ form.quantity(class="form-control form-control-lg") }}

    </div>

</div>

<div class="form-group">

        {{ form.add_movement(class="btn btn-outline-info") }}

</div>

#編輯


我注意到一旦強制丟失,產品字段中就缺少了一些東西:


class MovementForm(FlaskForm):

    to_location = SelectField('To Location', coerce=int)

    from_location = SelectField('From Location', coerce=int)

    product = SelectField('Product', coerce=int)

    quantity = IntegerField('Quantity')

    add_movement = SubmitField('Add Movement')

編輯#2


如果您遇到此類問題(這種情況經常發生),我建議您也添加打印語句和 If/Else 子句。這將極大地幫助您解決問題所在(您發布的問題類型的問題是您“看不到”)并且會給您“眼睛”。


例如,這就是我會做的:


@app.route('/movements',methods=["GET","POST"])

def add_movements():


    form = MovementForm()

    form.to_location.choices = [(location.id, location.location_name) for 

    location in Location.query.all()]

    form.from_location.choices = [(location.id, location.location_name) 

    for location in Location.query.all()]

    form.product.choices = [(product.id, product.product_name) for product 

    in Product.query.all()]

    form.from_location.choices.insert(0, (0, 'None')) 


    if form.validate_on_submit():

        print('Form Ok') #if you see the 'Form ok' to see if is validated

        new_movement = Movement(to_location_id=form.to_location.data, 

        from_location_id=form.from_location.data, 

        product_id=form.product.data, quantity=form.quantity.data)

        db.session.add(new_movement)

        db.session.commit()

        flash('Product has been moved!', 'success')

        return redirect(url_for('add_movements'))

     else:

         print('Form Not Ok') #If you see this printed then you see that 

          #is not validated


return render_template('add_movements.html', form=form)


查看完整回答
反對 回復 2023-04-25
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號