Python 如何将规则偶数/奇数与Flask一起用于注释部分
我是编程新手,我正在博客上工作,这是第一个使用Python Flask和SQLAlchemy的项目。我想创建一个评论部分,根据CSS规则偶数/奇数,每个评论都有不同的背景颜色(白色/灰色)。不幸的是,我无法应用它。这是我在Python Flask中的代码片段:Python 如何将规则偶数/奇数与Flask一起用于注释部分,python,html,css,flask,Python,Html,Css,Flask,我是编程新手,我正在博客上工作,这是第一个使用Python Flask和SQLAlchemy的项目。我想创建一个评论部分,根据CSS规则偶数/奇数,每个评论都有不同的背景颜色(白色/灰色)。不幸的是,我无法应用它。这是我在Python Flask中的代码片段: class Blogpost2(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50)) su
class Blogpost2(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50))
subtitle = db.Column(db.String(50))
author = db.Column(db.String(20))
date_posted = db.Column(db.DateTime)
content = db.Column(db.Text)
comments = db.relationship("Comment", backref="title", lazy="dynamic")
def get_comments(self):
return Comment.query.filter_by(post_id=blogpost2.id).order_by(Comment.timestamp.desc())
class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), unique=False, nullable=False)
email = db.Column(db.String(200), unique=False, nullable=False)
message = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime(),default=datetime.utcnow, index=True)
post_id = db.Column(db.Integer, db.ForeignKey('blogpost2.id'))
db.create_all()
def __repr__(self):
return '<Comment %r>' %self.name
@app.route('/post/<int:post_id>', methods=['POST','GET'])
def post(post_id):
post = Blogpost2.query.filter_by(id=post_id).one()
date_posted = post.date_posted.strftime('%d %B, %Y')
if request.method == 'POST':
name = request.form.get('name')
email = request.form.get('email')
message = request.form.get('message')
comment = Comment(name=name, email=email, message=message, post_id=post.id)
db.session.add(comment)
flash("Danke. Ihr Kommentar wurde erfolgreich gepostet")
db.session.commit()
return redirect(request.url)
return render_template('post.html', post=post, date_posted=date_posted)
我希望用户名1和消息1(奇数)为白色,用户名2和消息2(偶数)为灰色,但如下图所示,我只得到一个灰色区域,其中的消息似乎是HTML列表:
我怎样才能解决它?
提前非常感谢您的每一个建议
<!-----Posted Comment Section----->
<div class="comment-sec-area">
<div class="container">
<div class="row flex-column">
{% if post.comments %}
{% if post.comments.count() > 0 %}
<h3 id="comment-tag"> Comments </h3>
<ul class="comment-odd-even">
{% for comment in post.comments %}
<li> {{comment.name}} </li>
<li> {{comment.message}} </li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
</div>
</div>
</div>
.comment-odd-even:nth-child(2) {
background-color: grey;
}