如何将Slug添加到Blask Blog应用程序中



最近,我试图在Grinberg Miguel Blask Mega教程之后建立一个博客。当我尝试将帖子标题打开时,我得到了库存,以便在单击时显示细节。这是我的代码:

(models.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)

我在运行代码时看到了这个错误:

sqlalchemy.exc.operationalerror :( sqlite3.porationalerror(没有这样的 柱子: post.title [sql:'select post.id as post_id,post.title作为post_title, post.slug作为postronglug,post.body as post_body,post.timestamp as post_timestamp,post.user_id as post_user_id

只需确保类名称与表名称匹配。代码应罚款

相关内容

  • 没有找到相关文章

最新更新