[英]My code is not returning the desired output. Where am I going wrong?
以下是路线信息的代码:
@app.route("/", methods=["GET", "POST"])
def index():
if current_user.is_authenticated:
posts = Image.query.all()
for post in posts:
print(post.post_name)
# print(current_user.is_moderator)
return render_template("index.html", current_user=current_user, posts=posts)
else:
posts = Image.query.all()
return render_template("index.html", posts=posts)
以下是模板:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ url_for('static', filename='index.css') }}">
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<div class="flash-msg">{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}
<script src="{{ url_for('static', filename='index.js') }}"></script>
<div id="first">
<input type="text" id="searchBar" name="searchBar">
{% if current_user.is_authenticated %}
<a href="{{ url_for('user', username=current_user.username) }}"><img src="{{ url_for('static', filename='profile-icon.png') }}" alt="" id="profileIcon"></a>
<a href="{{ url_for('logout') }}" id="logout-btn">Logout</a>
{% else %}
<a href="{{ url_for('login') }}"><img src="{{ url_for('static', filename='profile-icon.png') }}" alt="" id="profileIcon"></a>
{% endif %}
</div>
<div>
{% for post in posts %}
{% if loop.changed(current_post) %}
<div>
<a href="{{ url_for('read', post_name=post.post_name) }}">
<img src="{{ url_for('static', filename='images/'+post.title) }}" alt="">
</a>
</div>
<!-- <div>{{ current_post }}</div>
<div>{{ current_post }}</div> -->
{% else %}
{% set current_post = post.post_name %}
<div></div>
{% endif %}
{% endfor %}
</div>
</body>
</html>
在路由信息中运行代码后,这是 for 循环的输出:
Hz7g5LlonYWG
Hz7g5LlonYWG
Hz7g5LlonYWG
Hz7g5LlonYWG
dHjeIv8guNVE
dHjeIv8guNVE
dHjeIv8guNVE
dHjeIv8guNVE
ZcLji2uNgT1V
这是表格图像的代码:
class Image(db.Model):
id = db.Column(db.Integer, primary_key=True)
title=db.Column(db.String(120), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
img_location = db.Column(db.String(600), nullable=False)
mimetype = db.Column(db.String(10))
post_name = db.Column(db.String(150), nullable=False, unique=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
现在我遇到的问题是,在我上面显示的代码的模板中,它只为 post_name 的一个值渲染一个标签。 根据我编写的代码,它应该为 post_name 的每个 UNIQUE 值呈现一个标签。 正如 for 循环的输出清楚地显示的那样,我的数据库中存储了 3 个 post_name 的唯一值。 我的意思是,只要 post.post_name 的值与上一次迭代的 post.post_name 值不同,就应该执行以下代码:
<div>
<a href="{{ url_for('read', post_name=post.post_name) }}">
<img src="{{ url_for('static', filename='images/'+post.title) }}" alt="">
</a>
</div>
但是,实际执行的是 else 块中的代码,当 post.post_name 的值与前一次迭代中的值不同时,从技术上讲,它不应该被执行。 我怎样才能解决这个问题? 请帮我。
问题是分配不能逃脱它们的定义块。 因此,在else
分支中定义的current_post
变量将不会出现在下一个循环的if
检查块中。 但是解决方案很简单,你甚至不需要创建新变量,只需在changed
的测试中使用post.post_name
:
{% for post in posts %}
{% if loop.changed(post.post_name) %}
<div>
<a href="{{ url_for('read', post_name=post.post_name) }}">
<img src="{{ url_for('static', filename='images/'+post.title) }}" alt="">
</a>
</div>
{% else %}
<div></div>
{% endif %}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.