Web2py ...连接到正确的控制器
我有 2 个表:业务和文章。
我有 3 个控制器:主页、文章、企业。
在主页控制器中,以下代码返回企业和文章的列表:
def index():
lists= db().select(db.article.ALL,limitby=(0, 5),orderby=~db.article.id)
listings=db().select(db.business.ALL)
return dict(lists=lists,listings=listings)
在主页视图文件中带有循环。
所以我想从主页将文章链接到文章控制器,将业务链接到业务控制器...我使用了以下代码:
def show():
myid == request.vars.id
redirect(URL(c='businesses',f='show?id=%s'% myid))
因此,即使文章列表现在也会使用业务控制器中显示的功能链接到业务控制器,但我想要根据各自的清单使用 if 和 elif 。
I have 2 tables : business and article.
And I have 3 controllers : Home, Articles, Businesses.
In Home page controller the following code returns a list of Businesses and Articles:
def index():
lists= db().select(db.article.ALL,limitby=(0, 5),orderby=~db.article.id)
listings=db().select(db.business.ALL)
return dict(lists=lists,listings=listings)
with a loop in the home view file.
So I want to link articles to the Articles controller and Businesses to the Business controller from the home page...I have used the following code:
def show():
myid == request.vars.id
redirect(URL(c='businesses',f='show?id=%s'% myid))
So even articles list will link to Business controller now using the function show in Business controller, but I want use if and elif according to the respective listing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接链接到每个控制器中的
show()
函数,而不是重定向,并按如下方式设置 URL:对于业务链接也是如此。
在article.py中:
另一种选择是在主控制器中仅使用一个
show()
函数来处理文章和业务。在这种情况下,链接将如下所示:文章:
URL('home', 'show', args=['article',article.id])
业务:
URL('home ', 'show', args=['business',business.id])
和 home.py 中:
show.html 视图可以包含以不同方式显示文章和业务的逻辑(取决于值的
request.args(0)
) 或show()
函数可以显式设置替代的article.html和business.html视图,如下所示:旁注:URL将看起来如果将记录 id 设置为 arg 而不是 var(即 /show/1 而不是 /show?id=1),效果会更好。另外,如果您确实需要在
URL()
函数中指定变量,请勿将它们作为查询字符串显式附加到函数名称中(即,不要执行URL(.. ., f='show?id=%s' % myid)
) -- 相反,使用vars
参数(即URL(..., f='显示', vars=dict(id=myid))
)。Instead of a redirect, why not just link directly to the
show()
function in each controller, and set up the URLs as follows:and similarly for the business links.
In article.py:
Another option is using just a single
show()
function in your main controller to handle both articles and businesses. In that case, links would look like this:Article:
URL('home', 'show', args=['article', article.id])
Business:
URL('home', 'show', args=['business', business.id])
and in home.py:
The show.html view could then include logic to display an article and a business differently (depending on the value of
request.args(0)
), or theshow()
function could explicitly set alternative article.html and business.html views as follows:Side Note: The URLs will look nicer if you make the record id an arg instead of a var (i.e., /show/1 instead of /show?id=1). Also, if you do need to specify vars in the
URL()
function, do not explicitly append them to the function name as a query string (i.e., don't doURL(..., f='show?id=%s' % myid)
) -- instead, use thevars
argument (i.e.,URL(..., f='show', vars=dict(id=myid))
).