Question

I am following http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982 tutorial to develop my own application.

I tried to create a signupform but encountered this error:

TypeError: 'Required' object is not iterable 

My routes page is

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)         

This is my models.py code

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)            

This is my forms.py code

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    

Can any one explain where the error is?

Was it helpful?

Solution

You are supposed to pass a list of validators, but you are missing your [ ... ] in the declaration of the birthdate field in SignupForm. It should read:

birthdate = DateField("Birthdate",
    [validators.Required("Please Enter your birthdate")])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top