繁体   English   中英

向Flask应用添加第二个文本区域

[英]Adding a Second Text Area to a Flask App

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

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

这是从原始应用更改的代码,它添加了与第一个文本区域相同的第二个文本区域。 看起来还可以,但是即使更改了文本区域名称,当我单击“提交”时,问题仍然存在。 我不明白服务器在这一点上如何看待第二盒和第一盒之间的差异。 这是正在更改的应用程序。 这个问题与您期望的一样复杂。 谢谢!

<!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)

即使您可能认为这太过分了。 我了解到我想以各种可能的方式使用flask扩展。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM