简体   繁体   English

如何将 slug 添加到 Flask 博客应用程序

[英]How to add slug to flask blog app

Recently I am trying to build a blog following Grinberg Miguel Flask mega tutorial.最近我正在尝试按照 Grinberg Miguel Flask 大型教程建立一个博客。 I got stocked when I try to slug my post title such that when it is clicked the detail will be displayed.当我尝试插入我的帖子标题时,我得到了库存,以便在单击时显示详细信息。 This is my code:这是我的代码:

(models.py) (模型.py)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(225))
    slug = db.Column(db.String(300))
    body = db.Column(db.Text)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)

def __repr__(self):
    return '<Post {}>'.format(self.body)

@staticmethod
def slugify (target, value, oldvalue, initiator):
    if value and (not target.slug or value != oldvalue):
        target.slug = slugify(value)
db.event.listen(Post.title, 'set', Post.slugify, retval=False)

my ROUTE code goes here:
from app.models import  Post

@bp.route('/', methods=['GET', 'POST'])
@bp.route('/index', methods=['GET', 'POST'])
def index():
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(
    page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.explore', page=posts.next_num) \
    if posts.has_next else None
        prev_url = url_for('main.explore', page=posts.prev_num) \
    if posts.has_prev else None
return render_template('index.html', title=_('Home'),
                       posts=posts.items, next_url=next_url,
                       prev_url=prev_url)

@bp.route('/explore',methods=['GET', 'POST'])
@login_required
def explore():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('main.index'))
    return render_template('create.html', title=_('Create'),form=form)

@bp.route('/<slug>')
def detail(slug):
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(
    page, current_app.config['POSTS_PER_PAGE'], False)
return render_template('detail.html', title=_('Home'),
                       posts=posts.items, next_url=next_url,
                       prev_url=prev_url)

I saw this error when I ran the code:我在运行代码时看到了这个错误:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: post.title [SQL: 'SELECT post.id AS post_id, post.title AS post_title, post.slug AS post_slug, post.body AS post_body, post.timestamp AS post_timestamp, post.user_id AS post_user_id sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的列: post.title [SQL: 'SELECT post.id AS post_id, post.title AS post_title, post.slug AS post_slug, post.body AS post_body, post.timestamp AS post_timestamp, post.user_id AS post_user_id

Just ensure that the class name matches the table name .只需确保类名与表名匹配即可。 And the code should run fine并且代码应该运行良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM