Flask和Python,尝试从表单读取时不允许使用Method

时间:2019-02-16 18:03:21

标签: python html flask

我想从表单中读取四个用户名,将它们放入def index中,然后根据变量ilu等多少不是空字符串来设置变量username1,但是在键入后用户名并点击gotowe按钮,我将看到方法不允许:(

我在这里看到了其他类似的问题,但是很抱歉,我无法理解解决方案。

这是我第一次来,对于这个问题中的所有错误我深表歉意。 我几乎失去了所有希望:<我不知道该怎么办。

nazwy_graczy.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% block form %}

    <form method="post" action="http://localhost:5000/index">
        <div>
            <p><input type="text" class="form-control" id="username1" name="username" placeholder="Wpisz nazwę użytkownika"></p>
            <p><input type="text" class="form-control" id="username2" name="username" placeholder="Wpisz nazwę użytkownika"></p>
            <p><input type="text" class="form-control" id="username3" name="username" placeholder="Wpisz nazwę użytkownika"></p>
            <p><input type="text" class="form-control" id="username4" name="username" placeholder="Wpisz nazwę użytkownika"></p>
        </div>

    </form>

<a href="/index"><button type="submit" class="btn btn-primary">Gotowe</button></a>


{% endblock %}

</body>
</html>

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Strona</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    {% extends "hand1.html" %}
    {% block tabela%}

     <table style="width:100%">
      <tr>
        <th>Gracz</th>
        <th>Phase</th>
        <th>Punkty</th>
      </tr>

      <tr style="text-align=center; ">
        <td>{{user1}}</td>
        <td>{{point}}</td>
        <td>{{phase}}</td>
      </tr>
        <tr style="text-align=center; ">
        <td>{{user2}}</td>
        <td>{{point}}</td>
        <td>{{phase}}</td>
      </tr>
        <tr style="text-align=center; ">
        <td>{{user3}}</td>
        <td>{{point}}</td>
        <td>{{phase}}</td>
      </tr>
        <tr style="text-align=center; ">
        <td>{{user4}}</td>
        <td>{{point}}</td>
        <td>{{phase}}</td>
      </tr>

    </table>
    {% endblock %}
</body>
</html>

app.py:

from flask import Flask, render_template, request
from players import Player
import engine
import cards
from random import shuffle

app = Flask(__name__)

# globals start  -----------------------------------------------------

rejected_card = cards.CardsNumbers(1, 1)
dash_length = 140
Q = 0
real_players = []
real_players_f_phases = []
player_decks = []
main_deck = []
players_phases = []
players_names = []
is_new_game = True

# globals end  -------------------------------------------------------


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


@app.route('/index', methods=['GET, POST'])
def index():
    global players_names
    global ilu
    if request.method == 'POST':
        user1 = request.form['username1']
        user2 = request.form['username2']
        user3 = request.form['username3']
        user4 = request.form['username4']

        players_names = [user1, user2, user3, user4]
        ilu = 0

        for u in list(players_names):
            if u != '':
                ilu += 1
    else:
        return 0

    return render_template('index.html', ilu=ilu, user1=user1, user2=user2, user3=user3, user4=user4)


@app.route('/hand1')
def hand1():
    global players_names
    global is_new_game
    global ilu

    if is_new_game:
        is_new_game = False

        new_game(ilu)

    user = players_names[0]
    user.sort_cards_by_number()

    return render_template('hand1.html', hand1=user.show_cards())


def new_game(uniq_amount):
    global real_players
    global real_players_f_phases
    global players_decks
    global main_deck
    global players_phases
    global players_names
    global Q

    Q = engine.queue(uniq_amount)

    all_cards = cards.rozdaj_karty(uniq_amount)
    players_decks = all_cards[0:-1]
    main_deck = all_cards[-1]
    shuffle(main_deck)

    for i in range(uniq_amount):
        real_players.append(Player(players_names[i], players_decks[i]))

    for i in range(len(real_players)):
        real_players_f_phases.append(False)


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

查看页面来源:

第一页:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="post" action="http://localhost:5000/index">
    <div>
        <p><input type="text" class="form-control" id="username1" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
        <p><input type="text" class="form-control" id="username2" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
        <p><input type="text" class="form-control" id="username3" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
        <p><input type="text" class="form-control" id="username4" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
    </div>

    <input type="submit" value="Submit">
</form>


