Home > Mobile >  HTML shows Flask-SQLAlchemy syntax?
HTML shows Flask-SQLAlchemy syntax?

Time:03-28

When trying to display replies of users on my page it displays them but they have sql syntax, like so: ('reply',) i've tried str() when returning from my route. html:

<ul style="float:left;"  id="anons">
{% for reply in replies %}
    <li  align="left">
        {{ reply }}
    </li>
        {% endfor %}

model:

class Reply(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    data = db.Column(db.String(10000))
    date = db.Column(db.DateTime(timezone=True), default=func.now())

route:

@views.route('/', methods=["GET", "POST"])
def home():
    if request.method == "POST":
        name = request.form.get("name")
        data = request.form.get("reply")

        if len(data) >= 1:
            new_reply = Reply(name=name, data=data)
            db.session.add(new_reply)
            db.session.commit()
            replies = db.session.query(Reply.data)

            return render_template('home.html', replies=replies)

    return render_template('home.html')

CodePudding user response:

Try modifying your replies definition to this:

replies = Reply.query.all()

This essentially creates a list of all the 'Reply' objects, which you then pass to the template. If you were wanting to only show the .data attribute of each 'Reply', you would access that with {{ reply.data }} in the for loop.

  • Related