A
A
Asprod2021-10-31 17:36:36
Flask
Asprod, 2021-10-31 17:36:36

Will I answer this question?

Here I used if request.form["btn"] == "Changepass": to accept multiple forms on the same page

if request.method == 'POST':
            if(request.data):
                json = request.get_json(force=True)
                print(json["id"])
                cursor.execute(
                    "DELETE FROM domains WHERE id='" + json["id"] + "'")
                conn.commit()
            else:
                if request.form["btn"] == "Changepass":
                    newpass = str(request.form.get('newpass'))
                    newpass1 = str(generate_password_hash(newpass))
                    cursor.execute("UPDATE users SET Password='" +
                                newpass1 + "' WHERE id=?", (id,))
                    conn.commit()
                    session.clear()
                    return redirect('/logout')
                else:
                    if request.form["btn_adddomain"] == "Adddomain":
                        domain = str(request.form.get('domain'))
                        cursor.execute("SELECT * FROM domains WHERE domain=?", (domain,))
                        if cursor.fetchone() is None:
                            bot.send_message(
                                config.tc, f"Завка на добавление домена  {domain}")
                            cursor.execute(
                                f"INSERT INTO domains (user, domain) VALUES (?, ?)", (id, domain,))
                            conn.commit()
                        else:
                            return {
                                "success": "False",
                            }
                        return redirect("/domains")


Here is the html form
<form action="" method='post' id="AddDomainForm">
                        <span>Введите домен в формате "example.com"</span>
                        <p style="margin-top: 5px;">
                            <input required type="text" class="form__input" placeholder="Домен" name="domain"></p>
                </div>
                <div class="modal-footer">
                    <button type="submit" style="border-radius: 6px;" name="btn_adddomain" value="Adddomain">Добавить</button>

The problem is that the first condition returns
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/root/web/panel.py", line 591, in domains
    if request.form["btn"] == "Changepass":
  File "/usr/local/lib/python3.9/dist-packages/werkzeug/datastructures.py", line 377, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'btn'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-10-31
@sergey-gornostaev

I bet you don't answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question