Flask WTForms always give false on validate_on_submit()

后端 未结 2 615
孤街浪徒
孤街浪徒 2021-01-20 17:39

I have created a signup form using wtforms. I am using FormField in it so that I don\'t have to repeat some of the elements of the form again. But whenever I click on the Su

2条回答
  •  生来不讨喜
    2021-01-20 18:24

    I solved my problem with the following function:

    def __init__(self, *args, **kwargs):
        kwargs['csrf_enabled'] = False
        super(ProfileInfoForm, self).__init__(*args, **kwargs)
    

    I added this function in ProfileInfoForm()

    The issue was FormField includes csrf_token field as well as Actual form, i.e., RegistrationForm was also including csrf_token, so there were two csrf_token which were to be verified and only one was getting rendered actually in form. So, I disabled csrf_token in ProfileInfoForm so when FormField rendered it, it had csrf_token = False.

    And RegistrationForm does have csrf_token enabled still now so the form is still safe.

    My Guess is this does also required to be done in FormField as well.

    FYI: This solution might be wrong due to my interpretation of the FormField code. SO please correct me if I am wrong in above solution.

提交回复
热议问题