Python 处理 Web 表单

发布于 2024-08-31 08:33:44 字数 468 浏览 2 评论 0原文

我在使用 ascii 文件中指定的字段生成 Web 表单时遇到一些问题。 我想做的是: 1)读入ascii文件。 其形式如下(可以有 N 个元素):

Object1 value1

Object2 value2

Object3 value3

Object4 value4

...

2) 从 ascii 文件内容生成 Web 表单。 ascii 文件中的每一行都应代表一个表单复选框选项。

...

3) 在特定 URL 处显示表单。

4) 在“提交”时,调用 cgi 脚本来处理表单。

我的问题出在步骤 2 中。我可以轻松生成具有固定值的静态表单并将其保存为标准 html 表单,但我需要一些可以在 ascii 文件中读取并在访问该 url 时动态生成 html 文件的内容。

关于最简单的方法是什么有什么建议吗?

I am having some trouble generating a web form using fields specified in an ascii file.
What I want to do is the following:
1) Read in the ascii file.
This is of the form (can have N elements):

Object1 value1

Object2 value2

Object3 value3

Object4 value4

...

2) Generate a web form from the ascii file contents. Each line in the ascii file should represent a form checkbox option.

...

3) Display the form at a specific url.

4) On "submit", call a cgi script to process the form.

My problem is in step 2. I can easily generate a static form with fixed values and save it as a standard html form but I need something that will read in the ascii file and generate the html file on the fly when visiting that url.

Any advice on what the easiest way of doing this is?

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

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

发布评论

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

评论(2

旧人哭 2024-09-07 08:33:44

在 Python 中设置 HTTP 服务的最简单方法:获取 CherryPy ( http://www.cherrypy.org

)例如,此程序:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

http://127.0.0.1/8080/ 上设置 Web 服务器并打印“你好世界!”当打开时。只需返回您的自定义 HTML 就可以了:)

或者,如果您想在特定服务器上托管表单,您可以使用 python ftplib 模块将其放在那里。

Simplest way to set up HTTP service in Python: get CherryPy ( http://www.cherrypy.org )

For example, this program:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

Sets up a web server on http://127.0.0.1/8080/ and prints "Hello World!" when opened. Just return your custom HTML instead and you're ready :)

Alternatively, if you want to host the form at a specific server, you can use the python ftplib module to put it there.

相对绾红妆 2024-09-07 08:33:44

鉴于您将步骤 2(模板处理)作为您的主要关注点,您应该研究 python 的模板库之一。好的(我现在记得的)是 Mako、Genshi、Jinja 和 Cheetah。 Python 的网站有完整的列表:http://wiki.python.org/moin/Templated

我个人认为 Mako 很容易使用,但其他的可能更适合您的特定问题。

您可能还想查看一个 Web 应用程序框架,该框架将具有模板以及第三步和第四步中描述的 CGI 内容。再次,查看 python 的网站以获取详细信息和选项。

Given that you cite step 2 (template processing) as your primary concern, you should look into one of python's templating libraries. The good ones (that I can remember right now) are Mako, Genshi, Jinja and Cheetah. Python's website has a whole list of them: http://wiki.python.org/moin/Templating

I personally find Mako easy to use, but others might be better suited to your particular problem.

You might also want to look at a web application framework, which will have templating plus the cgi stuff described in your 3rd and 4th steps. Again, check out python's website for details and options.

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