问题描述
我有一个类似于 / posts / 1
的网址,其中1是指分贝中文章的ID。
@ bp.route('/< post_id>)
pre>
@login_required
def post(post_id):
find
p = Post.query.get(post_id)
return render_template(post / single_post.html,post = p)
$ c $但是,我想要做的是在网址中加入一些段落标题,比如/ posts / 1 / my_stack_overflow_question_is_bad
。我可以在模型中创建一个slugify属性:
pre $ class $($ db $ '
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)
但是我怎样才能把它放在url中?
解决方案您只需要将slug元素添加到URL路径:
@ bp.route('/< post_id> / < slug>)
@login_required
def post(post_id,slug):
查找帖子然后显示
p = Post.query。 get(post_id)
return render_template(post / single_post.html,post = p)
然后,当你想为它创建URL时,只需要将slug提供给
url_for
函数:
<$ p $ (1)
url_for('post',post_id = p.id,slug = p.slugified_title)
这可能有点乏味,所以我倾向于有一个:
#取自http: //flask.pocoo.org/snippets/6/
from flask import url_for
from werkzeug.routing import BuildError
def permalink(function):
def inner(* args,** kwargs):
endpoint,values = function(* args,** kwargs)
try:
返回url_for(endpoint,** values)
除了BuildError:
返回
返回内部
然后调整我的模型使用它:
class Post(db.Model):
__tablename__ ='posts'
id = db.Column(db.Integer,primary_key = True)
title = db.Column(db.String)
html = db.Column(db.Str )
@property
def slugified_title():
return slugify(self.title,separator =_,to_lower = True)
@permalink
def url(self):
#其中'post'是显示帖子的路径标题
return'post',{'post_id':self.id,' slug':self.slugified_title}
这样当我需要一个url的时候,因为它的网址,而不必手动通过url_for步骤。
I have a url like /posts/1
, where 1 refers to the id of the article in the db.
@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)
However, what I would like to do is have a url with some sort of slugified title in it, like /posts/1/my_stack_overflow_question_is_bad
. I can make a slugify property in the model:
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)
but how would I put that in the url?
You just need to add the slug element to the URL route:
@bp.route('/<post_id>/<slug>')
@login_required
def post(post_id, slug):
""" find the post and then show it """
p = Post.query.get(post_id)
return render_template("post/single_post.html", post=p)
Then when you want to create the URL for it-- just supply the slug into the url_for
function:
p = Post.query.get(1)
url_for('post', post_id=p.id, slug=p.slugified_title)
That can get a bit tedious, so I tend to have a permalink decorator:
# Taken from http://flask.pocoo.org/snippets/6/
from flask import url_for
from werkzeug.routing import BuildError
def permalink(function):
def inner(*args, **kwargs):
endpoint, values = function(*args, **kwargs)
try:
return url_for(endpoint, **values)
except BuildError:
return
return inner
Then adjust my model to use it:
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)
@permalink
def url(self):
# where 'post' is the title of your route that displays the post
return 'post', {'post_id': self.id, 'slug':self.slugified_title}
That way when I need a url, I can just ask the object for it's url, and not have to go through the url_for step manually.
这篇关于添加段落标题的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!