英文:
Flask redirect to url_for
问题
以下是您提供的内容的中文翻译:
有没有办法,当用户点击链接时,使用Python/Flask显示.csv文件的内容?
尝试创建一个重定向到url_for的函数,但不起作用。
英文:
I have he following data_analytics.html` which in the end provides the
{% extends 'base.html' %} {% block title %}Home{% endblock %}
{% block content %}
<!-- <h1 align="center" >Your Files</h1> -->
<br />
<table class="table table-bordered table-hover">
<caption> Files Uploaded</caption>
<tr>
<th class="table-light"> # </th>
<th class="table-light">File Name</th>
<th class="table-light">Date</th>
<th class="table-light">Access Data</th>
</tr>
{% for file in csv_file %}
<tr>
<!-- <td>{{ file.id }}</td> -->
<th scope="row">1</th>
<td>{{ file.data }}</td>
<td>{{ file.date }}</td>
<td><a href="{{ url_for('views.data_analytics', data=file.data) }}">View Data</a></td>
{%endfor%}
</table>
{% endblock %}
Is there a way that when the user clicks on the link to have displayed the content of the .csv file using python/flask ?
Tried to create a function that redirects to an url_for but not working`
答案1
得分: 2
尝试这样做:
在这个<a>
标签中加入target="_blank"
<a href="{{ url_for('views.data_analytics', data=file.data) }}" target="_blank">查看数据</a>
然后,在Python中创建一个新的函数:
from flask import send_from_directory
@app.route('/csv')
def see_file():
return send_from_directory(directory='static', path='files/cheat_sheet.pdf')
只需更改路径和目录。文件将在新标签页中打开。
英文:
try this:
In this 'a' put a 'target="_blank"'
<a href="{{ url_for('views.data_analytics', data=file.data) }}" target="_blank">View Data</a>
And, in the python let's create a new function:
from flask import send_from_directory
@app.route('/csv')
def see_file():
return send_from_directory(directory='static', path='files/cheat_sheet.pdf')
Just change the path and the directory. And the file is going to open in a new tab.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论