skip to Main Content

I can’t figure out why I can’t get passed the login page. It doesn’t matter if I type in the right or wrong credentials, It reloads the login page, instead of signing me in.

import json

from flask import Flask, render_template, request, url_for, redirect
from flask.ext.bootstrap import Bootstrap
from flask.ext.login import LoginManager, UserMixin, login_required, login_user, current_user

from bson.objectid import ObjectId

from lib.mongo import db
from scripts.twitter_stats import ratio_of_wiki_links

app = Flask(__name__)
app.config["SECRET_KEY"] = ''
login_manager = LoginManager()
login_manager.login_view = 'login'
bootstrap = Bootstrap(app)

class User(UserMixin):
  def __init__(self, name, password, id, active=True): = name
    self.password = password = id = active

  def is_active(self):
    account = db.accounts.find_one({ "_id": ObjectId("561c7990e4b060b59d7cbcd1")})
    if not account['username'] == and account['password'] == self.password: = False

  def is_anonymous(self):
    return False

  def is_authenticated(self):
    return True

  def get_id(self):
    return str(db.accounts.find_one({'username':})['_id'])

def load_user(userid):
  user = db.accounts.find_one({'_id': userid})
  return user

@app.route('/login', methods=['POST', 'GET'])
def login():
  account = db.accounts.find_one({ "_id": ObjectId("561c7990e4b060b59d7cbcd1")})
  password = account['password']
  user = account['username']
  error = None
  if request.method == 'POST':
    username = request.form['username']
    passw = request.form['password']
    if username != user or passw != password:
      error = 'Invalid Credentials, please try again'
      user = User(username, passw, account['_id'])
      return redirect(url_for('home'))
  return render_template('login.html', error=error)

def home():
  account = db.accounts.find_one({'account': 'twitter'})
  entry = db.tweets.find().sort([['_id', -1]]).limit(1)
  entry = list(entry)[0]['post']
  # if g.user.is_authenticated():
  #     g.user =
  return render_template('index.html', account=account, entry=entry)



  1. Chosen as BEST ANSWER

    For those of you struggling with the same issue, my problem was with the load_user method. I fixed it by changing it to this:

    def load_user(userid):
      user_rec = db.accounts.find_one({'_id': ObjectId(userid)})
      user = User(user_rec['username'], user_rec['password'], user_rec['_id'])
      return user

  2. You should call add_routes before run app

    if __name__ == '__main__':
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top