'forms.ContactForm object' has no attribute 'hidden_tag'

前端 未结 4 2135
孤街浪徒
孤街浪徒 2021-02-13 05:53

I am trying to create a contact form using flask but keep getting this error when the page is rendered.

\'forms.ContactForm object\' has no attribute \'hidden_         


        
4条回答
  •  甜味超标
    2021-02-13 06:15

    It took me some time to fix this.

    First import Form, fields, bootstrap as:

    from flask_wtf import Form
    from wtforms import StringField #etc
    from flask_bootstrap import Bootstrap
    

    Config secret key and bootstrap

    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'secret key'
    Bootstrap(app)
    

    create your form as your used to:

    class ContactForm(Form):
      name = TextField("Name", [validators.Required()])
      email = TextField("Email",[validators.Required(), validators.email()])
      subject = TextField("Subject", [validators.Required()])
      message = TextAreaField("Message", [validators.Required()])
      submit = SubmitField("Send")
    

    Nothing special in the routing aswell, just return it normaly.

    In the html:

    {% extends "bootstrap/base.html" %}
    {% import "bootstrap/wtf.html" as wtf %}
        {% if form %}
            {{ wtf.quick_form(form, ) }}
        {% endif %}
    

    And that's it. Hope you find some (or all) of it useful.

提交回复
热议问题