簡體   English   中英

WTForms /燒瓶形式無法通過提交按鈕正確檢索數據

[英]WTForms/flaskforms not retrieving data properly via submit button

所以我只是想讓“提交”按鈕正常工作。 正常工作意味着獲取用戶輸入的電子郵件和密碼以直接登錄到我的登錄名。

當前,它僅重定向到index.html,但我希望它以重定向到配置文件或錯誤的結果來實現。

這是python部分:

@ app.route(“ / login”,Methods = [“ GET”,“ POST”])

def login():

"""Log user in if credentials provided are correct."""

form = LoginForm(request.post)

# this is if its POST
if form.validate and request.method == 'POST':
    email = request.form['email']
    password = request.form['password']

    if email == admin@gmail.com" and password == "admin":
        return redirect(url_for('/home'))
    else:
        return redirect(url_for('/error'))
# this is if its GET
#return render_template("index.html", form=form)

這是登錄表格

類LoginForm(FlaskForm):

email = StringField('email', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
remember = BooleanField('remember me')

這是html部分:

<div class="modal-body">
                <form role="form">
                    <div class="form-group">
                        <div class="input-group">

                            <form method="POST" action="{{ url_for('login')}}">
                                {{ form.csrf }}
                               <dl style="width: 100%;">
                                    <div class="form-group">
                                        <form role="form">
                                            <div class="form-group">
                                                <div class="input-group">
                                                    {{ wtf.form_field(form.email) }}
                                                    {{ wtf.form_field(form.password) }}
                                                </div>
                                            </div> <!-- /.form-group -->
                                        </form>
                                    </div> <!-- /.form-group -->

                                    <div style="margin-left: 70%;" class="checkbox">
                                        {{ wtf.form_field(form.remember) }}
                                    </div>

                                    <div class="modal-footer">
                                       <input class="btn btn-lg btn-primary btn-block" style="background-color: #3eb2a0;border-color: #3eb2a0;" type="submit" value="Sign In">
                                    </div>
                                </dl>
                            </form>

                        </div>
                    </div>
                </form>
            </div>

我發現您的代碼只有一個問題:

if email == admin@gmail.com" and password == "admin":
    return redirect(url_for('/home'))

您在admin@gmail.com之前沒有引號

二:

return redirect(url_for('/home'))

有正斜線的原因嗎? 你試過“回家”了嗎

編輯:

這是我如何設置與您相似的視圖的示例

@bp.route('/upvote', methods=['GET', 'POST'])
def view():

form = key()


if form.validate_on_submit():
    received_key = form.key_code.data
    url = form.url.data
    username = form.username.data

    return redirect(url_for('views.success')) #views is the name of the blueprint the success route is in


return render_template('upvote.html', title='title here', form=form)

form.validate_on_submit():代替form.validate和form.submit.data/if response.method ='POST'

然后您可以通過form.variable.data檢索存儲在表單中的數據。

檢查以查看您是否甚至還沒有收到來自表單的數據。 似乎可能無法接收到發布請求並跳過“ if request.method ='POST'”語句下的所有內容。

您的視圖如下所示:

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

def login()
form = LoginForm()

# this is if its POST
if form.validate_on_submit():
    email = form.email.data
    password = form.password.data

    if email == "admin@gmail.com" and password == "admin":
        return redirect(url_for('home'))
    else:
        return redirect(url_for('error'))


#return render_template("index.html", form=form)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM