'HTML shows Flask-SQLAlchemy syntax?
When trying to display replies of users on my page it displays them with SQL syntax, like so: ('reply',) I've tried str()
when returning from my route.
html:
<ul style="float:left;" class="list-group list-group-flush" id="anons">
{% for reply in replies %}
<li class="list-group-item" 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')
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Patrick Yoder |