Cannot Create Database
I am admittedly extremely new to Python and coding in general, however, I was following along with this tutorial a day ago and everything was going well up until it came time to create the database.
My code is as follows:
from flask import Flask from flask_sqlalchemy import SQLAlchemy from os import path'
db = SQLAlchemy() DB_NAME = "database.db"
def create_app(): app = Flask(name) app.config['SECRET_KEY'] = 'domine' app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}' db.init_app(app)
from .views import views
from .auth import auth
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')
from .models import User, Note
with app.app_context():
db.create_all()
return app
def create_database(app): if not path.exists('website/' + DB_NAME): db.create_all(app=app) print('Created Database!')
I have looked over stack overflow and tried a few things here and there, however, I just seem to run into more errors. At one point what seemed like a database was created in the instance folder of my project, however, it was not named what was expected and I have since been unable to replicate its creation.
What is the error you receive? Also please post more code, may it's due to other pages
The error that I get after attempting to use the sign up page to create a new user is: NameError
NameError: name 'db' is not defined
My code is as follows:
init.py is:
`from flask import Flask from flask_sqlalchemy import SQLAlchemy from os import path
db = SQLAlchemy() DB_NAME = "database.db"
def create_app(): app = Flask(name) app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs' app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}' db.init_app(app)
from .views import views
from .auth import auth
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')
from .models import User, Note
with app.app_context():
db.create_all()
return app
def create_database(app): if not path.exists('website/' + DB_NAME): db.create_all(app=app) print('Created Database!')`
auth.py is:
`from flask import Blueprint, render_template, request, flash, redirect, url_for from .models import User from werkzeug.security import generate_password_hash, check_password_hash
auth = Blueprint('auth', name)
@auth.route('/login', methods=['GET', 'POST']) def login(): return render_template("login.html", boolean=True)
@auth.route('/logout') def logout(): return "
logout
"
@auth.route('/sign-up', methods=['GET', 'POST']) def sign_up(): if request.method == 'POST': email = request.form.get('email') first_name = request.form.get('firstName') password1 = request.form.get('password1') password2 = request.form.get('password2')
if len(email) < 4:
flash('Your email must be greater than 3 characters!', category='error')
elif len(first_name) < 2:
flash('Your first name must be greater than 1 character!', category='error')
elif password1 != password2:
flash('The passwords do not match!', category='error')
elif len(password1) < 7:
flash('Your password must be at least 7 characters!', category="error")
else:
new_user = User(email=email, first_name=first_name, password=generate_password_hash(password1, method='pbkdf2:sha256'))
db.session.add(new_user)
db.session.commit()
flash('Account created successfully!', category="success")
return redirect(url_for('views.home'))
return render_template("sign_up.html")`
models.py is:
`from . import db from flask_login import UserMixin from sqlalchemy.sql import func
class Note(db.Model): id = db.Column(db.Integer, primary_key=True) data = db.Column(db.String(10000)) date = db.Column(db.DateTime(timezone=True), default=func.now()) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(150), unique=True) password = db.Column(db.String(150)) first_name = db.Column(db.String(150)) notes = db.relationship('Note')`
views.py is:
`from flask import Blueprint, render_template
views = Blueprint('views', name)
@views.route('/') def home(): return render_template("home.html")`
main.py is:
`from website import create_app
if name == 'main': app = create_app() app.run(debug=True)`