POST 瓶子中的方法 405 和 404 错误

POST method 405 and 404 errors in bottle

我对 Python 和 Bottle 以及服务器 requests/responses 的工作方式非常陌生,我仍然有很多基本的和一般的想法,我正在努力思考。话虽这么说,但我遗漏了一些基本逻辑。 为了熟悉 bottle,python,html 等,我有这个非常基础的项目。我有一个用 html 编写的表单(存储在 tpl 文件中),其中包含一些收音机、复选框、select 菜单和文本输入字段。我想存储所有这些输入并在用户单击提交时将它们打印到新的结果页面。作为一种存储所有变量的基本试验,我只从名字变量开始,我想将存储的变量打印到 url ('/fname')。如果我能让这个工作,那么我的计划是更改 post 方法以路由到 ('/fanpage/results') 然后 return 该页面上的所有信息(可能使用另一个模板,但我还没有走到那一步)

from bottle import route, run, post, get, request, template, static_file

HOST = 'localhost'

(...)

@post('/fname')
def show_fname():
    fname = request.forms.get('fname')
    return "<p>Your name is {{fname}}.</p>"

(...)

run(host=HOST, port=8080, debug=True)
(...)
<form action="/fanpage/results" class="needs-validated">
(...)
  <div class="row">
    <div class="col>
      <label for="fname" class="form-label">First name:</label>
      <input type="first name" class="form-control" id="fname" name="fname" placeholder="Enter first name" title="Please enter your first name" required>
      <div class="invalid-feedback">Please fill out this field to continue.</div>
    </div>
  </div>
(...)
</form>

我在 post 方法之前有一些路由和获取方法将 tpl 文件发送到服务器以显示表单,这些方法有效。但是,当我尝试转到 localhost:8080/fname 时,出现 405 错误 - 方法不允许。我觉得我是在直接复制我在文档和网上看到的内容,我不确定为什么 url 不起作用。如果我尝试将 post 方法的路由更改为 url('/fanpage/results'),我会收到 404 未找到错误。

还有一个基本问题:如果我正在处理多个模板文件并尝试仅从其中一个模板中提取信息(例如名字),它如何知道从哪个模板中提取数据?这可能是 405 错误的原因吗?

Also, a basic question: If I'm working with multiple template files and trying to pull information, such as the first name, from just one of the templates, how does it know which template to pull data from? Could that be the reason for the 405 error?

没有数据是从模板中“提取”出来的。工作流程是这样的:

  1. 客户端向服务器路由发送请求。请求通常是 GET 或 POST 请求方法,路由类似于 localhost:8080/fname。服务器必须公开匹配的请求方法和路由。
  2. 服务器对随请求发送的数据(如果有)采取行动并发送响应。响应可能是一个简单的状态代码(如 405 Method Not Allowed),或者它可能带有主体数据负载。正文可以是 HTML 页面、CSS、JSON 或其他内容。要构建 HTML 页面,服务器需要将动态数据注入模板。其他时候,HTML 是纯静态的(不变的)。
  3. 当从服务器收到响应时,客户端(可以是 Web 浏览器或 curl 或 postman 等实用程序)以其设计的任何形式向用户显示响应(Web 浏览器呈现 [=例如,56=],这通常涉及启动更多请求以获取 HTML 中包含的相关 CSS 文件、脚本或图像。

现在,假设响应是 HTML,可能是在服务器上呈现模板的结果。如果 HTML 中有一个表单(或异步触发请求的 JS 代码,避免页面刷新),用户可以 re-submit 表单并重新启动该过程。

还有一件事:当您将浏览器导航到 https://en.wikipedia.orglocalhost:8080/fname 时,这是一个 GET 请求,而不是 POST 请求,这解释了 405 响应代码,因为你告诉 /fname 只响应 POST 请求。

如果您想测试 POST 路由,请使用 curl、postman 或编写一个简单的 GET 路由,该路由以类似 <form action="/fname" method="post">.

的形式响应

这是所有这些连接在一起的最小示例:

server.py

from bottle import post, request, route, run, template

@route("/")
def index():
    return template("index.tpl")

@post("/fname")
def fname():
    return template("fname.tpl", fname=request.forms.get("fname"))

if __name__ == "__main__":
    run(host="localhost", port=8080, debug=True, reloader=True)

index.tpl

<!DOCTYPE html>
<html>
<body>
  <form action="/fname" method="post">
    <label for="fname">First name:</label>
    <input name="fname" placeholder="Enter first name" required>
    <input type="submit">
  </form>
</body>
</html>

fname.tpl

<!DOCTYPE html>
<html>
<body>
  <p>Your name is {{fname}}.</p>
  <p><a href="/">Back</a></p>
</body>
</html>

您可以启动服务器,然后导航到 http://localhost:8080 以填写表单并查看结果,或者在命令行上使用 curl 等实用程序 POST fname:

PS C:\Users\me> curl -X POST -F 'fname=foo' http://localhost:8080/fname
<!DOCTYPE html>
<html>
<body>
  <p>Your name is foo.</p>
  <p><a href="/">Back</a></p>
</body>
</html>

您可以看到服务器响应 HTML,这是将 POST 有效负载中的 fname 变量注入到 fname.tpl 模板的结果。浏览器会将其呈现为网页,但在命令行上您只会看到 HTML 代码。在JSONAPI中,命令行的响应会更容易操作,但这里的服务器是为使用浏览器的客户端设计的。