0

I'm trying to create a web that you can save bookmarks to wall and add some comments to it so you can check it out later. I made a db that keeps the data and now Im trying to display them in my html. Cant seem to fint what is wrong in my code, that its not showing up. Would love some help please. It does add it the the database. Just not show up.

routes:

@app.route('/', methods=['GET', 'POST'])
@app.route('/home', methods=['GET', 'POST'])
def home():
    form= AddLinkForm()
    bookmarks = Bookmarks.query.order_by(Bookmarks.date_posted.desc())
    if form.validate_on_submit():
        bookmark = Bookmarks(link=form.link.data, content=form.content.data, author=current_user)
        db.session.add(bookmark)
        db.session.commit()
        flash('Your post has been added!', 'success')
    return render_template('home.html', form=form, bookmarks=bookmarks)

models:

class Bookmarks(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    link = db.Column(db.String, nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"User('{self.link}, {self.content}, {self.date_posted}')"

home.html

{% for bookmark in bookmarks.items %}
<article class="media content-section-bookmark">
<img class="rounded_circle article-img" src="https://assets.stickpng.com/images/67.png" alt="">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="">{{ bookmark.link }}</a>
</div>
<small class="text-muted">{{ bookmark.content }}</small>
<p class="article-content float-right">Read More</p>
<p class="article-content float-right">{{ bookmark.date_posted.strftime('%Y-%m-%d') }}</p>
</div>
</article>
{% endfor %}

1 Answer 1

1

I think you forgot the .all() at the end of your query. In addition, it makes more sense to query the data records after a new one has been added.

@app.route('/home', methods=['GET', 'POST'])
def home():
    form= AddLinkForm()
    if form.validate_on_submit():
        bookmark = Bookmarks(link=form.link.data, content=form.content.data, author=current_user)
        db.session.add(bookmark)
        db.session.commit()
        flash('Your post has been added!', 'success')
    bookmarks = Bookmarks.query.order_by(Bookmarks.date_posted.desc()).all()
    return render_template('home.html', form=form, bookmarks=bookmarks)

And now you can iterate over the results of the query.

{% for bookmark in bookmarks %}
<!-- your code here -->
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.