Reputation: 608
Hat I'm trying to accomplish is to delete file from server ('static' folder, to be specific).
My jinja template:
<table>
<tr>
{% for file in files_.items %}
<td data-title="title" style="text-align: center">{{ file.title }}</td>
<td data-title="download"><a href="{{ url_for('static', filename=file.file) }}">Download</a></td>
{% if current_user.username == "admin" %}
<td data-title="delete" style="text-align: center"><a href="{{ delete }}">Delete</a></td>
{% endif %}
</tr>
{% endfor %}
</table>
and my function:
@app.route('/upload/<path:filename>/', methods=['GET', 'POST'])
@login_required
def delete(filename):
item = db.session.query(File).get(filename)
os.remove(os.path.join(app.static_folder, item.filename))
db.session.query(File).filter_by(file=filename).delete()
db.session.commit()
return render_template('dashboard.html',delete=delete)
What I'm trying to do is to after clicking on delete in html I want to delete record from database and file from the server. Right now I'm not sure if my approach to call this function is correct, since I've tried to use prints as a primitive log system and there was nothing in the terminal, co I would say function was not called. Also my guess is that I would need to pass filename to it, so Ive tried
{{ delete(filename=file.file) }}
but it returned
UndefinedError: 'delete' is undefined
Upvotes: 1
Views: 1797
Reputation: 2747
{{ delete(filename=file.file) }}
in template tells python "when rendering template, call function delete()". What you want to do is generate link which, when clicked, will call delete endpoint.
So, use {{ url_for('delete', filename=...) }}
Upvotes: 3