0

最近我正在尝试按照 Grinberg Miguel Flask 大型教程建立一个博客。当我尝试调整我的帖子标题时,我得到了库存,以便在单击它时会显示详细信息。这是我的代码:

(模型.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.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

4

1 回答 1

0

只需确保类名与表名匹配。并且代码应该运行良好

于 2020-12-19T13:04:00.020 回答