本文整理汇总了Python中models.Article.by_slug方法的典型用法代码示例。如果您正苦于以下问题:Python Article.by_slug方法的具体用法?Python Article.by_slug怎么用?Python Article.by_slug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Article
的用法示例。
在下文中一共展示了Article.by_slug方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: articles_view
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import by_slug [as 别名]
def articles_view(slug):
""" Show article from <slug> or 404 """
from models import Article
article = Article.by_slug(slug).filter(Article.pub_date < datetime.now()).first_or_404()
return render_template('articles/view.html', **locals())
示例2: article_edit
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import by_slug [as 别名]
def article_edit(slug):
""" Render edit article form by <slug> or update data from `POST` """
from models import Article, Tag, db
from forms import ArticleForm
article = Article.by_slug(slug=slug).first_or_404()
form = ArticleForm()
if request.method != 'POST':
# init form data from object
form.title.data = article.title
form.slug.data = article.slug
form.description.data = article.description
form.content.data = article.content
tags = [t.title for t in article.tags]
for t in tags:
if form.tags.data:
form.tags.data += ", " + t
else:
form.tags.data = t
form.pub_date.data = article.pub_date.strftime("%Y-%m-%d %H:%M:%S")
form.upd_date.data = article.upd_date.strftime("%Y-%m-%d %H:%M:%S")
else:
if form.validate():
article.title = form.title.data
article.slug = form.slug.data
article.description = form.description.data
article.content = form.content.data
article.pub_date = datetime.strptime(form.pub_date.data, '%Y-%m-%d %H:%M:%S')
# autonow if empty or error
try:
upd_datetime = datetime.strptime(form.upd_date.data, '%Y-%m-%d %H:%M:%S')
except Exception:
article.upd_date = datetime.now()
article.tags = []
tags_raw = form.tags.data.split(',')
for t in tags_raw:
t = t.strip()
# Get or create
try:
article.tags += [Tag.query.filter(Tag.title == t)[0]]
except Exception:
article.tags += [Tag(t)]
db.session.commit()
flash("Article updated success.", 'success')
return redirect(url_for('articles_view', slug=form.slug.data))
return render_template('articles/edit.html', **locals())