如何解决在烧瓶中检测会话过期的方法?
在 Flask 中,我想在会话超时时删除一些文件。我没有用户登录 - 任何人都可以上传图像并使用它们进行操作。我希望图像在会话结束时消失。但是我如何检测到那是什么时候?
我认为将图像存储在 RAM 中是没有意义的,这就是为什么我想将它们保存在服务器上并再次删除它们。
编辑:视图函数和 html 的代码,用于渲染包含在会话超时时将被删除的图像的 PDF。
views.py:
from flask import (render_template,request)
from flask_weasyprint import HTML,render_pdf
from .forms import toPDF
from .helpers import (custom_images_in_collection)
@user_blueprint.route("/collection",methods=["GET","POST"])
def collection():
form = toPDF()
# 'collection' is a list of preexisting/permanent image objects selected by user and stored in session
# custom_images is a list of extra image objects created by user on the fly. They're stored
# and retreived from the database from their own table of temporary images with a time field.
# I want the custom images to disappear when the session expires
custom_images = custom_images_in_collection(collection)
collection.append(custom_images)
if request.method == "POST":
if collection:
if form.validate_on_submit():
template = render_template("mypdf.html",collection=collection)
html = HTML(string=template)
# This bit of CSS is dynamically generated,the rest is hard coded in the template
return render_pdf(html)
return render_template("collection.html",collection=collection,form=form)
mypdf.html:
{% if collection|length %}
<div class="innerBox">
{% for word in collection %}
<div class="imgcard">
<div class="imgcontainer">
<img class="card-img-top" src="{{ url_for('static',filename='images/' + word.image.name)}}"
alt="image of '{{ word.word }}'">
</div>
<div class="card-body">
<h2 class="card-title">{{ word.word }}</h2>
</div>
</div>
</div>
{% endfor %}
{% else %}
<div class="col-4">
<p>You don't have a collection yet!</p>
</div>
{% endif %}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。