Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask Takin' Names Macros

Program does not run locally

when I ran the program on the local machine, I get TypeError: argument of type '_RequestGlobals' is not iterable App.py `from flask import (Flask, g, render_template, flash, redirect, url_for) from flask.ext.login import LoginManager import forms import models

DEBUG = True PORT = 8000 HOST = '0.0.0.0'

app = Flask(name) app.secret_key = 'njgnbfdng!2fkgj9856&!jmbk[pln]wrtj,.kydaSHAPA'

login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'login'

@login_manager.user_loader def load_user(userid): try: return models.User.get(models.User.id==userid) except models.DoesNotExist: return None

@app.before_request def before_request(): """Connect to the database before each request. """ g.db = models.DATABASE g.db.connect() # g.db = models.DATABASE.connect()

@app.after_request def after_request(response): """Close to the database after each request. """ g.db.close() return response

@app.route('/register', methods=('GET', 'POST')) def register(): form = forms.RegisterForm() if form.validate_on_submit(): flash("You registered!", 'success') models.User.create_user( username=form.username.data, email=form.email.data, password=form.password.data ) return redirect(url_for('index')) return render_template('register.html', form=form)

@app.route('/') def index(): return 'Hey'

if name == 'main': models.initiatize() try: models.User.create_user( username="MoatazBellah", email="moatazbellah@gmail.com", password='123456', admin=True, bio='I am software engineer' ) except ValueError: pass app.run(debug=DEBUG, host=HOST, port=PORT) ` forms.py

`from flask_wtf import Form from wtforms import StringField, PasswordField from wtforms.validators import (DataRequired, Regexp, ValidationError, Email, Length, EqualTo) from models import User

Function to check if the name already in exists

def name_exists(form, field): if User.select().where(User.username == field.data).exists(): raise ValidationError('User already exists.')

Function to check if the email already in exists

def email_exists(form, field): if User.select().where(User.email == field.data).exists(): raise ValidationError('User already exists.')

Create the Registeration class that contaoins username, email, password, password2

class RegisterForm(Form): username = StringField( 'Username', validators=[ DataRequired(), Regexp( r'^[a-zA-Z0-9_]+$', message='''Username should be one word, letters, numbers, and underscores only''' ), name_exists ]) email = StringField( 'Email', validators=[ DataRequired(), Email(), email_exists ]) password = PasswordField( 'Password', validators=[ DataRequired(), Length(min=6), EqualTo('password2', message="Password must match") ]) password2 = PasswordField( 'Confirm Password', validators=[DataRequired()] ) `

Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in call return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/vagrant/Treehouse/social/app.py", line 43, in register form = forms.RegisterForm() File "/home/vagrant/.local/lib/python2.7/site-packages/wtforms/form.py", line 212, in call return type.call(cls, *args, **kwargs) File "/home/vagrant/.local/lib/python2.7/site-packages/flask_wtf/form.py", line 158, in init super(Form, self).init(*args, **kwargs) File "/home/vagrant/.local/lib/python2.7/site-packages/flask_wtf/form.py", line 88, in init super(FlaskForm, self).init(formdata=formdata, **kwargs) File "/home/vagrant/.local/lib/python2.7/site-packages/wtforms/form.py", line 278, in init self.process(formdata, obj, data=data, **kwargs) File "/home/vagrant/.local/lib/python2.7/site-packages/wtforms/form.py", line 132, in process field.process(formdata) File "/home/vagrant/.local/lib/python2.7/site-packages/wtforms/csrf/core.py", line 43, in process self.current_token = self.csrf_impl.generate_csrf_token(self) File "/home/vagrant/.local/lib/python2.7/site-packages/flask_wtf/csrf.py", line 134, in generate_csrf_token token_key=self.meta.csrf_field_name File "/home/vagrant/.local/lib/python2.7/site-packages/flask_wtf/csrf.py", line 42, in generate_csrf if field_name not in g: File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 365, in <lambda> contains = lambda x, i: i in x._get_current_object() TypeError: argument of type '_RequestGlobals' is not iterable