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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Sam Dale](https://uploads.teamtreehouse.com/production/profile-photos/5190772/micro_1403083_511967942244331_1667001213_o_1024.jpg)
Sam Dale
6,136 PointsModel Initialization Error
Hi there. I definitely struggled in learning Flask, so there's probably a simple solution to my problem, but for my tacocat code, I'm getting the error
File "tacocat.py", line 96, in <module>
models.initialize()
File "/Tacocat/models.py", line 49, in initialize
DATABASE.create_tables([User, Taco], safe = True)
(Peewee errors)
AttributeError: 'function' object has no attribute '_meta'
models.py:
import datetime
from flask.ext.bcrypt import generate_password_hash
from flask.ext.login import UserMixin
from peewee import *
DATABASE = SqliteDatabase("tacocat.db")
class User(UserMixin, Model):
email = CharField(unique = True)
password = CharField(max_length = 100)
class Meta:
database = DATABASE
@classmethod
def create_user(cls, email, password):
try:
with DATABASE.transaction():
cls.create(
email = email,
password = generate_password_hash(password)
)
except IntegrityError:
raise ValueError("User already exists.")
def Taco(Model):
protein = CharField()
shell = CharField()
cheese = BooleanField()
extras = TextField()
timestamp = DateTimeField(default = datetime.datetime.now)
user = ForeignKeyField(
rel_model = User,
related_name = "tacos"
)
class Meta:
database = DATABASE
def initialize():
DATABASE.connect()
DATABASE.create_tables([User, Taco], safe = True)
DATABASE.close()
and my rough draft of tacocat.py:
from flask import (Flask, g, render_template, flash, redirect, url_for,
abort)
from flask.ext.bcrypt import check_password_hash
from flask.ext.login import (LoginManager, login_user, logout_user,
login_required, current_user)
import forms
import models
DEBUG = True
PORT = 8000
HOST = "0.0.0.0"
app = Flask(__name__)
app.secret_key = "asdfkaljf3j2-3$ADGa@#5AF#$TJ(KJ(L:$%:JA))"
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.user = current_user
@app.after_request
def after_request(response):
"""Close the database 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(
email = form.email.data,
password = form.password.data
)
return redirect(url_for("index"))
return render_template("register.html", form = form)
@app.route("/login", methods=("GET", "POST"))
def login():
form = forms.LoginForm()
if form.validate_on_submit():
try:
user = models.User.get(models.User.email == form.email.data)
except models.DoesNotExist:
flash("Your email or password doesn't match!", "error")
else:
if check_password_hash(user.password, form.password.data):
login_user(user)
flash("You've been logged in!", "success")
return redirect(url_for("index"))
else:
flash("Your email or password doesn't match!", "error")
return render_template("login.html", form=form)
@app.route("/logout")
@login_required
def logout():
logout_user()
flash("You've been logged out! Come back soon!", "success")
return redirect(url_for("index"))
@app.route("/")
def index():
stream = models.Post.select().limit(100)
return render_template("stream.html", stream = stream)
@app.errorhandler(404)
def not_found(error):
return render_template("404.html"), 404
if __name__ == "__main__":
models.initialize()
try:
models.User.create_user(
email = "sam@example.com",
password = "password"
)
except ValueError:
pass
app.run(debug = DEBUG, host = HOST, port = PORT)
Cheers!
1 Answer
![Nathan Tallack](https://uploads.teamtreehouse.com/production/profile-photos/4003052/micro_NathanBusinessEditedSquare.jpg)
Nathan Tallack
22,160 PointsAgain, close.
You want to change def to class for your Taco model. ;)
Let me know the next bug you hit. :)
Sam Dale
6,136 PointsSam Dale
6,136 PointsOh my god. Thank you. I think this is a sign that I probably should have gone to sleep earlier.