2017-06-30 114 views
0

我在下面有一個簡單的wt-form類。隨着提交按鈕,我需要一個取消按鈕,讓用戶回到主頁。有沒有一種方法可以在課堂中包括這一點?有沒有辦法在flask wtforms中創建自定義Field/CancelField?

我試圖寫一個函數,並將其傳遞給SubmitField(標記爲取消),但它似乎不工作。這裏是我的代碼:

P.S:我'處理使用wtf.quick_form(形式)的形式,如果有任何關聯到這個問題

class RegistrationForm(FlaskForm): 
username = StringField('Username', validators=[DataRequired(), Length(3, 16, message='3 to 6 Characters'), user_exists]) 
email = StringField('Email', validators=[DataRequired(), Email(), email_exists]) 
password = PasswordField('Password', validators=[DataRequired(), Length(5), EqualTo('confirm', message='Password Must Match')]) 
confirm = PasswordField('Confirm Password', validators=[DataRequired()]) 
submit = SubmitField('Submit') 
# cancel = SubmitField('Cancel', validators=([go_back]) 

回答

0

您可以處理在視圖中。當用戶按下「提交」按鈕時,form.submit.data被設置爲True。同樣,當按下「取消」時,form.cancel.data設置爲True

這裏是一個最小的例子來說明這一點:

forms.py

class RegistrationForm(FlaskForm): 
    # ... 
    submit = SubmitField('Submit') 
    cancel = SubmitField('Cancel') 

views.py

@app.route('/register', methods=['GET', 'POST']) 
def register(): 
    form = RegistrationForm() 
    if form.validate_on_submit(): 
     # The user pressed the "Submit" button 
     if form.submit.data: 
      # ... 
     # The user pressed the "Cancel" button 
     else: 
      return redirect(url_for('homepage')) 
    return render_template('register.html', form=form) 
相關問題