TypeError:'必需'对象不是可迭代的Flask WTF Forms

时间:2014-03-10 20:15:37

标签: python flask flask-sqlalchemy flask-wtforms flask-login

我正在关注http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982教程以开发我自己的应用程序。

我尝试创建一个注册表但遇到了这个错误:

TypeError: 'Required' object is not iterable 

我的路线页面是

from cafe_klatch import app
from flask import render_template,request,flash
from forms import ContactForm, SignupForm
from models import db


@app.route('/')
@app.route('/index')

def index():
    return render_template('index.html')

@app.route('/contact', methods = ['GET','POST'])

def contact():
    form1 = ContactForm()

    if request.method == 'POST':
        if form.validate() == False:
            flash('All fields are required.')
            return render_template('contact.html',form = form)
        else:
            return 'Form posted.'    
    elif request.method == 'GET':
        return render_template('contact.html',form = form)  


@app.route('/signup', methods =['GET','POST'])

def signup():
    form = SignupForm() 

    if request.method == 'POST':
        if form.validate() == False:
            return render_template('signup.html',form = form)
        else:
            return "[1] Create a new user [2] Sign in the user [3] redirect to the user's profile" 

    elif request.method == 'GET':
        return render_template('signup.html',form = form)         

这是我的models.py代码

from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash

db = SQLAlchemy()

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key = True)
    userid = db.Column(db.String(30), unique = True)
    email = db.Column(db.String(100), unique = True)
    pwdhash = db.Column(db.String(100))
    birthdate = db.Column(db.DateTime)
    zipcode = db.Column(db.Integer)
    country = db.Column(db.String(100))

    def __init__(self,userid,email,password,birthdate,zipcode,country):
        self.userid = userid.title()
        self.email = email.lower()
        self.set_password(password)
        self.birthdate = birthdate
        self.zipcode = zipcode
        self.country = country

    def __repr__(self):
            return '<User %r>' % self.username    

    def set_password(self,password):
        self.pwdhash = generate_password_hash(password)

    def check_password(self,password):
        return check_password(self.pwdhash, password)            

这是我的forms.py代码

from flask.ext.wtf import Form
from wtforms import TextField, TextAreaField, SubmitField, IntegerField, validators, ValidationError,PasswordField, DateField
from models import db, User 
class ContactForm(Form):
  name = TextField("Name",  [validators.Required("Please enter your name.")])
  email = TextField("Email",  [validators.Required("Please enter your email address."), validators.Email("Please enter your email address.")])
  subject = TextField("Subject",  [validators.Required("Please enter a subject.")])
  message = TextAreaField("Message",  [validators.Required("Please enter a message.")])
  submit = SubmitField("Send")

class SignupForm(Form):
    userid = TextField("userid", [validators.Required("Please enter your user id")])
    email = TextField("Email", [validators.Required("Please enter your email address."), validators.Email("Please enter your email address.")])
    password = PasswordField('Password', [validators.Required("Please enter a password.")])
    birthdate = DateField("Birthdate",validators.Required("Please Enter your birthdate"))
    zipcode =  IntegerField("Zipcode",[validators.Required("Please Enter your zipcode")])
    country = TextField("country", [validators.Required("Please enter your country")])
    submit = SubmitField("Create account")

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)

    def validate(self):
        if not Form.validate(self):
            return False

        user = User.query.filter_by(email = self.email.data.lower()).first()
        if user:
          self.email.errors.append("That email is already taken")
          return False
        else:
          return True    

任何人都可以解释错误的位置吗?

1 个答案:

答案 0 :(得分:9)

您应该传递验证码列表,但在[的{​​{1}}字段声明中,您遗漏了] ... birthdate。它应该是:

SignupForm