문제

I've been trying to get a dropdown menu created from my database which has main categories and subcategories. For example Animals would be the main with dogs and cats as the subs. I also have a main title for the menu that I set first. Basically I have not been able to get the code right to continue the formatting properly.

My db.py:

db.define_table('store_categories',
Field('maincategory', 'string'),
Field('subcategory', 'string'),
Field('description', 'text'))

Menu.py:

response.menu = [(T('Catalog'), False, '')]
maininfo=db(db.store_categories.subcategory=='').select(db.store_categories.maincategory)
for maincat in maininfo:
    response.menu.append([maincat.maincategory,False,'link'],)
    for subcat in db(db.store_categories.subcategory!='').select(db.store_categories.subcategory):
        response.menu[-1].append([(T(subcat.subcategory)),False,'link'])

My database is populated like this: (i removed the store_categories on the other fields to make things fit)

store_categories.id; .maincategory; .subcategory;   .description
1                    Animals                         Animals Main
2                    Animals        Dogs    
4                    Animals        Cats    

When it iterates without the "-1" for the slice it shows a menu but a simple static menu with all 4 entries straight across (no dropdown) however I want a dropdown menu which I have produced statically with this:

    response.menu2 = [(T('Catalog'), False, '', [
                     (T('Animals'), False, '/beta/default/Animals', [
                     (T('Dogs'), False, '/beta/default/Dogs'),
                     (T('Cats'), False, '/beta/default/Cats'),
                     ])
                     ]
                     )]

Which looks like this in the shell:

[(<lazyT 'Catalog'>,
  False,
  '',
  [(<lazyT 'Animals'>,
    False,
    '/beta/default/Animals',
    [(<lazyT 'Dogs'>, False, '/beta/default/Dogs'),
    (<lazyT 'Cats'>, False, '/beta/default/Cats')])])]

Unfortunately no matter the variations I try I cannot reproduce this in my code, and quite literally all of the variations produce an "more than 1 value needed to unpack" kind of error in the ticket of web2py.

Essentially in the shell my code produces this:

[(<lazyT 'Catalog'>, False, ''),
 ['Animals',
  False,
  'link',
  [<lazyT 'Dogs'>, False, 'link'],
  [<lazyT 'Cats'>, False, 'link']]]

I can see some of the differences, however I am unable to reproduce the same structure. I attempted to modify my code after reviewing the web2py chapter about menus but the format I followed (as far as I can tell) still errors out. In addition I tried to recreate the format manually and ended up using the menu.py stuff already present as a format to follow because even when copy/paste the examples of the format from the book it fails. I also reviewed these articles for assistance as well (unfortunately most of them are inconclusive but thought i would try searching first before asking anywhere):

http://article.gmane.org/gmane.comp.python.web2py/121092/match=generate+menu

hXXp://www.web2pyslices.com/slice/show/1330/dynamic-menu

hXXps://groups.google.com/forum/#!searchin/web2py/menu$20generate|sort:date|spell:false/web2py/fbegfZ9VGys/O5jtwjXNYdMJ

Any and all help is greatly appreciated, thanks to all.

도움이 되었습니까?

해결책

Strange no one could answer or even suggest, yet I'm sure many of you have a similar style menu. At any rate the creator of web2py helped me and the solution he provided was this:

def menu_rec(items):
    return [(x.title,None,URL('shop', 'category',
        args=pretty_url(x.id, x.slug)), menu_rec(x.children)) for x in items or []]

Which works great.

다른 팁

  1. Query your items from db for each desired group.
items_list1 = db.executesql('QUERY MY ITEMLIST1')
  1. Compose the tuple within a function and feed an array to return it.
    def gen_menulist(items):
        menulist = []
        for x in items:
            thistuple = (T(x[3]), False,x[4])
            menulist.append(thistuple)
        return menulist
  1. Call the function inside response.menu array.
response.menu += [
            ('Menu list group 1', False, '#', gen_menulist(items_list1)),
        ]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top