Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9288269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:52:32+00:00 2026-06-18T19:52:32+00:00

After writing my own login/registration system, I found out about Flask-Security and decided to

  • 0

After writing my own login/registration system, I found out about Flask-Security and decided to integrate it into my app. All of my authentication and recovery code is gone, but I can’t figure out why I’m getting simple errors upon starting up my application.

Here’s the most stripped-down example that I can come up with:

from flask import Flask, render_template_string

from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin, current_user

app = Flask('test')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/database.db'

db = SQLAlchemy(app)

roles_users = db.Table('roles_users',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('role_id', db.Integer, db.ForeignKey('role.id'))
)

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)

    username = db.Column(db.String(50), unique=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))

    active = db.Column(db.Boolean)
    confirmed_at = db.Column(db.DateTime)
    roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic'))

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

    def __repr__(self):
        return '<User {self.username}>'.format(self=self)

user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app)

@app.before_first_request
def create_db():
    db.create_all()

@app.route('/')
def index():
    return render_template_string('''
        {% if current_user.is_authenticated() %}
            You're logged in.
        {% else %}
            You're not logged in.
        {% endif %}
    ''')

if __name__ == '__main__':
    app.run(debug=True)

Upon visiting http://localhost:5000/, I get this error:

Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/tmp/app.py", line 62, in index
    ''')
  File "/usr/lib/python2.7/site-packages/flask/templating.py", line 140, in render_template_string
    context, ctx.app)
  File "/usr/lib/python2.7/site-packages/flask/templating.py", line 107, in _render
    rv = template.render(context)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "<template>", line 2, in top-level template code

  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 372, in getattr
    return getattr(obj, attribute)
UndefinedError: 'current_user' is undefined

I looked at the source of both Flask-Security-Example and Flask-Social-Example, but I can’t figure out where I’m going wrong here.

All help is greatly appreciated.


As a small side question, why is this table created like this and (more importantly) what is it for?

roles_users = db.Table('roles_users',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
    db.Column('role_id', db.Integer, db.ForeignKey('role.id'))
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:52:33+00:00Added an answer on June 18, 2026 at 7:52 pm

    Looks like I found my problem (facepalm):

    security = Security(app)
    

    I should be passing in the datastore as a second argument:

    security = Security(app, user_datastore)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an application that displays Google Map right after the login screen. The
I'm writing a physics simulating program, and found after() useful. I once would like
I'm writing my own software rasterizer in Java, and I ran into some trouble
I'm writing my own minifying tool for practice (regular expresssions practice), but after a
For the sake of my own amusement, I decided to try writing a batch
Suppose, I have a connected socket after writing this code.. if ((sd = accept(socket_d,
I am using JQuery to execute an operation within a web service. After writing
I am writing a blackberry application - after the application has started I need
When writing server-side code you need to explicitly stop execution after sending a Location:
I'm writing a tool which will push changes to database after commit to svn.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.