3

You know how blogs work where a the homepage will vertically display all the posts but it will shorten the text and have a 'read more' link to go to the page of that article.

I want to do that with my python/flask app because I don't want it to show the whole text of the post. How would i shorten it's length for the homepage with python/flask/sqlalchemy?

Here's some code for the view and the template

#loop through posts
@app.route('/')
def read():
    post = Post.query.filter_by(visibility='public').order_by(Post.published.desc()).all()
    return render_template('index.html', post=post)
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block title %}
    booksummary app
{% endblock %}

{% block jumbotron %}
    <div class="jumbotron">
    </div>
{% endblock %}

{% block content %}
    {% if post %}
        {% for display in post %}
            <article>
                <h2><a href="post/{{ display.url }}">{{ display.title }}</a></h2>

                    <p class="center">{{ display.author }} || {{ display.published.strftime('%b %d, %Y') }}</p>



                <div class="text">
                    {{ display.body | safe }}
                </div>
            </article>  

        {% endfor %}
    {% else %}
        <div class="well">
            <h2>Sorry, there are no posts yet.  I am currently working on it</h2>
        </div>
    {% endif %}
{% endblock %}

I want the text of the 'post.body' part of it to be shortened for the homepage. If there's a way to do that than please let me know.

vallentin
  • 23,478
  • 6
  • 59
  • 81
hckrieger
  • 81
  • 2
  • 7

0 Answers0