AttributeError: 'NoneType' object has no attribute 'cursor'

Hi everyone can you help me with this error

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/flask/", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/site-packages/flask/", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/marcellnugraha/mysite/", line 107, in signup
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
AttributeError: 'NoneType' object has no attribute 'cursor'

[edit by admin: formatting]

Where is the mysql.connection object being set up by your code? The error message is telling you that it is set to None.

the code is like this

app = Flask(__name__)

app.config['MYSQL_HOST'] = ''
app.config['MYSQL_USER'] = 'marcellnugraha'
app.config['MYSQL_PASSWORD'] = 'cell20022001'
app.config['MYSQL_DB'] = 'marcellnugraha$cnn'

mysql = MySQL(app)

@app.route('/signup', methods =['GET', 'POST'])
def signup():
    msg = ''
    if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form :
        username = request.form['username']
        password = request.form['password']
        email = request.form['email']

        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM user WHERE username = % s OR email = % s', (username,
                                                                                  email, ))
        user = cursor.fetchone()
        if user:
            msg = 'Account already exists !'
        elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
            msg = 'Invalid email address !'
        elif not re.match(r'[A-Za-z0-9]+', username):
            msg = 'Username must contain only characters and numbers !'
        elif not username or not password or not email:
            msg = 'Please fill out the form !'
            totalUser = getTotalUser()
            user_id = 'U'+ str(totalUser)

            cursor.execute('INSERT INTO user VALUES (% s, % s, % s, % s)', (user_id, username, email, password, ))

            listAngkringan = getListAngkringan()
            for i in range(len(listAngkringan)):
                cursor.execute('INSERT INTO rating VALUES (% s, 0, % s)', (user_id, listAngkringan[i]['angkringan_id'], ))

            msg = 'Account registered, please login!'
            return render_template('login.html', msg = msg)

        msg = 'Please fill out the form !'
    return render_template('login.html', msg = msg)

Am I doing it right?

You need to check first what is the state of the connection and see why it is unsuccessful. According to None indicates that.