返回图像文件时出错-Python(Flask)

时间:2020-08-28 18:48:32

标签: python flask flask-sqlalchemy

我正在添加一项功能,该功能允许用户通过 flask-wtf形式添加图像/常规文件。文件已成功添加到 static / files 文件夹中,字符串名称也已添加到 database 中。但是,当我要显示它时,它会返回一些疯狂的 / s t a c / f e l e s / S E L E C T%2 0 a t t a c h m e n t。 i d%2 0 A S%2 0 a t t a c h m e n t _ i d%2 C%2 0 a t t a c h m e n t。 %2 0 A S%2 0 a t t a c h m e n t _f i l e%2 C%2 0 a t t a c h m e n t。 t i c k e t _ i d%2 0 A S%2 0 a t t a c h m e n n t _ t i c k e t _ i d%2 0%0 A F R O M%2 0 a t t a c h m e n t%2 0%0 A W H E R E%2 0%3 F%2 0%3 D%2 0 a t t a c。 ,当我尝试下载它时,还会下载没有文件参数的 None.html 。 这是我的 models.py

class Tickets(db.Model):
   id = db.Column(db.Integer,primary_key=True)
   title = db.Column(db.String(100),nullable=False)
   attach = db.relationship('Attachment', backref='ticket', lazy='dynamic')
class Attachment(db.Model):
   id = db.Column(db.Integer, primary_key=True)
   file = db.Column(db.String(140))
   ticket_id = db.Column(db.Integer, db.ForeignKey('tickets.id'), nullable=False)

routes.py

@app.route('/ticket/<ticket_id>',methods=['GET','POST'])
@login_required
def ticket(ticket_id):
    ticket = Tickets.query.get_or_404(ticket_id)
    attachform = AttachForm()
    if attachform.validate_on_submit():
        if attachform.file.data:
            picture_file = save_file(attachform.file.data)
            attachment = Attachment(file=picture_file,ticket_id=ticket_id)
            db.session.add(attachment)
            ticket.attach = picture_file
        db.session.commit()
        flash('Your file has been published.')
        return redirect(url_for('ticket', ticket_id=ticket_id))
    file = url_for('static', filename='files/' + str(ticket.attach))
    return render_template('ticket.html', title=ticket.title, file=file , ticket=ticket, attachform=attachform)

html文件

<a href="{{ file }}" download>
 {{ file }}
</a>

我在做什么错了?

0 个答案:

没有答案