最近我正在尝试按照Grinberg Miguel Flask大型教程建立一个博客。当我尝试敲击我的帖子标题时,我得到了库存,这样当点击它时,将显示细节。这是我的代码:
(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.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
答案 0 :(得分:0)
只要确保类名与表名匹配即可。并且代码应该运行良好