'NoneType'对象没有属性'password'

时间:2017-04-04 01:57:39

标签: python-3.x flask google-cloud-datastore

我想使用flask web框架创建一个功能登录表单。我正在使用Google Datastore。目前,我收到以下错误。

'NoneType' object has no attribute 'password'

另外,我不确定我在用户模型中使用的id属性。可能是服务器无法识别每个用户的ID。有点困惑。任何帮助表示赞赏。

我在视图中的models.py文件:

import datetime

from google.appengine.api import memcache
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import ndb

class User(ndb.Model):
    id = ndb.StringProperty()
    username = ndb.StringProperty()
    email = ndb.StringProperty()
    password = ndb.StringProperty()
    joined_at = ndb.DateTimeProperty() 
    updated_at = ndb.DateTimeProperty() 
    is_admin = ndb.BooleanProperty(default=False)

    # Flask-Login integration
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return false

    def get_id(self):
        return self.id

forms.py文件:

class LoginForm(Form):
    username  = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])

main.py:

from flask import Flask, g, flash, render_template, url_for, redirect
from flask_login import LoginManager, login_user
import models
import forms

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = forms.LoginForm()
    if form.validate_on_submit():
        try:
            user = models.User.exists(username = form.username.data)
        except models.DoesNotExist:
            flash("Your email does not match", "error")
        else:
            if user.password == form.password.data:
                login_user(user)
                flash("You've been logged in!", "success")
                return redirect(url_for('index'))
            else:
                flash("Your password does not match!", "error")
    return render_template('login.html', form=form)

1 个答案:

答案 0 :(得分:1)

您目前只是检查天气用户名是否存在。您必须首先获取该实体,然后检查用户名和密码。这将解决您的问题。

user_check = User.query(User.username == form.username.data).get()

    if user_check:
        if user_check.password == form.password.data:
            print "Success"
        else:
            print "Wrong Password"
    else:
        print "No Username Exists"