> Welcome to Flask’s large tutorial project! When it comes time to refactor the user model, run the script and it says :
TypeError: ‘bool’ object is not callable
This is the user model:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True) nickname = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) posts = db.relationship('Post', backref='author', lazy='dynamic') @property def is_authenticated(self): return True @property def is_active(self): return True @property def is_anonymous(self): return False def get_id(self): try: return unicode(self.id) # python 2 except NameError: return str(self.id) # python 3 def __repr__(self): return '<User %r>' % (self.nickname)
This is the code at the time of the call:
from flask import render_template, flash, redirect, session, url_for, request, g
from flask.ext.login import login_user, logout_user, current_user, login_required
from app import app, db, lm, oid from .forms import LoginForm from .models import User @app.route('/login', methods=['GET', 'POST']) @oid.loginhandler def login(): if g.user is not None and g.user.is_authenticated(): # error return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): session['remember_me'] = form.remember_me.data return oid.try_login(form.openid.data, ask_for=['nickname', 'email']) return render_template('login.html', title='Sign In', form=form, providers=app.config['OPENID_PROVIDERS'])
is_authenticated is a property, not a method, as described in Resources, so just remove the parentheses. There are two typos in this section of the book. Please refer to the Git source code.
put the wrong place:
if g.user is not None and g.user.is_authenticated():
if g.user is not None and G.user. is_authenticated:
and then no error.
Similar Posts:
- TypeError: ‘unicode’ object is not callable
- TypeError: ‘unicode’ object is not callable
- Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
- Strange frame error caused by incorrect use of flash. Redirect (‘/ path’) in flash
- [Solved] Uncaught TypeError: Cannot create property ‘LAY_TABLE_INDEX’ on number ‘1’
- [Solved] Error resolving template “XXX”, template might not exist or might not be accessible by any of the
- SQL Error: 1064, SQLState: 42000 [Three Methods to Solve]
- [Solved] Forbidden (403) CSRF verification failed. Request aborted.
- export ‘default’ (imported as ‘mod’) was not found in ‘-!../../../../node_module .vue?vue&type=script&lang=ts&’ (possible exports: __esModule)
- [Django CSRF tutorial] solve the problem of forbidden (403) CSRF verification failed. Request aborted