D Wojciechowski
D Wojciechowski

Reputation: 21

Flask current_user is None type

My code that was previously working is now causing my main flask app to not run.

The error is coming from my forms.py file.

class selectClass(FlaskForm):

    a = current_user.database
    conn = sqlite3.connect("C:\\Users\\Lenovo\\PycharmProjects\\spacedonline\\"+a)
    c = conn.cursor()
    c.execute("SELECT Class FROM Students ")

    data = c.fetchall()

    listofclasses = []
    for clas in data:
        if clas[0] not in listofclasses:
            listofclasses.append(clas[0])

    finallist = []
    for clas in listofclasses:
        finallist.append((clas, clas))

    nameofclass=SelectField(u"Name of Class", choices=finallist)
    submit= SubmitField("Select")

On trying to launch the main.py file I get the message:

Traceback (most recent call last):
  File "C:/Users/Lenovo/PycharmProjects/spacedonline/forms.py", line 102, in <module>
    class selectClass(FlaskForm):
  File "C:/Users/Lenovo/PycharmProjects/spacedonline/forms.py", line 104, in selectClass
    a = current_user.database
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\werkzeug\local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'NoneType' object has no attribute 'database'

As I said, it was not returning this error before, I am at a loss.

Upvotes: 0

Views: 1514

Answers (3)

NicolasSens
NicolasSens

Reputation: 325

I've had the same issue, and this was actually due to the security keys.

I have set different app security keys and it works now.

app.config['SECRET_KEY'] = 'new key 1'
app.config['SECURITY_PASSWORD_SALT'] = 'new key 2' 

It is probably due to a security control that fails when creating a new instance.

Upvotes: 0

D Wojciechowski
D Wojciechowski

Reputation: 21

I have been logged in and when the problem code is commented out it, my page shows me as logged on.

I have worked around the problem by creating a function which creates the class: '''

def selectclassform():
        class SelectClass(FlaskForm):


            a = current_user.database
            conn = sqlite3.connect("C:\\Users\\Lenovo\\PycharmProjects\\spacedonline\\"+a)
            c = conn.cursor()
            c.execute("SELECT Class FROM Students ")

            data = c.fetchall()

            listofclasses = []
            for clas in data:
                if clas[0] not in listofclasses:
                    listofclasses.append(clas[0])

            finallist = []
            for clas in listofclasses:
                finallist.append((clas, clas))

            nameofclass=SelectField(u"Name of Class", choices=finallist)
            submit= SubmitField("Select")

        return (SelectClass)
'''

And then calling the function in the main apps.py file:

'''

@app.route("/select", methods=["GET", "POST"])
def selectclass():
    if current_user.is_authenticated:
        form = selectclassform()()
        print(form)
        if form.validate_on_submit():
            print("valid")
            session ["nameofclass"]=form.nameofclass.data
            #return printtable(form.nameofclass.data, current_user.database)
            return redirect(url_for("validate"))
        else:
            print("bye")
        return render_template("select.html", form=form)
    else:
        return redirect(url_for("login"))

'''

Upvotes: 0

GAEfan
GAEfan

Reputation: 11360

you are probably not logged in. so current_user is NoneType. Try:

if current_user: # or current_user.is_active:
    a = current_user.database
    ...
else:
    return redirect('/login')

Upvotes: 1

Related Questions