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
Josh Keenan
20,315 PointsFlask social network, Register form error
Code is copied directly from Kenneth and has been rewritten 5 times and I borrowed a friends code and still got the exact same error whereas it ran for him.... Tech support say it's a bug in my code... help!
Error: AttributeError: 'module' object has no attribute 'RegisterForm' File "/home/treehouse/workspace/app.py", line 42, in register form = forms.RegisterForm()
code:
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 = 'aiurbg uogbwg lar h09312 SJDF DH GF497y uhg93 th3 5g35g0u3g595jg83'
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()
@app.after_request
def after_request(response):
"""Close the databse connection 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("Yay, 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 'Wagwan!'
if __name__ == '__main__':
models.initialize()
try:
models.User.create_user(
username='joshkeenan',
email='example@example.com',
password='password',
admin=True)
except ValueError:
pass
app.run(debug=True, host='0.0.0.0', port=8000)
[MOD: added ```python formatting -cf]
4 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsFocusing on the error message:
Error: AttributeError: 'module' object has no attribute 'RegisterForm' File "/home/treehouse/workspace/app.py", line 42, in register form = forms.RegisterForm()
This is saying the python interpreter can not find the class RegisterForm in the module forms. Since you haven't posted your forms.py file, I'll venture to guess there is a typo or misspelling of the class in the forms.py file.
I looked my code based on Kenneth's videos. The name of the form in my file is RegistrationForm not RegisterForm.
If this isn't the problem, please post your models.py and forms.py files to further debug.
Josh Keenan
20,315 PointsIt was function name in forms.py, not RegisterForm... Registerfor.... it took me 6 hours and 3 rewrites to realise I kept using a lowercased 'f'...
Josh Keenan
20,315 PointsThanks for pointing me in the right direction, I figured it out. Thank you for helping me end a stressful day.
Chris Freeman
Treehouse Moderator 68,468 PointsGlad to hear. What turned out to be the issue?
Josh Roberts
9,930 PointsI had the same problem. Chris Freeman made me realize that we wrote forms and it wasn't build in. Once I looked at forms.py it was clear that when we wrote that file we used "class Registration(form)" and then in app.py tried to call "forms.RegisterForm()" instead. I then switched it to "forms.Registration()" and it is now working correctly.