H
H
hardwellZero2015-03-07 09:42:25
Flask
hardwellZero, 2015-03-07 09:42:25

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)

views.py
@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)


forms.py
class AddForm(Form):
    title = TextField(
        'title',
        validators=[DataRequired(), Length(min=3, max=25)]
    )
    description = TextField(
        'description',
        validators=[DataRequired(), Length(min=6, max=40)]
    )

I can also show you a template if needed.
Essence. Everything starts without errors, the page also works. When filling out the form and pressing the "Submit" button, the following message appears:
TypeError: __init__() got an unexpected keyword argument 'author_id'

Where did I take a wrong turn?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hardwellZero, 2015-03-07
@hardwellZero

Issue resolved.
Extra line - author_id=current_user.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question