Flask重定向到url_for。

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

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:

{% 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"'

&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:

from flask import send_from_directory    

@app.route(&#39;/csv&#39;)
def see_file():
   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:

确定