【问题标题】:Flask in PythonAnywhere doesn't find file name with spanish charactersPythonAnywhere 中的 Flask 找不到带有西班牙语字符的文件名
【发布时间】:2020-09-03 05:58:49
【问题描述】:

我正在尝试构建一个网站,该网站有一个用于下载某些资源的页面,而我的本地烧瓶版本恰好可以找到任何文件名(使用 send_from_directory() 时),但是一旦部署在 PythonAnywhere 上,它就不会'不适用于带有西班牙重音字符的文件名,例如á

我猜它与 unicode 有关,但我找不到如何修复它(pythonanywhere 的日志似乎没有显示任何内容,因为烧瓶只是向用户提供“未找到”页面) .

...我真的很想在人们下载的文件的名称中加上这些口音(它们是 anki 套牌,其中一些用于教育目的,在牌组名称)。

我的代码如下所示:

@app.route('/anki/d/<file>')
def d_anki(file):
   if file == "verbscat":
       ankideck = "[Rusca] Temps Verbals Catalans.apkg"
   elif file == "irregular":
       ankideck = "[Rusca] Verbs Irregulars Anglès.apkg"
   # ...
   else:
       return f"The file {file} wasn't found."
   return send_from_directory("./static/anki/", ankideck, as_attachment=True, cache_timeout=0)

(然后我通过&lt;a href="/anki/d/irregular" ...&gt;在按钮中链接到此网址)

【问题讨论】:

    标签: file flask unicode pythonanywhere


    【解决方案1】:

    哦,我刚刚意识到我可以通过将attachment_filename="Whatever I want to call it" 添加到send_from_directory 中的参数来为下载的文件选择不同的名称。

    所以我想我们可以使用这种解决方法(让原始文件具有简单的非重音名称,然后添加正确的名称)。

    if file == "irregular":
        ankideck = "irregular.apkg"
        name = "[Rusca] Verbs Irregulars Anglès.apkg"
    # ...
    return send_from_directory("./static/anki/", ankideck, as_attachment=True, attachment_filename=name cache_timeout=0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 2012-09-28
      • 2017-05-19
      • 2013-07-03
      • 2023-03-18
      • 2011-10-30
      • 1970-01-01
      相关资源
      最近更新 更多