1

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)

1 Answers1

4

url_for takes keyword arguments, not positional ones. It should be:

{{ url_for('download', filename=file.name) }}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I tried this as well! It does not work. Which leads me to think there is a different problem or even maybe design choice I'm overlooking. – carrotwizard _ Jul 26 '18 at 17:17