I am working on app and did registration page, conencting to db and adding new user. Using SQL and not SQLalchemy. Now I am getting an error when I press register.
Register route in views:
import sqlite3
from functools import wraps
from flask import Flask, flash, redirect, render_template, request, session, url_for, g
from forms import AddTaskForm, RegisterForm, LoginForm
# Config
app = Flask(__name__)
app.config.from_object("_config")
# Helper functions
def connect_db():
return sqlite3.connect(app.config["DATABASE_PATH"])
@app.route("/register/", methods=["GET", "POST"])
def register():
form = RegisterForm(request.form)
if request.method == "POST" and form.validate_on_submit():
name = request.form["name"]
email = request.form["email"]
password = request.form["password"]
g.db.connect_db()
g.db.execute("INSERT INTO users(name, email, password) VALUES (?,?,?)", (name, email, password))
g.db.commit()
g.db.close()
return render_template("register.html", form=form)
Config:
import os
#Grab the folder where this script lives
basedir = os.path.abspath(os.path.dirname(__file__))
DATABASE = "flasktaskr.db"
WTF_CSRF_ENABLED = True
SECRET_KEY = "sjfdoifj948uf98jf9349f2kjiu78z7823"
# Define the full path for the database
DATABASE_PATH = os.path.join(basedir, DATABASE)
And my register form with WTForms looks like this:
from flask_wtf import Form
from wtforms import StringField, DateField, IntegerField, SelectField, PasswordField
from wtforms.validators import DataRequired, Length, EqualTo
class RegisterForm(Form):
name = StringField(
'Username',
validators=[DataRequired(), Length(min=4, max=25)]
)
email = StringField(
'Email',
validators=[DataRequired(), Length(min=6, max=40)]
)
password = PasswordField(
'Password',
validators=[DataRequired(), Length(min=6, max=40)])
confirm = PasswordField(
'Repeat Password',
validators=[DataRequired(), EqualTo('password', message='Passwords must match')]
)
And finally how I made db if that helps:
import sqlite3
from _config import DATABASE_PATH
with sqlite3.connect(DATABASE_PATH) as connection:
c = connection.cursor()
c.execute("""CREATE TABLE tasks(task_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, notes TEXT NOT NULL, due_date TEXT NOT NULL, priority INTEGER NOT NULL,
status INTEGER NOT NULL)""")
c.execute("""CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE, email TEXT NOT NULL UNIQUE, password TEXT NOT NULL)""")
I hope I posted enough code to find error: Again the error I get is:
AttributeError: '_AppCtxGlobals' object has no attribute 'db'
It weird to me because other functions like logging in and adding some tasks to database work just fine. Appreciate any help.
Thanks
You haven't done anything at all to associate your database connection with the global g
object. connect_db
is a standalone function, and returns the connection itself. So your code needs to be:
db = connect_db()
db.execute("INSERT INTO users(name, email, password) VALUES (?,?,?)", (name, email, password))
db.commit()
db.close()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments