Web2py ...连接到正确的控制器

发布于 2025-01-06 08:54:45 字数 573 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2025-01-13 08:54:45

为什么不直接链接到每个控制器中的 show() 函数,而不是重定向,并按如下方式设置 URL:

{{for article in lists:}}
<a href="{{=URL('article', 'show', args=article.id)}}">{{=article.title}}</a>
{{pass}}

对于业务链接也是如此。

在article.py中:

def show():
    article = db(db.article.id == request.args(0)).select().first()
    return dict(article=article)

另一种选择是在主控制器中仅使用一个show()函数来处理文章和业务。在这种情况下,链接将如下所示:

文章:URL('home', 'show', args=['article',article.id])

业务:URL('home ', 'show', args=['business',business.id])

和 home.py 中:

def show():
    item = db(db[request.args(0)].id == request.args(1)).select().first()
    return dict(item=item)

show.html 视图可以包含以不同方式显示文章和业务的逻辑(取决于值的request.args(0)) 或 show() 函数可以显式设置替代的article.html和business.html视图,如下所示:

    response.view = 'home/%s.html' % request.args(0)

旁注: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:

{{for article in lists:}}
<a href="{{=URL('article', 'show', args=article.id)}}">{{=article.title}}</a>
{{pass}}

and similarly for the business links.

In article.py:

def show():
    article = db(db.article.id == request.args(0)).select().first()
    return dict(article=article)

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:

def show():
    item = db(db[request.args(0)].id == request.args(1)).select().first()
    return dict(item=item)

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 the show() function could explicitly set alternative article.html and business.html views as follows:

    response.view = 'home/%s.html' % request.args(0)

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 do URL(..., f='show?id=%s' % myid)) -- instead, use the vars argument (i.e., URL(..., f='show', vars=dict(id=myid))).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文