简体   繁体   English

使用python提交简单表单

[英]Simple form submit with python

I am totall newbie to python so if the question will be totally newbie please excuse me. 我是python的新手,所以如果问题完全是新手,请原谅。 I have simple landing page index.html and have a form there: 我有一个简单的登陆页面index.html并在其中有一个表单:

<form action="getPhone.py" method = "POST">
First name:
<input type="text" name="firstname" >

Phone:
<input type="text" name="lastname" >

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

So can you provide a simple basic example of getPhone.py for me How to get data from this form ( I want to send this data via email, but as first step i just have to get it) ? 因此,您能为我提供一个简单的getPhone.py基本示例getPhone.py如何从此表单中获取数据(我想通过电子邮件发送此数据,但第一步,我只需要获取它)? UPD : I am developing on windows.I don't know about such things as will my server be able to execute cgi scripts or not. UPD :我在Windows上进行开发。我不知道服务器是否能够执行cgi脚本。

If your web-server doesn't support Python CGI scripts, you could use some micro-framework. 如果您的网络服务器不支持Python CGI脚本,则可以使用一些微框架。
For example, using Flask : 例如,使用Flask

from flask import Flask, request
app = Flask(__name__)

@app.route('/get_phone/', methods=['POST', 'GET'])
def get_phone():
    if request.method == 'POST':
        print 'First name:', request.form['firstname']
        print 'Phone:', request.form['lastname']

    return 'Take a look at your terminal!'

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

(Please note that you use a textbox named "lastname" , but you call it "Phone" . I did the same.) (请注意,您使用的文本框名为"lastname" ,但您将其称为"Phone" 。我也是如此。)

Then run it and use http://127.0.0.1:5000/get_phone/ instead of getPhone.py : 然后运行它,并使用http://127.0.0.1:5000/get_phone/代替getPhone.py

<form action="http://127.0.0.1:5000/get_phone/" method="POST">

It's a more popular way, especially if you consider growing your site. 这是一种更流行的方式,特别是如果您考虑扩大站点。 If you won't need anything else, you could go with CGI and python-supported web-server. 如果您不需要其他任何内容,则可以使用CGI和python支持的Web服务器。

Actually, it would make sense if you provide some more information on what you're trying to do. 实际上,如果您提供有关您要执行的操作的更多信息,这将很有意义。 Do you want to create your own website and have no idea on how it works? 您想创建自己的网站,对网站如何运作一无所知吗? I guess it'd be easier for you to jump right into the web-frameworks like Flask or Django . 我想您可能更容易进入FlaskDjango之类的网络框架。

If you aren't sure you'd like to progress in web-development and just want to learn Python and its builtin libs, you can set up some webserver or take a look at Python's webservers manual . 如果您不确定要在Web开发方面取得进步,只是想学习Python及其内置库,则可以设置一些Web服务器或查看Python的Web服务器手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM