开发者

How do i pass my form value (which is dynamic) on to the menu on web2py? That is it should be reflected in the URL

I have a menu that is something like the below:

response.menu = [
    [T('Home'), False, 
     URL(request.application,'default','index'), []],
    [T('Request Log'), False, URL(request.application, 'default', 'method1'),],
    [T('Management Log'), False, URL(request.application, 'default', 'method2?filter_scenario=%s'%my_dynamic_var),],
    ]

Now my_dynamic_var should be taken from the form's field. Can anyone help me crack this.

Thanks in开发者_如何学JAVA Advance.


I'd recommend asking on the web2py google group

You can modify/append to response.menu from within your controller function, so you could try setting up the main part of your menu in models/menu.py and then add in the 'management log' entry inside the corresponding controller function.

Also, when generating the URL, there is no need for you to do 'method2?filter_scenario=%s'%my_dynamic_var you should just let the URL() helper take care of it for you as in:

URL(request.application, 'default', 'method2', vars=dict(filter_scenario = my_dynamic_var))

So all together maybe try something like this

models/menu.py

response.menu = [
    [T('Home'), False, 
     URL(request.application,'default','index'), []],
    [T('Request Log'), False, URL(request.application, 'default', 'method1'),],
    ]

controllers/default.py

def method2():
    form = FORM(#define your form here)

    if form.accepts(request.vars, session):
        #add in extra menu option
        response.menu.append([T('Management Log'), False, URL(request.application, 'default', 'method2', vars=dict(filter_scenario = form.vars.my_dynamic_var)),])


Answer one does not work. The answer really depends on the workflow but, for example, you can do:

response.menu = [
  [T('Home'),False,URL('default','index')],
  [T('Request Log'), False, URL('default', 'method1')],
]
if session.my_dynamic_var: response.menu+=[
  [T('Management Log'), False, URL('default', 'method2', 
   vars=dict(filter_scenario=session.my_dynamic_var))]]

and in the controller create an action to set the value:

def method1():
    form = SQLFORM.factory(Field('my_dynamic_var'))
    if form.accepts(request, session):
        session.my_dynamic_var=form.vars.my_dynamic_var
    return dict(form=form)

Please ask these questions on the web2py mailing list. That is where the experts are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