</body>
</html>

错误:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>werkzeug.exceptions.HTTPException.wrap.&lt;locals&gt;.newcls: 400 Bad Request: KeyError: 'username1' // Werkzeug Debugger</title>
    <link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css"
        type="text/css">
    <!-- We need to make sure this has a favicon so that the debugger does
         not by accident trigger a request to /favicon.ico which might
         change the application state. -->
    <link rel="shortcut icon"
        href="?__debugger__=yes&amp;cmd=resource&amp;f=console.png">
    <script src="?__debugger__=yes&amp;cmd=resource&amp;f=jquery.js"></script>
    <script src="?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js"></script>
    <script type="text/javascript">
      var TRACEBACK = 140589934758992,
          CONSOLE_MODE = false,
          EVALEX = true,
          EVALEX_TRUSTED = false,
          SECRET = "NYIRoPSadEHEGDJ1PkU6";
    </script>
  </head>
  <body style="background-color: #fff">
    <div class="debugger">
<h1>werkzeug.exceptions.BadRequestKeyError</h1>
<div class="detail">
  <p class="errormsg">werkzeug.exceptions.HTTPException.wrap.&lt;locals&gt;.newcls: 400 Bad Request: KeyError: 'username1'</p>
</div>
<h2 class="traceback">Traceback <em>(most recent call last)</em></h2>
<div class="traceback">

  <ul><li><div class="frame" id="frame-140589917417656">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">2309</em>,
      in <code class="function">__call__</code></h4>
  <div class="source"><pre class="line before"><span class="ws"></span> </pre>
