I
I
inalan2020-08-28 22:18:22
Flask
inalan, 2020-08-28 22:18:22

Getting None when displaying file with folder?

I'm adding a feature to my Flask app that adds a file attach field (image and files) . The file is successfully added to static/files and also string is successfully added to the database. But when displaying this file, I get static/file/None. What could be the reason?

update:
the code returns a long text from which nothing is downloaded either

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)


@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)


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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-08-28
@soremix

attachment = Attachment()
Are you taking an empty file?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question