从Web2py中的数据库填充下拉导航菜单

时间:2015-08-08 05:31:35

标签: web2py

大家好日子!

这可以在Web2py中使用吗?

查看学生 第一年(从数据库填充)

  • A部分(从数据库填充)
  • B部分等
  • C节

第二年(从数据库填充)

  • A部分等
  • B节
  • C节

等等

现在代码

myDB.py

db.define_table('stud_info', 
            Field('firstname'),
            Field('lastname'))

db.define_table('section', 
            Field('section_name', requires=IS_NOT_EMPTY()))

db.define_table('yearstanding', 
            Field('year_level'),
            Field('section', 'reference section'))

menu.py

response.menu = [(T('Home'), False, URL('icthome', 'home'), []),]

def _():
# shortcuts
app = request.application
ctr = request.controller
# useful links to internal and external resources

for row in db(db.year_standing).select():
    response.menu += [
        ((T('View Students'), False, '#', [
                    ((T(row.year_level), False, '#', [
                                (T(row.section), False, URL('',''), [])]))
                ]))
    ]

我收到了错误:

Traceback (most recent call last): <br><br>
File "D:\Kuya Files\My Videos\Tutorial\Python\web2py_win\gluon\restricted.py", line 227, in restricted
    exec ccode in environment <br><br>
File "D:/Kuya Files/My Videos/Tutorial/Python/web2py_win/applications/ClearanceSystem/models/menu.py", line 168, in <module>
    if DEVELOPMENT_MENU: _() <br><br>
  File "D:/Kuya Files/My Videos/Tutorial/Python/web2py_win/applications/ClearanceSystem/models/menu.py", line 53, in _
    for row in db(db.year_standing).select():<br><br>
  File "D:\Kuya Files\My Videos\Tutorial\Python\web2py_win\gluon\packages\dal\pydal\base.py", line 906, in __getattr__
    return super(DAL, self).__getattr__(key)<br><br>
  File "D:\Kuya Files\My Videos\Tutorial\Python\web2py_win\gluon\packages\dal\pydal\helpers\classes.py", line 348, in __getattr__
    raise AttributeError<br><br>
AttributeError

1 个答案:

答案 0 :(得分:0)

/ models文件夹顶层的文件按字母顺序执行,因此在myDB.py文件中定义db.year_standing表之前,正在执行menu.py文件。只需更改文件名,即可在定义表后创建菜单。

此外,cache this query可能是一个好主意,以避免在每次请求时都访问数据库。