使用 Flask 应用程序和 Google App Engine 处理 POST 请求表单

问题描述 投票:0回答:1

我正在使用 Flask 作为后端开发我的第一个 Web 应用程序,但我在使用带有 POST 请求的表单时遇到了一些问题。该表单在我的本地计算机上运行良好,但当我将其部署到 GCP 应用程序引擎时,它不起作用。每当我单击表单的提交按钮时,页面就会无限期地加载。

查看日志时,我什至没有看到 POST 请求经过。我在 Flask 代码中添加了一些调试打印,并且 POST 函数一开始的调试打印没有显示在日志中。这让我认为这与表单/POST 请求的设置方式有关。

有人知道发生什么事吗?我是否必须做一些 CSRF 的事情(在另一个线程中看到)?在 App Engine 上处理 POST 请求时还需要执行其他操作吗?也许

app.yaml
文件中有一些配置?我很困惑,因为 POST 请求看起来根本就没有经过,所以不知道该去哪里继续寻找。

更多背景信息:该应用程序允许用户通过 iMessage 群聊中发送的 Spotify 链接创建 Spotify 播放列表。

我的表格的基本框架如下。它有一个文件输入、文本输入和一个提交按钮。

<form method="post" enctype="multipart/form-data" id="chatTracksForm" class="playlist-form">
        <h2 class="mainHeaders">1. Upload file</h2>
        <input type="file" name="file" id="file" accept=".db" class="input-file">
      
        <h2 id="header2" class="mainHeaders">2. Enter name</h2>
        <input type="text" id="chat_name" name="chat_name" placeholder=" Group chat name">

        <input type="submit" value="Create Playlist" id="btnSub">
</form>

这里是处理表单提交的 Flask 代码。清理了一些调试打印,但除此之外这是相当简单的。

@app.route('/', methods=['POST'])
def create_playlist():
    print("YO")
    cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
    auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler)
    if not auth_manager.validate_token(cache_handler.get_cached_token()):
        return redirect('/')
    sp = spotipy.Spotify(auth_manager=auth_manager)

    file = request.files['file']
    if file:
        filename = secure_filename(file.filename)
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        output = file.save(filepath)

        chat_name = request.form['chat_name']
        playlist = PlaylistFromiMessage(sp, filepath)

        ##### CREATING PLAYLIST LOGIC ##### # Getting messages - if messages return 0 that means error with input chat_name
        messages = playlist.get_messages_from_groupchat(chat_name)
        if len(messages) == 0:
            error_message = f"No messages for {chat_name} - try another group chat"
            return render_template("create_playlist.html", error_message=error_message)

        # Creating playlist
        playlist_id = playlist.create_new_playlist_if_not_exists(chat_name)
        # Getting spotify ids for messages
        spot_ids = playlist.get_spotify_ids(messages)
        # Adding tracks to playlist
        tracks_added = playlist.add_tracks_to_playlist(playlist_id, spot_ids)

        # Return success page with link to spotify playlist
        playlist_obj = sp.playlist(playlist_id)
        spotify_playlist_link = playlist_obj["external_urls"]["spotify"]
        return render_template('success.html', playlist_url=spotify_playlist_link, tracks_added=tracks_added)

    else:
        return redirect('/')
html flask google-cloud-platform google-app-engine web-applications
1个回答
0
投票
  1. 如果您说您发布的 Flask 代码将处理您的表单提交,则将

    action = "/"
    添加到您的表单中(请参阅 action 属性)

  2. 但这又引发了另一个问题。您的

    /
    路由处理程序仅支持
    POST
    ,但在同一代码块中,您将路由回
    /
    ,这是一个
    GET
    请求(但没有路由器可以处理它)。另外,我很好奇哪个路由器甚至加载您拥有表单的页面

© www.soinside.com 2019 - 2024. All rights reserved.