Jon You're redefining db right above your define_table for 'image'. db is already assigned/defined higher up in the db.py file. We don't want to redefine it.
I've just been looking at the tutorial you're following and see that it seems a bit out-dated. -Jim On Wed, Apr 22, 2020 at 4:39 PM Jon Paris <[email protected]> wrote: > # -*- coding: utf-8 -*- > > # ------------------------------------------------------------------------- > # AppConfig configuration made easy. Look inside private/appconfig.ini > # Auth is for authenticaiton and access control > # ------------------------------------------------------------------------- > from gluon.contrib.appconfig import AppConfig > from gluon.tools import Auth > # If these tqwo lines are ativated they cause the db not ddefined error. > # If active at the end of the source then no error but no authenticalion > either > # auth = Auth(db) > # auth.define_tables(username=True) > > > > # ------------------------------------------------------------------------- > # This scaffolding model makes your app work on Google App Engine too > # File is released under public domain and you can use without limitations > # ------------------------------------------------------------------------- > > if request.global_settings.web2py_version < "2.15.5": > raise HTTP(500, "Requires web2py 2.15.5 or newer") > > # ------------------------------------------------------------------------- > # if SSL/HTTPS is properly configured and you want all HTTP requests to > # be redirected to HTTPS, uncomment the line below: > # ------------------------------------------------------------------------- > # request.requires_https() > > # ------------------------------------------------------------------------- > # once in production, remove reload=True to gain full speed > # ------------------------------------------------------------------------- > configuration = AppConfig(reload=True) > > if not request.env.web2py_runtime_gae: > # --------------------------------------------------------------------- > # if NOT running on Google App Engine use SQLite or other DB > # --------------------------------------------------------------------- > db = DAL(configuration.get('db.uri'), > pool_size=configuration.get('db.pool_size'), > migrate_enabled=configuration.get('db.migrate'), > check_reserved=['all']) > else: > # --------------------------------------------------------------------- > # connect to Google BigTable (optional 'google:datastore://namespace') > # --------------------------------------------------------------------- > db = DAL('google:datastore+ndb') > # --------------------------------------------------------------------- > # store sessions and tickets there > # --------------------------------------------------------------------- > session.connect(request, response, db=db) > # --------------------------------------------------------------------- > # or store session in Memcache, Redis, etc. > # from gluon.contrib.memdb import MEMDB > # from google.appengine.api.memcache import Client > # session.connect(request, response, db = MEMDB(Client())) > # --------------------------------------------------------------------- > > # ------------------------------------------------------------------------- > # by default give a view/generic.extension to all actions from localhost > # none otherwise. a pattern can be 'controller/function.extension' > # ------------------------------------------------------------------------- > response.generic_patterns = [] > if request.is_local and not configuration.get('app.production'): > response.generic_patterns.append('*') > > # ------------------------------------------------------------------------- > # choose a style for forms > # ------------------------------------------------------------------------- > response.formstyle = 'bootstrap4_inline' > response.form_label_separator = '' > > # ------------------------------------------------------------------------- > # (optional) optimize handling of static files > # ------------------------------------------------------------------------- > # response.optimize_css = 'concat,minify,inline' > # response.optimize_js = 'concat,minify,inline' > > # ------------------------------------------------------------------------- > # (optional) static assets folder versioning > # ------------------------------------------------------------------------- > # response.static_version = '0.0.0' > > # ------------------------------------------------------------------------- > # Here is sample code if you need for > # - email capabilities > # - authentication (registration, login, logout, ... ) > # - authorization (role based authorization) > # - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss) > # - old style crud actions > # (more options discussed in gluon/tools.py) > # ------------------------------------------------------------------------- > > # host names must be a list of allowed host names (glob syntax allowed) > auth = Auth(db, host_names=configuration.get('host.names')) > > auth.define_tables(username=True) > > # ------------------------------------------------------------------------- > # create all tables needed by auth, maybe add a list of extra fields > # ------------------------------------------------------------------------- > auth.settings.extra_fields['auth_user'] = [] > auth.define_tables(username=False, signature=False) > > # ------------------------------------------------------------------------- > # configure email > # ------------------------------------------------------------------------- > mail = auth.settings.mailer > mail.settings.server = 'logging' if request.is_local else > configuration.get('smtp.server') > mail.settings.sender = configuration.get('smtp.sender') > mail.settings.login = configuration.get('smtp.login') > mail.settings.tls = configuration.get('smtp.tls') or False > mail.settings.ssl = configuration.get('smtp.ssl') or False > > # ------------------------------------------------------------------------- > # configure auth policy > # ------------------------------------------------------------------------- > auth.settings.registration_requires_verification = False > auth.settings.registration_requires_approval = False > auth.settings.reset_password_requires_verification = True > > # > ------------------------------------------------------------------------- > # read more at http://dev.w3.org/html5/markup/meta.name.html > > # ------------------------------------------------------------------------- > response.meta.author = configuration.get('app.author') > response.meta.description = configuration.get('app.description') > response.meta.keywords = configuration.get('app.keywords') > response.meta.generator = configuration.get('app.generator') > response.show_toolbar = configuration.get('app.toolbar') > > # ------------------------------------------------------------------------- > # your http://google.com/analytics id > > # ------------------------------------------------------------------------- > response.google_analytics_id = configuration.get('google.analytics_id') > > # ------------------------------------------------------------------------- > # maybe use the scheduler > # ------------------------------------------------------------------------- > if configuration.get('scheduler.enabled'): > from gluon.scheduler import Scheduler > scheduler = Scheduler(db, > heartbeat=configuration.get('scheduler.heartbeat')) > > # ------------------------------------------------------------------------- > # Define your tables below (or better in another model file) for example > # > # >>> db.define_table('mytable', Field('myfield', 'string')) > # > # Fields can be 'string','text','password','integer','double','boolean' > # 'date','time','datetime','blob','upload', 'reference TABLENAME' > # There is an implicit 'id integer autoincrement' field > # Consult manual for more options, validators, etc. > # > # More API examples for controllers: > # > # >>> db.mytable.insert(myfield='value') > # >>> rows = db(db.mytable.myfield == 'value').select(db.mytable.ALL) > # >>> for row in rows: print row.id, row.myfield > # ------------------------------------------------------------------------- > > # ------------------------------------------------------------------------- > # after defining tables, uncomment below to enable auditing > # ------------------------------------------------------------------------- > # auth.enable_record_versioning(db) > > db = DAL("sqlite://storage.sqlite") > > db.define_table('image', > Field('title', unique=True), > Field('file', 'upload'), > format = '%(title)s') > > db.define_table('post', > Field('image_id', 'reference image'), > Field('author'), > Field('email'), > Field('body', 'text')) > > db.image.title.requires = IS_NOT_IN_DB(db, db.image.title) > db.post.image_id.requires = IS_IN_DB(db, db.image.id, '%(title)s') > db.post.author.requires = IS_NOT_EMPTY() > db.post.email.requires = IS_EMAIL() > db.post.body.requires = IS_NOT_EMPTY() > > db.post.image_id.writable = db.post.image_id.readable = False > > auth = Auth(db) > auth.define_tables(username=True) > > > On Wednesday, April 22, 2020 at 5:34:11 PM UTC-4, Jim S wrote: >> >> I'm guessing if at the top, they are getting called before db is defined. >> >> Are you able to share your db.py? >> >> -Jim >> >> On Wed, Apr 22, 2020 at 4:09 PM Jon Paris <[email protected]> wrote: >> >>> Well there's not error message right now - but if I move these two lines: >>> >>> auth = Auth(db) >>> auth.define_tables(username=True) >>> >>> back to the beginning of the file (after the from ...) then I get this: >>> >>> Traceback (most recent call last): >>> File "/Applications/web2py.app/Contents/MacOS/gluon/restricted.py", >>> line 219, in restricted >>> exec(ccode, environment) >>> File >>> "/Applications/web2py.app/Contents/MacOS/applications/Images/models/db.py", >>> line 9, in <module> >>> auth = Auth(db) >>> NameError: name 'db' is not defined >>> >>> In what would appear to my (very) untrained eye - if I leave them at the >>> end of the file then there is no error ... but no authentication either. >>> >>> >>> On Wednesday, April 22, 2020 at 4:30:31 PM UTC-4, Jim S wrote: >>>> >>>> Jon >>>> >>>> Can you post the traceback that you're receiving? >>>> >>>> -Jim >>>> >>>> On Wednesday, April 22, 2020 at 3:08:14 PM UTC-5, Jon Paris wrote: >>>>> >>>>> In the section on basic Authentication in the documents ( >>>>> http://web2py.com/books/default/chapter/29/03/overview#An-image-blog) >>>>> it says to include in the model the following: >>>>> >>>>> from gluon.tools import Auth >>>>> auth = Auth(db) >>>>> auth.define_tables(username=True) >>>>> >>>>> And to add this to the default controller. >>>>> >>>>> def user(): >>>>> return dict(form=auth()) >>>>> >>>>> But _where_? >>>>> >>>>> The def user(); part was already in the base controller. >>>>> >>>>> So I just added the "from ..." to the start of the file where it >>>>> already had: >>>>> from gluon.contrib.appconfig import AppConfig >>>>> >>>>> That cause the app to fail with a message that db did not exist. >>>>> >>>>> So I moved the two action lines down to the end following the table >>>>> definition. No error that way but no authentication occurs either. >>>>> >>>>> So where is the stuff supposed to go so that it is actioned? >>>>> >>>>> >>>>> >>>>> -- >>> Resources: >>> - http://web2py.com >>> - http://web2py.com/book (Documentation) >>> - http://github.com/web2py/web2py (Source code) >>> - https://code.google.com/p/web2py/issues/list (Report Issues) >>> --- >>> You received this message because you are subscribed to a topic in the >>> Google Groups "web2py-users" group. >>> To unsubscribe from this topic, visit >>> https://groups.google.com/d/topic/web2py/51UaQT4SxbQ/unsubscribe. >>> To unsubscribe from this group and all its topics, send an email to >>> [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/web2py/535b6e39-a1a8-48bb-944a-f836b9d6e835%40googlegroups.com >>> <https://groups.google.com/d/msgid/web2py/535b6e39-a1a8-48bb-944a-f836b9d6e835%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- > Resources: > - http://web2py.com > - http://web2py.com/book (Documentation) > - http://github.com/web2py/web2py (Source code) > - https://code.google.com/p/web2py/issues/list (Report Issues) > --- > You received this message because you are subscribed to a topic in the > Google Groups "web2py-users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/web2py/51UaQT4SxbQ/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/web2py/a5c3a14e-e8e3-4e7a-a578-9f230a7020a9%40googlegroups.com > <https://groups.google.com/d/msgid/web2py/a5c3a14e-e8e3-4e7a-a578-9f230a7020a9%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/CAERBpoC3SYYDKPcdQg4Uy2buCKQFDVLtgU-tby29XNHJDRgbZg%40mail.gmail.com.

