将第二个文本区域添加到Flask应用程序

时间:2014-09-08 03:09:37

标签: python flask

我有一个简单的烧瓶应用程序。我尝试在应用程序中添加第二个文本区域以添加其他功能。我完全复制了文本区域,当我在下方框中提交时,我收到以下消息:

错误请求 浏览器(或代理)发送了此服务器无法理解的请求。

以下是从原始应用程序更改的所有代码添加了与第一个相同的第二个文本区域。看起来没问题,但是即使文本区域名称被更改,当我点击提交时问题仍然存在。我不明白服务器此时如何看到第二个和第一个框之间的差异。 Heres the app that's being changed.这个问题与你期望的一样复杂。 THX!

<!DOCTYPE html>
<html>
<head>
    <title>Ven Diagram</title>
<style type=”text/css”>
#pagearea {
width: 100%;
margin: 0 auto;
}
textarea {
width: 48%;
padding: 0 0 0 0;
margin: 0 0 0 0;
}
input {
width: 80px;
height: 40px;
}
 </style>
</head>
<body>
    <div id="pagearea">
    <h1>
    This program allows you to match text. The text must be unicode.
    Enter two text blocks to compare:
    </h1>
    <form action="/" method="post">
      <textarea name="A" cols="100" rows="20"></textarea>
      <textarea name="B" cols="100" rows="20"></textarea>
      <br />
      <input type="submit" value="Execute" />
    </form>
    </div>

 <div id="pagearea">
    <h1>
    This will give add and subtract permutations for numbers. 
    </h1>
    <form action="/" method="post">
      <textarea name="A" cols="100" rows="20"></textarea>
      <br />
      <input type="submit" value="Execute" />
    </form>
    </div>

    {% with messages = get_flashed_messages() %}
        {% if messages %}
            Results:
            <pre>
                {% for message in messages %}
{{ message }}
                {% endfor %}
            </pre>
        {% endif %}
    {% endwith %}
</body>
</html>

这是Python代码:

#!flask/bin/python
import flask, flask.views
import os
import urllib

app = flask.Flask(__name__)
app.secret_key = "REDACTED"

class View(flask.views.MethodView):
    def get(self):
        return flask.render_template('index.html')

    def post(self):
        A = flask.request.form['A']
        B = flask.request.form['B']
        A = urllib.unquote(unicode(A))
        B = urllib.unquote(unicode(B))
        C = A.split()
        D = B.split()
        Both = []
        for x in C:
            if x in D:
                Both.append(x)
        for x in range(len(Both)):
            Both[x]=str(Both[x])
        Final = []
        for x in set(Both):
            Final.append(x)
        MissingA = []
        for x in C:
            if x not in Final and x not in MissingA:
                MissingA.append(x)
        for x in range(len(MissingA)):
            MissingA[x]=str(MissingA[x])
        MissingB = []
        for x in D:
            if x not in Final and x not in MissingB:
                MissingB.append(x)
        for x in range(len(MissingB)):
            MissingB[x]=str(MissingB[x])
        #flask.flash("A:")
        #flask.flash(A)
        #flask.flash("B:")
        #flask.flash(B)
        #flask.flash("C:")
        #flask.flash(C)
        #flask.flash("D:")
        #flask.flash(D)
        flask.flash("Words in Both:")
        flask.flash(Final)
        flask.flash("Words in First Box Only:")
        flask.flash(MissingA)
        flask.flash("Words in Second Box Only:")
        flask.flash(MissingB)
        return self.get()

app.add_url_rule('/', view_func=View.as_view('main'), methods=['GET', 'POST'])

app.debug = True
if __name__ == "__main__":
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

1 个答案:

答案 0 :(得分:1)

即使你认为它有点矫枉过正。我了解到我想以各种可能的方式使用烧瓶扩展。

在您的情况下,我会建议使用wtforms flask-wtf来更好地处理任何形式。