Search code examples
python-3.xflaskflask-sqlalchemywerkzeug

Flasksqlalchemy 'BaseQuery' object has no attribute


I'm following this tutorial from tech with tim about flask so I could understand some of the basics of flask, everything was going ok until I tried to log in a user and it gave a Nonetype error, I don't know what I did but then changing variable names it gave me a BaseQuery has no attribute 'password', I've been changing everything I could think about from the db model file and the auth file and also the login html file but it didn't work, this are the files I think are the problem:

from auth.py:

from flask import Blueprint, render_template, request, flash, redirect, url_for
from . import db
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash

auth = Blueprint('auth', __name__)

@auth.route('/login', methods=["GET", "POST"])
def login():
    if request.method == "POST":
        email_request = request.form.get("email_request")
        password_request = request.form.get("password")

        user = User.query.filter_by(email=email_request)
        print(user)
        if user:
            if check_password_hash(user.password, password_request):
                flash("Logged in successfully", category="success")
            else:
                flash("Incorrect password", category="error")

        else:
            flash("Email does not exist", category="error")

    return render_template("login.html")

From models.py:

from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func

class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_note = db.Column(db.String(10000))
    date = db.Column(db.DateTime(timezone=True), default=func.now())
    user_id = db.Column(db.Integer, db.ForeignKey("user.id"))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(130), unique=True)
    password = db.Column(db.String(150))
    user_name = db.Column(db.String(100))
    notes = db.relationship("Note")

From login.html:

{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% block content %}
<form method="POST">
    <h3 align="center">Login</h3>
    <div class="form-group">
            <label for="email_request">Email address</label>
            <input type="email" class="form-control" id="email_request" name="email_request" placeholder="Enter email">
    </div>
    <div class="form-group">
            <label for="passwordEnter">Password</label>
            <input type="password" class="form-control" id="passwordEnter" name="passwordEnter" placeholder="Enter password">
    </div>
    <br/>
    <button type="submit" class="btn btn-primary">Login</button>
</form>
{% endblock%}

Solution

  • It was an error calling password instead of passwordEnter in auth.py