Answer the question
In order to leave comments, you need to log in
Why doesn't adding posts work?
Hello.
I am writing a mini-blog to introduce Flask.
Got to adding posts.
Here is what I have:
models.py
class BlogPost(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=False)
description = db.Column(db.String, nullable=False)
author_id = db.Column(db.Integer, ForeignKey('users.id'))
def __init__(self, title, description):
self.title = title
self.description = description
self.author_id = current_user.id
def __repr__(self):
return '<title {}'.format(self.title)
@users_blueprint.route('/add', methods=['GET', 'POST'])
@login_required
def add():
form = AddForm()
if form.validate_on_submit():
post = BlogPost(
title=form.title.data,
description=form.description.data,
author_id=current_user.id
)
db.session.add(post)
db.session.commit()
return redirect(url_for('home.home'))
return render_template('add.html', form=form)
class AddForm(Form):
title = TextField(
'title',
validators=[DataRequired(), Length(min=3, max=25)]
)
description = TextField(
'description',
validators=[DataRequired(), Length(min=6, max=40)]
)
TypeError: __init__() got an unexpected keyword argument 'author_id'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question