Python 你如何对一个url进行slagify?
我正在使用slugify,我不知道如何使我的文章标题slugify,我需要在我的文章中创建一个新的专栏吗?我一整天都在忙这个 有什么建议吗 视图.pyPython 你如何对一个url进行slagify?,python,flask,slug,Python,Flask,Slug,我正在使用slugify,我不知道如何使我的文章标题slugify,我需要在我的文章中创建一个新的专栏吗?我一整天都在忙这个 有什么建议吗 视图.py @app.route('/posts/<title>') @login_required def show(title): link = db.session.query(Post).filter_by(title = title).one() link2 = slugify(link.title) ret
@app.route('/posts/<title>')
@login_required
def show(title):
link = db.session.query(Post).filter_by(title = title).one()
link2 = slugify(link.title)
return render_template("post.html", post=link2, pid=id, title=link2)
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
def __init__(self, title, body):
self.title = title
self.body = body
@app.route('/posts/<slug>')
@login_required
def show(slug):
post = db.session.query(Post).filter_by(slug = slug).first()
if post:
return render_template("post.html", post=post)
abort(404)
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
slug = db.Column(db.String(80), index=True)
def __init__(self, title, body):
self.title = title
self.body = body
self.slug = slugify(title)
错误消息
File "C:\Program Files\Python35-32\lib\site-packages\sqlalchemy\orm\query.py", line 2760, in one
raise orm_exc.NoResultFound("No row was found for one()")
sqlalchemy.orm.exc.NoResultFound: No row was found for one()
我猜你是在用标题写文章,比如“这是我的第一篇博客文章”,这很好。我还猜测,您希望使用slug,以便为每篇文章提供一个更为URL友好的永久链接。在前面的例子中,slug类似于“这是我的第一篇博文” 因此,给定一个slug,您的帖子将在/posts/{unique post slug}路径上可用,而不是/posts/{title}。我的建议是将slug存储在数据库中,以便检索帖子,因为反向slagify并不那么容易 总结一下,您的模型应该是:
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
slug = db.Column(db.Text, index=True)
def __init__(self, title, body):
self.title = title
self.body = body
self.slug = slugfiy(title)
请注意index=True
选项。这样做是为了更快地查询
你的帖子应该是
@app.route('/posts/<slug>')
@login_required
def show(slug):
post = db.session.query(Post).filter_by(slug=slug).one()
return render_template("post.html", post=post)
@app.route(“/posts/”)
@需要登录
def显示(缓动):
post=db.session.query(post).filter_by(slug=slug.one)()
返回呈现模板(“post.html”,post=post)
在您的post.html文件中,您可以使用所有post字段(id、title、body、slug)作为post.id、post.title等我猜您正在为帖子使用标题,例如“这是我的第一篇博客文章”,这很好。我还猜测,您希望使用slug,以便为每篇文章提供一个更为URL友好的永久链接。在前面的例子中,slug类似于“这是我的第一篇博文” 因此,给定一个slug,您的帖子将在/posts/{unique post slug}路径上可用,而不是/posts/{title}。我的建议是将slug存储在数据库中,以便检索帖子,因为反向slagify并不那么容易 总结一下,您的模型应该是:
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
slug = db.Column(db.Text, index=True)
def __init__(self, title, body):
self.title = title
self.body = body
self.slug = slugfiy(title)
请注意index=True
选项。这样做是为了更快地查询
你的帖子应该是
@app.route('/posts/<slug>')
@login_required
def show(slug):
post = db.session.query(Post).filter_by(slug=slug).one()
return render_template("post.html", post=post)
@app.route(“/posts/”)
@需要登录
def显示(缓动):
post=db.session.query(post).filter_by(slug=slug.one)()
返回呈现模板(“post.html”,post=post)
在post.html文件中,您可以使用所有post字段(id、title、body、slug)作为post.id、post.title等在视图中使用
slug
而不是title
,并基于slug查询数据库。创建帖子时,请将标题slug化
,并将其存储在slug
字段中
视图.py
@app.route('/posts/<title>')
@login_required
def show(title):
link = db.session.query(Post).filter_by(title = title).one()
link2 = slugify(link.title)
return render_template("post.html", post=link2, pid=id, title=link2)
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
def __init__(self, title, body):
self.title = title
self.body = body
@app.route('/posts/<slug>')
@login_required
def show(slug):
post = db.session.query(Post).filter_by(slug = slug).first()
if post:
return render_template("post.html", post=post)
abort(404)
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
slug = db.Column(db.String(80), index=True)
def __init__(self, title, body):
self.title = title
self.body = body
self.slug = slugify(title)
您可以在视图中使用
slug
而不是title
,并基于slug查询数据库。创建帖子时,请将标题slug化
,并将其存储在slug
字段中
视图.py
@app.route('/posts/<title>')
@login_required
def show(title):
link = db.session.query(Post).filter_by(title = title).one()
link2 = slugify(link.title)
return render_template("post.html", post=link2, pid=id, title=link2)
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
def __init__(self, title, body):
self.title = title
self.body = body
@app.route('/posts/<slug>')
@login_required
def show(slug):
post = db.session.query(Post).filter_by(slug = slug).first()
if post:
return render_template("post.html", post=post)
abort(404)
class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
body = db.Column(db.Text)
slug = db.Column(db.String(80), index=True)
def __init__(self, title, body):
self.title = title
self.body = body
self.slug = slugify(title)
尝试登录
标题。是否正确接收?错误只表明它找不到任何行,而您只需要一行。我通常使用类似于Post.query.filter\u by(title=title)。first()
,然后检查结果是否为空。@btquanto你能重新表述一下我不明白的内容吗?Post.query.filter\u by(title=title)。first()有效,但当我在itI上放置slagify时它不起作用。我假设你使用的是url\u('show',title=title)
。这不起作用,因为你没有通过冠军,你通过了鼻涕虫。查询数据库时,没有标题与slug匹配。如果您需要按slug进行查询,那么是的,您应该将其存储在数据库中。请尝试使用loggintitle
。是否正确接收?错误只表明它找不到任何行,而您只需要一行。我通常使用类似于Post.query.filter\u by(title=title)。first()
,然后检查结果是否为空。@btquanto你能重新表述一下我不明白的内容吗?Post.query.filter\u by(title=title)。first()有效,但当我在itI上放置slagify时它不起作用。我假设你使用的是url\u('show',title=title)
。这不起作用,因为你没有通过冠军,你通过了鼻涕虫。查询数据库时,没有标题与slug匹配。如果您需要按slug进行查询,那么是的,您应该将其存储在数据库中。db.Text
用于存储多行文本,您可以使用db.string
db.Text
用于存储多行文本,您可以使用db.string
。非常感谢vivek,我想指出我使用了slug=db.Column(db.String(80))但是我在尝试之前没有添加index=true属性,这非常感谢您对它的解释。但是,我一直收到这个错误sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的列:posts.slug[SQL:'选择posts.id作为posts\u id,posts.title作为posts\u title,posts.body作为posts\u body,posts.slug作为posts\u slug\n from posts']回溯(最后一次调用)@EliHood我认为您尚未在posts数据库表中创建列slug
。您可以在数据库中手动插入该列,或者如果您正在使用flask sqlalchemy
,则可以执行db.create_all()
创建表。现在可以使用了,我使用的是不同的sql db名称,而不是test.db,我使用的是test12.db。这是您在uuu init_u文件上标识db名称的地方,如app.config['SQLALCHEMY_DATABASE_URI']='sqlite://'+os.path.join(tempfile.gettempdir(),'test122.db'))但是我仍然想知道如何完全清除数据库中的所有内容,我想指出我使用了slug=db.Column(db.String(80))但是,在我之前尝试添加index=true属性时,我没有添加index=true属性,这非常感谢您对它的解释。但是,我一直收到这个错误sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的列:posts.slug[SQL:'SELECT posts.id作为posts\u id,posts.title作为posts\u title,posts.body作为posts\u body,posts.slug作为posts\u slug\n from posts']Traceback(最近一次呼叫上次)@elhood我想你还没有