I'm new to web2py and I am trying to get the basics down. I'm following the instructions in the manual (http://web2py.com/books/default/chapter/29/03/overview#Adding-authentication)
I downloaded the source code from web2py, cd to the web2py direction
`>>>`python web2py.py
brings up the the screen with server port 8000 and my password...i enter it and the start the server which brings me to the website as expected
I create another tab in my terminal where I cd to the web2py directory. run python
from gluon import *
from gluon.tools import *
db = DAL('sqlite://storage.sqlite')
auth = Auth(db)
my db is fine when I >>>db._uri or >>>db._dbname but when I try auth = Auth(db) I get an error:
>>> auth = Auth(db)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "gluon/tools.py", line 1268, in __init__
request = current.request
AttributeError: 'thread._local' object has no attribute 'request'
but if I dir(Auth), it seems to have imported correctly:
>>> dir(Auth)
['_Auth__get_migrate', '_HTTP', '__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_login_settings', '_get_user_id', '_reset_two_factor_auth', 'accessible_query', 'add_group', 'add_membership', 'add_permission', 'archive', 'basic', 'cas_login', 'cas_validate', 'change_password', 'default_messages', 'default_settings', 'define_signature', 'define_tables', 'del_group', 'del_membership', 'del_permission', 'email_reset_password', 'enable_record_versioning', 'get_or_create_key', 'get_or_create_user', 'get_vars_next', 'groups', 'has_membership', 'has_permission', 'here', 'id_group', 'impersonate', 'is_impersonating', 'is_logged_in', 'log_event', 'login', 'login_bare', 'login_user', 'logout', 'navbar', 'not_authorized', 'profile', 'random_password', 'register', 'register_bare', 'request_reset_password', 'requires', 'requires_login', 'requires_membership', 'requires_permission', 'requires_signature', 'reset_password', 'reset_password_deprecated', 'retrieve_password', 'retrieve_username', 'run_login_onaccept', 'table_cas', 'table_event', 'table_group', 'table_membership', 'table_permission', 'table_user', 'update_groups', 'url', 'user_group', 'user_group_role', 'user_id', 'verify_email', 'wiki', 'wikimenu']
However, I noticed that my version does't have a 'db' and my friends did but that might be because her auth = Auth(db) worked
Why am I getting this error and how do I fix it?
Auth
requires a request
object, which is only available (1) within the web2py environment created when the framework receives an HTTP request, or (2) in a web2py shell. If you are simply in a standard Python shell or running a Python script/module outside of the web2py environment, there will be no request
object available. In fact, you will also need response
, session
, and T
objects to initialize Auth
.
If you are working in a Python shell, your best option is to instead open a web2py shell:
> python web2py.py -S myapp/mycontroller -M
The above will open a Python shell but create a web2py environment in the context of myapp. The -M
flag tells it to run the models for the app as well. With that option, you won't even need to define your db
or initialize Auth
because those objects will already be available in the shell environment (assuming they are defined in the app's models).
If necessary, you can also run an external script in a web2py environment using the -R
flag (see the documentation for more details).