2
我有一個像/posts/1
這樣的網址,其中1表示db中文章的ID。爲網址添加slugified標題
@bp.route('/<post_id>')
@login_required
def post(post_id):
""" find the post and then show it """
p = Post.query.get(post_id)
return render_template("post/single_post.html", post=p)
不過,我想這樣做是有一個網址,在它某種slugified稱號,像/posts/1/my_stack_overflow_question_is_bad
。我可以在模型中使slugify屬性:
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
html = db.Column(db.String)
@property
def slugified_title():
return slugify(self.title, separator="_", to_lower=True)
但我將如何把它在網址?
優秀的答案,謝謝你超越和超越 – corvid 2014-10-19 15:47:04