如何使用数据库 slug 字段显示相应的博客条目和 URL
How to use database slug field to display a respective blog entry and URL
我是 运行 一个包含博客的 web2py 网络应用程序。我在 post 数据库中使用了一个 slug 字段,并希望将该 slug 显示为 url 的一部分(而不是博客 post id)。我设法将 slug 字段作为 request.args 传递给 post 函数,但无法显示相关的 post - 相反,我收到 404 Page Not Found 错误。当我将 post.id 字段用作 request.args 时,post 显示正确,但 url 显示 post id 而不是 slug 字段。我想主要出于 SEO 原因使用 slug。任何指针都会很棒!
在我的模型中我有:
db.define_table('post',
Field('title', unique=True),
Field('subtitle'),
Field('intro', 'text'),
Field('body', 'text'),
Field('body_mid', 'text'),
Field('body_end', 'text'),
Field('published_on', 'date'),
Field('category'),
Field('file1', 'upload'),
Field('caption_one'),
Field('file2', 'upload'),
Field('caption_two'),
Field('slug', 'string', compute=lambda r: r['title']),
format = '%(title)s')
在我的控制器中我有:
#this selects all posts
def blog():
post = db().select(db.post.ALL, orderby=~db.post.published_on)
return dict(post=post)
#this selects a post based on request.args
def show():
post = db.post(request.args(0, cast = int)) or redirect(URL('index'))
return dict(post=post)
在我的博客视图中(有效):
{{在 post 中输入:}}
{{=LI(A(entry.title, _href=URL("show", args=entry.id)))}} {{pass}}
当我更改为(这不起作用)时:
{{进入 post:}}
{{=LI(A(entry.title, _href=URL("show", args=entry.slug)))}} {{pass}}
如何正确使用 slug 字段以在 URL 中显示 slug 并显示相关的数据库条目?
非常感谢
db.post(id)
语法要求传递给 db.post()
的值是记录 ID。如果要 select 基于另一个字段的记录,必须执行以下操作之一:
`db.post(slug=request.args(0))`
或:
`db(db.post.slug == request.args(0)).select().first()`
见http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Fetching-a-Row。
我是 运行 一个包含博客的 web2py 网络应用程序。我在 post 数据库中使用了一个 slug 字段,并希望将该 slug 显示为 url 的一部分(而不是博客 post id)。我设法将 slug 字段作为 request.args 传递给 post 函数,但无法显示相关的 post - 相反,我收到 404 Page Not Found 错误。当我将 post.id 字段用作 request.args 时,post 显示正确,但 url 显示 post id 而不是 slug 字段。我想主要出于 SEO 原因使用 slug。任何指针都会很棒!
在我的模型中我有:
db.define_table('post',
Field('title', unique=True),
Field('subtitle'),
Field('intro', 'text'),
Field('body', 'text'),
Field('body_mid', 'text'),
Field('body_end', 'text'),
Field('published_on', 'date'),
Field('category'),
Field('file1', 'upload'),
Field('caption_one'),
Field('file2', 'upload'),
Field('caption_two'),
Field('slug', 'string', compute=lambda r: r['title']),
format = '%(title)s')
在我的控制器中我有:
#this selects all posts
def blog():
post = db().select(db.post.ALL, orderby=~db.post.published_on)
return dict(post=post)
#this selects a post based on request.args
def show():
post = db.post(request.args(0, cast = int)) or redirect(URL('index'))
return dict(post=post)
在我的博客视图中(有效):
{{在 post 中输入:}} {{=LI(A(entry.title, _href=URL("show", args=entry.id)))}} {{pass}}
当我更改为(这不起作用)时: {{进入 post:}} {{=LI(A(entry.title, _href=URL("show", args=entry.slug)))}} {{pass}}
如何正确使用 slug 字段以在 URL 中显示 slug 并显示相关的数据库条目?
非常感谢
db.post(id)
语法要求传递给 db.post()
的值是记录 ID。如果要 select 基于另一个字段的记录,必须执行以下操作之一:
`db.post(slug=request.args(0))`
或:
`db(db.post.slug == request.args(0)).select().first()`
见http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Fetching-a-Row。