AttributeError: "NoneType" object has no attribute "email"

  flask, python

I followed a tutorial, and I got this error. Since nobody else seems to have problems with it, can somebody here help me with it?

Traceback (most recent call last):
  File "C:UsersKentzPycharmProjectsSeliisiaVC3.0venvLibsite-packagesflaskapp.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:UsersKentzPycharmProjectsSeliisiaVC3.0venvLibsite-packagesflaskapp.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "C:UsersKentzPycharmProjectsSeliisiaVC3.0venvLibsite-packagesflaskapp.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:UsersKentzPycharmProjectsSeliisiaVC3.0venvLibsite-packagesflaskapp.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:UsersKentzPycharmProjectsSeliisiaVC3.0venvLibsite-packagesflaskapp.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:UsersKentzPycharmProjectsSeliisiaVC3.0venvLibsite-packagesflaskapp.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:UsersKentzPycharmProjectsSeliisiaVC3.0NewWeb.py", line 75, in user
    found_user.email = email
AttributeError: 'NoneType' object has no attribute 'email'

Also here is the code if it’s needed:

from flask import *
from datetime import *
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.secret_key = "Sälgu"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///users.sqlite3"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.permanent_session_lifetime = timedelta(hours=3)

db = SQLAlchemy(app)

class users(db.Model):
    _id = db.Column("id", db.Integer, primary_key = True)
    name = db.Column(db.String(100))
    email = db.Column(db.String(100))

    def __init__(self, name, email):
        self.name = name
        self.email = email

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/view")
def view():
    return render_template("view.html", values= users.query.all())

@app.route("/asjad/")
def asjad():
    return render_template("asjad.html")

@app.route("/kokandus/")
def asjad2():
    return render_template("kokandus.html")

@app.route("/login", methods=["POST", "GET"])
def login():
    if request.method == "POST":
        session.permanent = True         #<>
        user = request.form["nm"]
        session["user"] = user

        found_user = users.query.filter_by(name=user).first()
        if found_user:
            session["email"] = found_user.email
        else:
            usr = users(user, None)
            db.session.add(usr)
            db.session.commit()

        flash("Logisid Sisse!")
        return redirect(url_for("user", usr=user))
    else:
        if "user" in session:
            flash("Juba Sisse Logitud Sisse!")
            return  redirect(url_for("user"))

        return render_template("login.html")

@app.route("/user", methods=["POST", "GET"])
def user():
    email = None
    if "user" in session:
        user = session["user"]

        if request.method == "POST":
            email = request.form["email"]
            session["email"] = email

            found_user = users.query.filter_by(name=user).first()
            found_user.email = email
            db.session.commit()

            flash("Email Salvestatud!")
        else:
            if "email" in session:
                email = session["email"]

        return render_template("user.html", email=email)
    else:
        flash("Sa ei ole sisse loginud!")
        return  redirect(url_for("login"))

@app.route("/logout")
def logout():
    flash("Logisid Välja!", "info")
    session.pop("user", None)
    session.pop("email", None)
    return redirect(url_for("login"))


if __name__ == "__main__":
    db.create_all()
    db.session.commit()
    app.run(debug = True)

Source: Python Questions

LEAVE A COMMENT