简体   繁体   English

如何在烧瓶中编写python脚本

[英]How to write python script in flask

I have problem about my python script with HTML. 我对使用HTML的python脚本有疑问。

I'm newbie. 我是新手。

My python script looks as follow: 我的python脚本如下所示:

#!/usr/bin/python3.4

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<head>")
print("<title>Hello - Second CGI Program</title>")
print("</head>")
print("<body>")
print("<h2>Hello %s %s</h2>" % (first_name, last_name))
print("</body>")
print("</html>")

and my HTML file: 和我的HTML文件:

<form action="hello_get.py" method="get">
First Name: <input type="text" name="first_name">  <br />

Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>

I run HTML and write to input first name and last name, but my script doesn't start. 我运行HTML并输入要输入的名字和姓氏,但是我的脚本没有启动。 It's just show the windows If I want to open script, or save it. 它只是显示窗口,如果我想打开脚本或保存它。

EDIT: 编辑:

My HTML: 我的HTML:

<div class="container">
    <div class="row">
        <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
            <p>Want to get in touch with me? Fill out the form below to send me a message and I will try to get back to you within 24 hours!</p>
            <!-- Contact Form - Enter your email address on line 19 of the mail/contact_me.php file to make this form work. -->
            <!-- WARNING: Some web hosts do not allow emails to be sent through forms to common mail hosts like Gmail or Yahoo. It's recommended that you use a private domain email address! -->
            <!-- NOTE: To use the contact form, your site must be on a live web host with PHP! The form will not work locally! -->

            <form action="/app/get.cgi" method="get">
                <div class="row control-group">
                    <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Name</label>
                            <input type="text" name="name" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="row control-group">
                    <div class="form-group col-xs-12 floating-label-form-group controls">
                        <label>Email Address</label>
                        <input type="email" name="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="row control-group">
                    <div class="form-group col-xs-12 floating-label-form-group controls">
                        <label>Message</label>
                        <textarea rows="5" name="message" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <br>
                <div id="success"></div>
                <div class="row">
                    <div class="form-group col-xs-12">
                        <button type="submit" class="btn btn-default">Odoslať</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

(my python script) get.cgi: (我的python脚本)get.cgi:

#!/usr/bin/python3.4

# Import modules for CGI handling
import cgi, cgitb
file = open('file.txt', mode='w')
form = cgi.FieldStorage()

name = form.getvalue('name')
email = form.getvalue('email')
message = form.getvalue('message')

file.write(name) 
file.write(email)
file.write(message)

file.close()

and my app.py which is main flask module 和我的app.py这是主烧瓶模块

from flask import Flask, render_template, request

app = Flask(__name__)
app.secret_key = 'mypa$$w0rd'

@app.route('/')
@app.route('/index.html')
def index():
    return render_template('index.html')

@app.route('/about.html')
def about():
    return render_template('about.html')


@app.route('/contact.html')
def contact():
    return render_template('contact.html')


@app.route('/post.html')
def sample_post():
    return render_template('post.html')

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

I'm running it as localhost. 我将其作为localhost运行。 Output when i click on SEND(in my html it equals ODOSLAT in my language): Not Found 当我单击SEND时输出(在我的html中等于我的语言中的ODOSLAT):找不到

The requested URL was not found on the server. 在服务器上找不到请求的URL。 If you entered the URL manually please check your spelling and try again. 如果您手动输入网址,请检查拼写,然后重试。

And text field in browser search now looks like this: 现在,浏览器搜索中的文本字段如下所示:

http://127.0.0.1:5000/app/get.cgi?name=asdf&email=julo.marko%40gmail.com&message=asfasfasf http://127.0.0.1:5000/app/get.cgi?name=asdf&email=julo.marko%40gmail.com&message=asfasfasf

If you have a Flask web application, you don't need any .cgi scripts. 如果您拥有Flask Web应用程序,则不需要任何.cgi脚本。 Just point your form to the appropriate route in flask. 只需将表格指向烧瓶中的适当路线即可。 With the url in your html: 在html中添加网址:

@app.route('/api/get.cgi')
def api_get():
    first_name = request.args.get('first_name')
    last_name = request.args.get('last_name')
    return """<html><body><h2>Hello %s %s</h2></body></html>""" % (first_name, last_name)

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

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