Flask重定向到url_for。

huangapple go评论99阅读模式
英文:

Flask redirect to url_for

问题

以下是您提供的内容的中文翻译:

有没有办法,当用户点击链接时,使用Python/Flask显示.csv文件的内容?

尝试创建一个重定向到url_for的函数,但不起作用。

英文:

I have he following data_analytics.html` which in the end provides the

Flask重定向到url_for。 view:

  1. {% extends 'base.html' %} {% block title %}Home{% endblock %}
  2. {% block content %}
  3. <!-- <h1 align="center" >Your Files</h1> -->
  4. <br />
  5. <table class="table table-bordered table-hover">
  6. <caption> Files Uploaded</caption>
  7. <tr>
  8. <th class="table-light"> # </th>
  9. <th class="table-light">File Name</th>
  10. <th class="table-light">Date</th>
  11. <th class="table-light">Access Data</th>
  12. </tr>
  13. {% for file in csv_file %}
  14. <tr>
  15. <!-- <td>{{ file.id }}</td> -->
  16. <th scope="row">1</th>
  17. <td>{{ file.data }}</td>
  18. <td>{{ file.date }}</td>
  19. <td><a href="{{ url_for('views.data_analytics', data=file.data) }}">View Data</a></td>
  20. {%endfor%}
  21. </table>
  22. {% 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"

  1. <a href="{{ url_for('views.data_analytics', data=file.data) }}" target="_blank">查看数据</a>

然后,在Python中创建一个新的函数:

  1. from flask import send_from_directory
  2. @app.route('/csv')
  3. def see_file():
  4. return send_from_directory(directory='static', path='files/cheat_sheet.pdf')

只需更改路径和目录。文件将在新标签页中打开。

英文:

try this:

In this 'a' put a 'target="_blank"'

  1. &lt;a href=&quot;{{ url_for(&#39;views.data_analytics&#39;, data=file.data) }}&quot; target=&quot;_blank&quot;&gt;View Data&lt;/a&gt;

And, in the python let's create a new function:

  1. from flask import send_from_directory
  2. @app.route(&#39;/csv&#39;)
  3. def see_file():
  4. return send_from_directory(directory=&#39;static&#39;, path=&#39;files/cheat_sheet.pdf&#39;)

Just change the path and the directory. And the file is going to open in a new tab.

huangapple
  • 本文由 发表于 2023年2月7日 01:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定