6

HTML でチェックボックスを使用し、これらのチェックボックスを Python バックエンドに戻し、ボックスがクリックされると 3 つのカウンターをインクリメントしようとしています。

現在、私の HTML は次のようになり、正常に動作します。

<form method="post">
    <input type="checkbox inline" name="adjective" value="entertaining">Entertaining
    <input type="checkbox inline" name="adjective" value="informative">Informative
    <input type="checkbox inline" name="adjective" value="exceptional">Exceptional
</form>

そして、私のpythonバックエンドには次のものがあります:

def post(self):
    adjective = self.request.get('adjective ')

    if adjective :
        #somehow tell if the entertaining box was checked
        #increment entertaining counter
        #do the same for the others
4

2 に答える 2

8

nameフォームに同じ属性を持つ複数のチェックボックスがある場合、フォームが送信されると、リクエストはその名前に対して複数の値を持ちます。

現在のコードはRequest.get値を取得するために使用していますが、これは複数の値がある場合にのみ最初の値を取得します。Request.get_all(name)代わりに、 (webapp) またはRequest.get(name, allow_multiple=True)(webapp2)を使用してすべての値を取得できます。これは、その名前のすべての値を含む (おそらく空の) リストを返します。

コードで in を使用する方法は次のとおりです。

def post(self):
    adjectives = self.request.get('adjective', allow_multiple=True)
    for a in adjectives:
        # increment count
        self.adjective_count[a] += 1 # or whatever

        # do more stuff with adjective a, if you want

    # do other stuff with the request
于 2012-11-06T00:33:47.707 に答える
0

name="" を変更して値と同じにする方が簡単ではないので、尋ねることができます

面白い場合 :

有益な場合:

私はPythonプログラマーではありません

于 2012-11-06T00:21:36.997 に答える