<pre class="line before"><span class="ws">    </span>def __call__(self, environ, start_response):</pre>
<pre class="line before"><span class="ws">        </span>&quot;&quot;&quot;The WSGI server calls the Flask application object as the</pre>
<pre class="line before"><span class="ws">        </span>WSGI application. This calls :meth:`wsgi_app` which can be</pre>
<pre class="line before"><span class="ws">        </span>wrapped to applying middleware.&quot;&quot;&quot;</pre>
<pre class="line current"><span class="ws">        </span>return self.wsgi_app(environ, start_response)</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>def __repr__(self):</pre>
<pre class="line after"><span class="ws">        </span>return '&lt;%s %r&gt;' % (</pre>
<pre class="line after"><span class="ws">            </span>self.__class__.__name__,</pre>
<pre class="line after"><span class="ws">            </span>self.name,</pre></div>
</div>

<li><div class="frame" id="frame-140589917419112">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">2295</em>,
      in <code class="function">wsgi_app</code></h4>
  <div class="source"><pre class="line before"><span class="ws">            </span>try:</pre>
<pre class="line before"><span class="ws">                </span>ctx.push()</pre>
<pre class="line before"><span class="ws">                </span>response = self.full_dispatch_request()</pre>
<pre class="line before"><span class="ws">            </span>except Exception as e:</pre>
<pre class="line before"><span class="ws">                </span>error = e</pre>
<pre class="line current"><span class="ws">                </span>response = self.handle_exception(e)</pre>
<pre class="line after"><span class="ws">            </span>except:</pre>
<pre class="line after"><span class="ws">                </span>error = sys.exc_info()[1]</pre>
<pre class="line after"><span class="ws">                </span>raise</pre>
<pre class="line after"><span class="ws">            </span>return response(environ, start_response)</pre>
<pre class="line after"><span class="ws">        </span>finally:</pre></div>
</div>

<li><div class="frame" id="frame-140589917419168">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">1741</em>,
      in <code class="function">handle_exception</code></h4>
  <div class="source"><pre class="line before"><span class="ws">            </span># if we want to repropagate the exception, we can attempt to</pre>
<pre class="line before"><span class="ws">            </span># raise it with the whole traceback in case we can do that</pre>
<pre class="line before"><span class="ws">            </span># (the function was actually called from the except part)</pre>
<pre class="line before"><span class="ws">            </span># otherwise, we just raise the error again</pre>
<pre class="line before"><span class="ws">            </span>if exc_value is e:</pre>
<pre class="line current"><span class="ws">                </span>reraise(exc_type, exc_value, tb)</pre>
<pre class="line after"><span class="ws">            </span>else:</pre>
<pre class="line after"><span class="ws">                </span>raise e</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">        </span>self.log_exception((exc_type, exc_value, tb))</pre>
<pre class="line after"><span class="ws">        </span>if handler is None:</pre></div>
</div>

<li><div class="frame" id="frame-140589917419224">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/_compat.py"</cite>,
      line <em class="line">35</em>,
      in <code class="function">reraise</code></h4>
  <div class="source"><pre class="line before"><span class="ws">    </span>from io import StringIO</pre>
<pre class="line before"><span class="ws"></span> </pre>
<pre class="line before"><span class="ws">    </span>def reraise(tp, value, tb=None):</pre>
<pre class="line before"><span class="ws">        </span>if value.__traceback__ is not tb:</pre>
<pre class="line before"><span class="ws">            </span>raise value.with_traceback(tb)</pre>
<pre class="line current"><span class="ws">        </span>raise value</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>implements_to_string = _identity</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws"></span>else:</pre>
<pre class="line after"><span class="ws">    </span>text_type = unicode</pre></div>
</div>

<li><div class="frame" id="frame-140589917419056">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">2292</em>,
      in <code class="function">wsgi_app</code></h4>
  <div class="source"><pre class="line before"><span class="ws">        </span>ctx = self.request_context(environ)</pre>
<pre class="line before"><span class="ws">        </span>error = None</pre>
<pre class="line before"><span class="ws">        </span>try:</pre>
<pre class="line before"><span class="ws">            </span>try:</pre>
<pre class="line before"><span class="ws">                </span>ctx.push()</pre>
<pre class="line current"><span class="ws">                </span>response = self.full_dispatch_request()</pre>
<pre class="line after"><span class="ws">            </span>except Exception as e:</pre>
<pre class="line after"><span class="ws">                </span>error = e</pre>
<pre class="line after"><span class="ws">                </span>response = self.handle_exception(e)</pre>
<pre class="line after"><span class="ws">            </span>except:</pre>
<pre class="line after"><span class="ws">                </span>error = sys.exc_info()[1]</pre></div>
</div>

<li><div class="frame" id="frame-140589917419336">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">1815</em>,
      in <code class="function">full_dispatch_request</code></h4>
  <div class="source"><pre class="line before"><span class="ws">            </span>request_started.send(self)</pre>
<pre class="line before"><span class="ws">            </span>rv = self.preprocess_request()</pre>
<pre class="line before"><span class="ws">            </span>if rv is None:</pre>
<pre class="line before"><span class="ws">                </span>rv = self.dispatch_request()</pre>
<pre class="line before"><span class="ws">        </span>except Exception as e:</pre>
<pre class="line current"><span class="ws">            </span>rv = self.handle_user_exception(e)</pre>
<pre class="line after"><span class="ws">        </span>return self.finalize_request(rv)</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>def finalize_request(self, rv, from_error_handler=False):</pre>
<pre class="line after"><span class="ws">        </span>&quot;&quot;&quot;Given the return value from a view function this finalizes</pre>
<pre class="line after"><span class="ws">        </span>the request by converting it into a response and invoking the</pre></div>
</div>

<li><div class="frame" id="frame-140589917419392">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">1718</em>,
      in <code class="function">handle_user_exception</code></h4>
  <div class="source"><pre class="line before"><span class="ws">            </span>return self.handle_http_exception(e)</pre>
<pre class="line before"><span class="ws"></span> </pre>
<pre class="line before"><span class="ws">        </span>handler = self._find_error_handler(e)</pre>
<pre class="line before"><span class="ws"></span> </pre>
<pre class="line before"><span class="ws">        </span>if handler is None:</pre>
<pre class="line current"><span class="ws">            </span>reraise(exc_type, exc_value, tb)</pre>
<pre class="line after"><span class="ws">        </span>return handler(e)</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>def handle_exception(self, e):</pre>
<pre class="line after"><span class="ws">        </span>&quot;&quot;&quot;Default exception handling that kicks in when an exception</pre>
<pre class="line after"><span class="ws">        </span>occurs that is not caught.  In debug mode the exception will</pre></div>
</div>

<li><div class="frame" id="frame-140589917419448">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/_compat.py"</cite>,
      line <em class="line">35</em>,
      in <code class="function">reraise</code></h4>
  <div class="source"><pre class="line before"><span class="ws">    </span>from io import StringIO</pre>
<pre class="line before"><span class="ws"></span> </pre>
<pre class="line before"><span class="ws">    </span>def reraise(tp, value, tb=None):</pre>
<pre class="line before"><span class="ws">        </span>if value.__traceback__ is not tb:</pre>
<pre class="line before"><span class="ws">            </span>raise value.with_traceback(tb)</pre>
<pre class="line current"><span class="ws">        </span>raise value</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>implements_to_string = _identity</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws"></span>else:</pre>
<pre class="line after"><span class="ws">    </span>text_type = unicode</pre></div>
</div>

<li><div class="frame" id="frame-140589917419280">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">1813</em>,
      in <code class="function">full_dispatch_request</code></h4>
  <div class="source"><pre class="line before"><span class="ws">        </span>self.try_trigger_before_first_request_functions()</pre>
<pre class="line before"><span class="ws">        </span>try:</pre>
<pre class="line before"><span class="ws">            </span>request_started.send(self)</pre>
<pre class="line before"><span class="ws">            </span>rv = self.preprocess_request()</pre>
<pre class="line before"><span class="ws">            </span>if rv is None:</pre>
<pre class="line current"><span class="ws">                </span>rv = self.dispatch_request()</pre>
<pre class="line after"><span class="ws">        </span>except Exception as e:</pre>
<pre class="line after"><span class="ws">            </span>rv = self.handle_user_exception(e)</pre>
<pre class="line after"><span class="ws">        </span>return self.finalize_request(rv)</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>def finalize_request(self, rv, from_error_handler=False):</pre></div>
</div>

<li><div class="frame" id="frame-140589917419560">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/flask/app.py"</cite>,
      line <em class="line">1799</em>,
      in <code class="function">dispatch_request</code></h4>
  <div class="source"><pre class="line before"><span class="ws">        </span># request came with the OPTIONS method, reply automatically</pre>
<pre class="line before"><span class="ws">        </span>if getattr(rule, 'provide_automatic_options', False) \</pre>
<pre class="line before"><span class="ws">           </span>and req.method == 'OPTIONS':</pre>
<pre class="line before"><span class="ws">            </span>return self.make_default_options_response()</pre>
<pre class="line before"><span class="ws">        </span># otherwise dispatch to the handler for that endpoint</pre>
<pre class="line current"><span class="ws">        </span>return self.view_functions[rule.endpoint](**req.view_args)</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>def full_dispatch_request(self):</pre>
<pre class="line after"><span class="ws">        </span>&quot;&quot;&quot;Dispatches the request and on top of that performs request</pre>
<pre class="line after"><span class="ws">        </span>pre and postprocessing as well as HTTP exception catching and</pre>
<pre class="line after"><span class="ws">        </span>error handling.</pre></div>
</div>

<li><div class="frame" id="frame-140589917419616">
  <h4>File <cite class="filename">"/root/PycharmProjects/projekt_ig/app.py"</cite>,
      line <em class="line">35</em>,
      in <code class="function">index</code></h4>
  <div class="source"><pre class="line before"><span class="ws"></span>@app.route('/index', methods=['POST'])</pre>
<pre class="line before"><span class="ws"></span>def index():</pre>
<pre class="line before"><span class="ws">    </span>global players_names</pre>
<pre class="line before"><span class="ws">    </span>global ilu</pre>
<pre class="line before"><span class="ws">    </span>if request.method == 'POST':</pre>
<pre class="line current"><span class="ws">        </span>user1 = request.form['username1']</pre>
<pre class="line after"><span class="ws">        </span>user2 = request.form['username2']</pre>
<pre class="line after"><span class="ws">        </span>user3 = request.form['username3']</pre>
<pre class="line after"><span class="ws">        </span>user4 = request.form['username4']</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">        </span>players_names = [user1, user2, user3, user4]</pre></div>
</div>

<li><div class="frame" id="frame-140589917417712">
  <h4>File <cite class="filename">"/usr/local/lib/python3.7/dist-packages/werkzeug/datastructures.py"</cite>,
      line <em class="line">431</em>,
      in <code class="function">__getitem__</code></h4>
  <div class="source"><pre class="line before"><span class="ws">        </span>&quot;&quot;&quot;</pre>
<pre class="line before"><span class="ws">        </span>if key in self:</pre>
<pre class="line before"><span class="ws">            </span>lst = dict.__getitem__(self, key)</pre>
<pre class="line before"><span class="ws">            </span>if len(lst) &gt; 0:</pre>
<pre class="line before"><span class="ws">                </span>return lst[0]</pre>
<pre class="line current"><span class="ws">        </span>raise exceptions.BadRequestKeyError(key)</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">    </span>def __setitem__(self, key, value):</pre>
<pre class="line after"><span class="ws">        </span>&quot;&quot;&quot;Like :meth:`add` but removes an existing key first.</pre>
<pre class="line after"><span class="ws"></span> </pre>
<pre class="line after"><span class="ws">        </span>:param key: the key for the value.</pre></div>
</div>
</ul>
  <blockquote>werkzeug.exceptions.HTTPException.wrap.&lt;locals&gt;.newcls: 400 Bad Request: KeyError: 'username1'</blockquote>
</div>

<div class="plain">
  <form action="/?__debugger__=yes&amp;cmd=paste" method="post">
    <p>
      <input type="hidden" name="language" value="pytb">
      This is the Copy/Paste friendly version of the traceback.  <span
      class="pastemessage">You can also paste this traceback into
      a <a href="https://gist.github.com/">gist</a>:
      <input type="submit" value="create paste"></span>
    </p>
    <textarea cols="50" rows="10" name="code" readonly>Traceback (most recent call last):
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 2295, in wsgi_app
    response = self.handle_exception(e)
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/_compat.py&quot;, line 35, in reraise
    raise value
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/_compat.py&quot;, line 35, in reraise
    raise value
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File &quot;/usr/local/lib/python3.7/dist-packages/flask/app.py&quot;, line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File &quot;/root/PycharmProjects/projekt_ig/app.py&quot;, line 35, in index
    user1 = request.form['username1']
  File &quot;/usr/local/lib/python3.7/dist-packages/werkzeug/datastructures.py&quot;, line 431, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.HTTPException.wrap.&lt;locals&gt;.newcls: 400 Bad Request: KeyError: 'username1'</textarea>
  </form>
</div>
<div class="explanation">
  The debugger caught an exception in your WSGI application.  You can now
  look at the traceback which led to the error.  <span class="nojavascript">
  If you enable JavaScript you can also use additional features such as code
  execution (if the evalex feature is enabled), automatic pasting of the
  exceptions and much more.</span>
</div>
      <div class="footer">
        Brought to you by <strong class="arthur">DON'T PANIC</strong>, your
        friendly Werkzeug powered traceback interpreter.
      </div>
    </div>

    <div class="pin-prompt">
      <div class="inner">
        <h3>Console Locked</h3>
        <p>
          The console is locked and needs to be unlocked by entering the PIN.
          You can find the PIN printed out on the standard output of your
          shell that runs the server.
        <form>
          <p>PIN:
            <input type=text name=pin size=14>
            <input type=submit name=btn value="Confirm Pin">
        </form>
      </div>
    </div>
  </body>
</html>

<!--

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/root/PycharmProjects/projekt_ig/app.py", line 35, in index
    user1 = request.form['username1']
  File "/usr/local/lib/python3.7/dist-packages/werkzeug/datastructures.py", line 431, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'username1'

-->

2 个答案:

答案 0 :(得分:0)

尝试将nazwy_graczy.html修改为以下内容:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="post" action="http://localhost:5000/index">
    <div>
        <p><input type="text" class="form-control" id="username1" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
        <p><input type="text" class="form-control" id="username2" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
        <p><input type="text" class="form-control" id="username3" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
        <p><input type="text" class="form-control" id="username4" name="username" placeholder="Wpisz nazwę użytkownika">
        </p>
    </div>

    <input type="submit" value="Submit">
</form>


</body>
</html>

答案 1 :(得分:0)

我找到了答案!我要做的就是在name

中区分<input type="text" class="form-control" id="username1" name="username1" placeholder="Wpisz nazwę użytkownika">
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    {% block form %}

        <form method="post" action="http://localhost:5000/index">
            <div>
                <p><input type="text" class="form-control" id="username1" name="username1" placeholder="Wpisz nazwę użytkownika"></p>
                <p><input type="text" class="form-control" id="username2" name="username2" placeholder="Wpisz nazwę użytkownika"></p>
                <p><input type="text" class="form-control" id="username3" name="username3" placeholder="Wpisz nazwę użytkownika"></p>
                <p><input type="text" class="form-control" id="username4" name="username4" placeholder="Wpisz nazwę użytkownika"></p>
            </div>

        </form>