I want to generate a list of download links for an indefinite amount of files. Idea is to generate a rerouting link to (/download/). However, it gives me an error:
**werkzeug.routing.BuildError: Could not build url for endpoint 'download'. Did you forget to specify values ['filename']?**
Any idea what I'm doing wrong?? Sorry for the messy formatting, it's my first time here after one entire day of trying everything. EDIT: I've tried syntax changes and they do not work. Hence I asked this question. Here's the code.
{% extends "base.html" %}
{% block content %}
{% for file in files %}
<div><a href="{{ url_for('download', file.name) }}">{{ file.name }}</a></div>
<!-- <div><p>{{ file.name }}</p></div> -->
{% endfor %}
<div class="container">
<h1>File Input</h1>
<form method="POST" action="/upload" enctype="multipart/form-data">
<div class="form-group">
<label for="inputFile">File input</label>
<input type="file" name="inputFile">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
{% endblock %}
class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(300))
#timestamp = db.Colum(db.Datetime, index=True, default=datetime.utcnow)
data = db.Column(db.LargeBinary)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<FileContents {}>'.format(self.name)
@app.route('/download/<filename>')
def download(filename):
file_data = File.query.filter_by(name = filename).first()
return send_file(BytesIO(file_data.data),attachment_filename=filename, as_attachment=True)
@app.route('/files')
def files():
files = File.query.all()
return render_template('files.html', title='Files', files=files)