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

Why does my file not create a database? (Python/flask/sqlalchemy

from flask_migrate import Migrate

from flask import Flask, render_template,url_for,redirect import os from flask_sqlalchemy import SQLAlchemy from formss import AddPup, DelPup

from tables import Puppy, Owner

app = Flask(name) basedir = os.path.abspath(os.path.dirname(file)) app.config["SECRET_KEY"] = "mykey" app.config["SQLAlCHEMY_DATABASE_URI"] = 'sqlite:///site.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app)

class Puppy(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text)

def __init__(self, name):
    self.name = name

def __repr__(self):
    return f"{self.name}"

class Owner(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text)

def __init__(self, name):
    self.name = name

db.create_all() db.session.commit()

@app.route("/") def home(): return render_template("home.html")

@app.route("/add", methods=["GET", "POST"]) def add_pup(): form = AddPup()

if form.validate_on_submit():
    name = form.name.data
    new_pup = Puppy(name)
    db.session.add(new_pup)
    db.session.commit()

    return redirect(url_for("pup_list"))

return render_template("add_puppy.html", form=form)

@app.route("/delete", methods=["GET", "POST"]) def del_pup(): form = DelPup()

if form.validate_on_submit():
    pup = form.id.data
    dead_pup = Puppy.query.get(pup)
    db.session.delete(dead_pup)
    db.session.commit()

    return redirect(url_for("pup_list"))

return render_template("delete.html", form=form)

@app.route("/list") def pup_list():

all_puppies = Puppy.query.all()

return render_template("list.html", all_puppies=all_puppies)

if name == 'main': app.run(debug=True, port=8000)