我正在制作Flask应用程序,它实际上是一个博客。对于数据库,我正在使用Firebase。使用我的管理员凭据,每当我添加新闻时,就不会显示最新新闻。 例如,如果我添加3个新闻,那么第三个新闻应该排在最前面。
这是html文件。
{% block content %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-12" style="margin-top: 25px; margin-bottom: 40px;">
{% for trending_news in trending_news|reverse %}
<div class="card " style="margin-bottom: 10px;">
<div class="row">
<div class="col-lg-8 col-md-8 col-sm-12 col-12">
<div class="card-body">
<div class="card-title" style="font-size: 1.3em;"><a href="/trending-news/{{ trending_news.title }}">{{ trending_news.title }}</a></div>
<p class="small"><i>{{ trending_news.created_at }}</i></p>
<p style="width:100%;height: 95px;overflow-y: hidden;scroll-behavior: smooth; text-align: justify; text-justify: inter-word;">{{ trending_news.description }}</p>
<p><a href="/trending-news/{{ trending_news.title }}">Read full story</a></p>
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-12 col-12">
<div class="card-body">
{% if trending_news.image != '' %}
<img src="static/SERVER_FILES/img/{{ trending_news.image }}" class="img-responsive img-fluid z-depth-1 rounded" alt="1" style="width: 100%;height: 200px;">
{% endif %}
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
这是py文件。
@app.route('/add-trending-news', methods=['GET', 'POST'])
@is_admin_logged_in
def add_trending_news():
if request.method == 'POST':
created_at = datetime.now(timezone('Asia/Kolkata')).strftime("%B %d, %Y %I:%M:%S %p")
title = request.form['title']
description = request.form['description']
source = request.form['source']
image_name = ''
if 'image' in request.files:
image = request.files['image']
img_ext = image.filename.rsplit('.', 1)[1].lower()
image.filename = 'img' + str(datetime.now().date()) + '_' + str(datetime.now().time()) + '.' + img_ext
image_name = secure_filename(image.filename)
image.save(os.path.join(app.config['SERVER_FILES_PATH'], image_name))
else:
image_name = ''
tags = request.form['tags']
data = {
"created_at": str(created_at),
"title": str(title),
"description": str(description),
"source": str(source),
"image": str(image_name),
"tags": str(tags)
}
db.child(app.config['TABLE_NAME1']).push(data)
flash('New news added successfully. Want to add another news?' + Markup(
"<a href='/add-trending-news'> Click Here.</a>"), 'success')
return redirect(url_for('trending_news'))
return render_template('add_trending_news.html')