【问题标题】:Flask Form 500 Internal Server ErrorFlask Form 500 内部服务器错误
【发布时间】:2015-12-09 16:06:14
【问题描述】:

我正在尝试让表单在我的烧瓶网络应用程序(Twitter 克隆)中执行某些操作,但每当我添加时:

action={{ url_for('tweet') }}

对我的表格,它给了我一个

500 内部服务器错误

并在终端显示

“GET / HTTP/1.1”500 -

我在网上寻找解决方案,但找不到任何有用的方法。 我只是让我的表单在屏幕上返回一个简单的字符串。当我尝试加载索引页面而不是使用表单时,我也遇到了错误。

这里有什么关于我做错了什么的想法吗?我正在学习在线教程,并且确信我已正确按照说明进行操作。

以下是相关代码:

我的 html:

<!DOCTYPE html>
<html>

<head>
    <title>Fake Twitter</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='styles/css.css') }}">
    <link rel="shortcut icon" href="{{ url_for('static',filename='images/favicon.ico') }}" type="image/x-icon" />
</head>

<body>
    <img id="logo" src="{{ url_for('static',filename='images/logo.png') }}">

    <section>

        <div id="leftDiv">

            <!-- <button id="composeButton" type="button" onclick="tweetForm()">Compose Tweet</button> -->


            <form id="tweetForm" name="tweetForm" action="{{ url_for('tweet') }}" method="POST">

            <h1>Compose Tweet</h1>

                <table>
                    <tr>
                        <td>
                            <label>Username</label>
                        </td>
                        <td>
                            <input type="text" id="usernameField" name="username">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Tweet<br><i>(Max 140 chars)</i></label>
                        </td>
                        <td>
                            <textarea id="tweetContentField" name="tweetContent" size="140"></textarea>
                        </td>
                    </tr>

                <tr><td colspan="2" align="center">
                <button id="sendTweet">Send Tweet</button>
                </td></tr>
                </table>

            </form>

        </div>

        <div id="rightDiv">

            <div class="tweetDiv">

                <p class="user">@DarachRonayne</p>

                <p class="tweetContent">Neque porro quisquam est qui dolorem ipsum quia dolor amet, consectetur, adipisci velit. Neque poro quisquam est qui dolorem. Neque porrrro.</p>

                <p class="timestamp">Timestamp Here</p>

                <button class="deleteButton" type="button">Delete Tweet</button>

            </div>

        </div>

    </section>

</body>

<script src="{{ url_for('static',filename='js/js.js') }}"></script>

</html>

我的 Python 文件:

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/tweet', methods=['POST'])
def tweet():
    return 'TWEET TWEET!'

if __name__ == '__main__':
    app.run()

app.run(debug=True, port=8080, host='0.0.0.0')

【问题讨论】:

  • action={{ url_for('tweet') }} 可能会引发服务器错误,因为您没有将其导入为 url_for()。使用action={{ flask.url_for('tweet') }}from flask import render_template, url_for 请参阅docs
  • @albert 成功了。谢谢

标签: python html flask


【解决方案1】:

action={{ url_for('tweet') }} 可能会引发服务器错误,因为您没有将其导入为 url_for()。使用action={{ flask.url_for('tweet') }}from flask import render_template, url_for

docs

albert发表的评论

【讨论】: