Flask - AttributeError: '_AppCtxGlobals' object has no attribute 'db'

Žan Fras

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

Daniel Roseman

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask

From Dev

Flask AttributeError: 'NoneType' object has no attribute 'split'

From Dev

Flask - AttributeError: 'module' object has no attribute 'items'

From Dev

AttributeError: object has no attribute

From Dev

AttributeError: '' object has no attribute ''

From Dev

AttributeError: '_Option' object has no attribute '_sa_instance_state' in flask

From Dev

Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active'

From Dev

AttributeError: 'tuple' object has no attribute 'drivername' using Flask SqlAlchemy

From Dev

AttributeError: 'Flask' object has no attribute 'user_options'

From Dev

Flask Blueprint AttributeError: 'module' object has no attribute 'name' error

From Dev

Flask-login AttributeError: 'User' object has no attribute 'is_active'

From Dev

flask-mail AttributeError: 'function' object has no attribute 'send'

From Dev

AttributeError: 'tuple' object has no attribute 'strip' : Flask Markdown?

From Dev

Flask: AttributeError: 'UnboundField' object has no attribute '__call__'?

From Dev

AttributeError: 'tuple' object has no attribute 'strip' : Flask Markdown?

From Dev

Flask error: AttributeError: 'NoneType' object has no attribute 'startswith'

From Dev

Python Flask: AttributeError: 'NoneType' object has no attribute 'count'

From Dev

Flask AttributeError: 'Blueprint' object has no attribute 'response_class'

From Dev

AttributeError: object has no attribute 'tk'

From Dev

AttributeError: 'module' object has no attribute

From Dev

AttributeError: 'DataFrame' object has no attribute

From Dev

AttributeError: 'module' object has no attribute

From Dev

AttributeError: Object has no attribute 'listbox

From Dev

AttributeError: 'module' object has no attribute

From Dev

AttributeError: 'Namespace' object has no attribute

From Dev

AttributeError: object has no attribute 'title'

From Dev

AttributeError: object has no attribute 'split'

From Dev

AttributeError: object has no attribute rect

From Dev

AttributeError: 'str' object has no attribute

Related Related

  1. 1

    AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask

  2. 2

    Flask AttributeError: 'NoneType' object has no attribute 'split'

  3. 3

    Flask - AttributeError: 'module' object has no attribute 'items'

  4. 4

    AttributeError: object has no attribute

  5. 5

    AttributeError: '' object has no attribute ''

  6. 6

    AttributeError: '_Option' object has no attribute '_sa_instance_state' in flask

  7. 7

    Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active'

  8. 8

    AttributeError: 'tuple' object has no attribute 'drivername' using Flask SqlAlchemy

  9. 9

    AttributeError: 'Flask' object has no attribute 'user_options'

  10. 10

    Flask Blueprint AttributeError: 'module' object has no attribute 'name' error

  11. 11

    Flask-login AttributeError: 'User' object has no attribute 'is_active'

  12. 12

    flask-mail AttributeError: 'function' object has no attribute 'send'

  13. 13

    AttributeError: 'tuple' object has no attribute 'strip' : Flask Markdown?

  14. 14

    Flask: AttributeError: 'UnboundField' object has no attribute '__call__'?

  15. 15

    AttributeError: 'tuple' object has no attribute 'strip' : Flask Markdown?

  16. 16

    Flask error: AttributeError: 'NoneType' object has no attribute 'startswith'

  17. 17

    Python Flask: AttributeError: 'NoneType' object has no attribute 'count'

  18. 18

    Flask AttributeError: 'Blueprint' object has no attribute 'response_class'

  19. 19

    AttributeError: object has no attribute 'tk'

  20. 20

    AttributeError: 'module' object has no attribute

  21. 21

    AttributeError: 'DataFrame' object has no attribute

  22. 22

    AttributeError: 'module' object has no attribute

  23. 23

    AttributeError: Object has no attribute 'listbox

  24. 24

    AttributeError: 'module' object has no attribute

  25. 25

    AttributeError: 'Namespace' object has no attribute

  26. 26

    AttributeError: object has no attribute 'title'

  27. 27

    AttributeError: object has no attribute 'split'

  28. 28

    AttributeError: object has no attribute rect

  29. 29

    AttributeError: 'str' object has no attribute

HotTag

Archive