错误绑定参数0 - 可能不支持的类型。 u'INSERT INTO“联系人”

时间:2014-12-11 23:11:20

标签: flask sqlalchemy flask-sqlalchemy flask-wtforms

  

我在contact.html中有一个wtform,想要显示用户在info.html中输入的数据   当我按提交重定向到info.html   getError:(InterfaceError)绑定参数0的错误 - 可能是不支持的类型。你插入"联系人" (姓名,电子邮件,主题,消息)VALUES(?,?,?,?)'

  @app.route('/contact', methods=['GET', 'POST'])
  def contact():
  form = LoginForm()
     if request.method == 'POST':
      if form.validate()== True:
      newcontact = Contacts(name=form.name,
                                 email=form.email,
                                subject=form.subject,
                                message=form.message)

        db.session.add(newcontact)
        db.session.commit()
        return redirect(flask.url_for('info')) 

    else: 
     #If the form does not have all fields that are required 
        flash('All fields are required.') 
 return render_template('contact.html', form=form)

@app.route('/info', methods=['GET', 'POST'])
def info():
  form = LoginForm()
  #fetch the first 
  contactinfo = Contacts.query.first()
  #Populate the form Not sure about this part?? 

  form.name = contactinfo.name 
  form.email = contactinfo.email
  form.subject = contactinfo.subject
  form.message = contactinfo.message
  #returns the html page, along with the form        
  return render_template('info.html', form=form)

 if __name__ == '__main__':
 app.run(debug=True)   
  

form.py

 class LoginForm(Form):
     name = StringField("Name", [validators.Required("Please enter your name.")])
     email = StringField("Email", 
     [validators.Required("Please enter your email  address."),
     validators.Email("Please enter valid email address.")])
     subject = StringField("Subject", 
     [validators.Required("Please enter your subject.")])
     message = StringField("Message", 
     [validators.Required("Please enter your  message.")])
     submit = SubmitField("Submit")
  

model.py

     class Contacts(db.Model):
     __tablename__ = "Contacts"
     id = db.Column(db.Integer, primary_key = True)
     name = db.Column(db.String(50))
     email = db.Column(db.String(50))
     subject = db.Column(db.String(50))
     message = db.Column(db.String(50))
  

在info.html

     {% extends "layout.html" %}
     {% block content %}
       <h2>show the info</h2>
      {% for entry in form %}
         name: {{ entry.name}} <br>
         email: {{ entry.email }} <br>
         subject{{ entry.subject }} <br>
         messaget {{ entry.message }} <br>
        <br>
        {% endfor %}
        {% endblock %}

1 个答案:

答案 0 :(得分:2)

您要将表单字段而不是其值分配给模型字段。该值存储在每个字段的data属性中。

newcontact = Contacts(
    name=form.name.data,
    email=form.email.data,
    subject=form.subject.data,
    message=form.message.data)