在网络中使用 Python 的帮助

发布于 2024-08-05 08:25:04 字数 918 浏览 7 评论 0原文

我一直在使用 Werkzeug 来制作 WSGI 兼容的应用程序。我正在尝试修改首页的代码。

其基本思想是,您访问 /hello URL,您会得到一个“Hello World!”信息。你转到/hello/,你会得到“你好!”。例如,/hello/jeff 产生“Hello Jeff!”。不管怎样,我想做的是在首页放置一个带有文本框的表单,您可以在其中输入您的姓名,然后它将其提交到/hello。因此,如果您在表单中输入“Jeff”并提交,您会收到“Hello Jeff!”信息。

但是,我不知道该怎么做。我需要将“name”变量传递给 hello 模板,但我不知道如何传递。这是我的index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Index page</title>
</head>

<body>
<h1>Go to the <a href="${url_for('say_hello')}">default</a></h1>
<form name="helloform" action="${url_for('say_hello')}" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>

method =“get”也不起作用,可以预见。

I've been using Werkzeug to make WSGI compliant applications. I'm trying to modify the code in the front page.

Its basic idea is that you go to the /hello URL and you get a "Hello World!" message. You go to /hello/ and you get "hello !". For example, /hello/jeff yields "Hello Jeff!". Anyway, what I'm trying to do is putting a form in the front page with a text box where you can enter your name, and it will submit it to /hello. So if you enter "Jeff" in the form and submit, you get the "Hello Jeff!" message.

However, I have no idea how to do this. I need to pass the "name" variable to the hello template, but I don't know how. Here's my index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Index page</title>
</head>

<body>
<h1>Go to the <a href="${url_for('say_hello')}">default</a></h1>
<form name="helloform" action="${url_for('say_hello')}" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>

method="get" doesn't work either, predictably.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小傻瓜 2024-08-12 08:25:04

以正确的方式进行:转到 /hello?name=joe 向 joe 打招呼,等等。这就是 HTML/HTTP 的设计工作方式! /hello URL 后面的代码只需要从请求中获取 name 参数(如果存在),并做出相应的响应。

Do it the right way: go to /hello?name=joe to say hello to joe, and so forth. That's how HTML/HTTP is designed to work! Your code behind the /hello URL just needs to get the name parameter from the request, if present, and respond accordingly.

初见 2024-08-12 08:25:04

HTML 表单有一个静态目标地址 action="/something",但您希望根据用户输入更改地址
您有两个选择:

  1. 将 javascript 添加到 html 表单中
    更改目标地址(通过
    在表格前附加姓名)
    已提交。

  2. 在您的 Web 框架中编写一个读取 GET 或 POST 的新方法
    变量并将表单指向那里。

HTML Forms have a static target address, action="/something", but you want to change the address depending user input.
You have two options:

  1. Add javascript to the html form to
    change the target address (by
    appending the name) before the form
    is submitted.

  2. Write a new method in your web framework that reads GET or POST
    variables and point the form there.

素手挽清风 2024-08-12 08:25:04

直接在您提供的页面链接 (http://werkzeug.pocoo.org/) 上单击单击“单击此处”,您将获得 hello X 示例的代码。您似乎缺少的是:

Hello ${url_values['name']|h}!

html 模板中的某处(假设它是响应和请求的模板)

Directly on the page link to which you provide (http://werkzeug.pocoo.org/) when clicking on 'Click here', you get a code for the hello X example. What you seem to be missing is:

Hello ${url_values['name']|h}!

somewhere in your html template (assuming it is the template for response as well as for request)

心奴独伤 2024-08-12 08:25:04

HTML 基于 REST 原则...每个 url 都应该是一个对象,就像一个人;不是一个动作,比如向一个人打招呼。仅当您的 Web 应用程序通过查看其数据库在内部知道是谁时,才让 URL 识别您想要问候的人。

如果您的 Web 应用程序没有名为 Joe 的对象,那么设计包含 Joe 的 URL 并不是正确的方法。您的特定 Web 应用程序是为了找出人们是谁并向他们发送问候消息。因此,您可能应该有一个 URL:/greeter,它会查找 GET 或 POST 请求(来自您的表单)中发送的信息,并显示一条问候语。如果它不知道该向谁打招呼,它可以显示表格来查找。

在构建软件时,始终考虑您实际使用的对象(构成系统的组件)。

HTML is based on the REST principle... each url should be an object, like a person; NOT an action, like saying hello to a person. Have the URL identify who you want to greet only if your web application knows who that is internally, by looking at its database.

If your webapplication has no object called Joe, then designing URLs with Joe in them is not the right approach. Your particular web application is about finding out who people are and sending a hello messages to them. So, you should probably have one URL: /greeter, which looks for information sent in GET or POST requests (from your form), and displays a greeting. If it doesn't know who to greet, it can display the form to find out.

Always think in terms of the objects you're actually working with --- the components that make up the system --- when building software.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文