将输入从html传递到python并返回 [英] Passing input from html to python and back
问题描述
我需要制作一个网页进行作业,不必将其上传到网络,我只是使用本地.html文件. 我做了一些阅读,并想出了以下html和python:
I need to make a webpage for an assignment, it doesn't have to be uploaded to the web, I am just using a local .html file. I did some reading up and came up with the following html and python:
<!DOCTYPE html>
<html>
<head>
<title>
CV - Rogier
</title>
</head
<body>
<h3>
Study
</h3>
<p>
At my study we learn Python.<br>
This is a sall example:<br>
<form action="/cgi-bin/cvpython.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>
</p>
</body>
</html>
Python:
import cgi
import cgitb #found this but isn't used?
form = cgi.FieldStorage()
first_name = form.getvalue('first_name').capitalize()
last_name = form.getvalue('last_name').capitalize()
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>Your name is {}. {} {}</h2>".format(last_name, first_name, last_name))
print ("</body>")
print ("</html>")
但是,这只是将打印内容作为文本提供,而不是带有我想要的1行的适当html文件.
However this just gives the prints as text and not as a proper html file with the 1 line that I want.
推荐答案
您是否正在运行像apache setup这样的Web服务器?
如果您不这样做,我不确定这是否行得通,所以您可能想看看 Mamp 要使其执行您的python脚本,您还需要编辑httpd.conf
文件
Do you have a web server like apache setup this is running on?
If you don't I'm not sure this will work so you may want to have a look at Mamp To allow it to execute your python script you will also need to edit the httpd.conf
file
发件人:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
收件人:
<Directory "/var/www/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .py
Order allow,vdeny
Allow from all
</Directory>
或者
如果您只是想在不设置服务器的情况下制作实际的HTML文件,这是一种非常基本但粗略的方法,那就是将所有内容简单地写入您创建的HTML文件中,例如:
If you simply want to make an actual HTML file without setting up a server a very basic but crude way of doing this would be to simply write everything to a HTML file you create like:
fo.write("Content-type:text/html\r\n\r\n")
fo.write("<html>")
fo.write("<head>")
fo.write("<title>Hello - Second CGI Program</title>")
fo.write("</head>")
fo.write("<body>")
fo.write("<h2>Your name is {}. {} {}</h2>".format("last_name", "first_name", "last_name"))
fo.write("</body>")
fo.write("</html>")
fo.close()
这将在与python项目相同的目录中创建一个名为yourfile.html
的HTML文档.
Which will create a HTML document called yourfile.html
in the same directory as your python project.
我不建议这样做,但是我意识到,由于这是一项任务,因此您可能无法选择使用库.如果您愿意的话,一种更优雅的方法是使用 yattag 之类的东西,这将使其更易于维护. .
I don't recommend doing this, but I realise since it's an assignment you may not have the choice to use libraries. In case you, are a more elegant way would be to use something like yattag which will make it much more maintainable.
要从他们的网站复制Hello World示例.
To copy the Hello World example from their website.
from yattag import Doc
doc, tag, text = Doc().tagtext()
with tag('h1'):
text('Hello world!')
print(doc.getvalue())
更新:
如果您没有本地Web服务器设置,另一种替代方法是使用 Flask 作为您的网络服务器. 您需要像这样构建项目:
Another alternative if you don't have a local web server setup is to use Flask as your web server. You'll need to structure your project like:
/yourapp
basic_example.py
/static/
/test.css
/templates/
/test.html
Python:
__author__ = 'kai'
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('test.html')
@app.route('/hello', methods=['POST'])
def hello():
first_name = request.form['first_name']
last_name = request.form['last_name']
return 'Hello %s %s have fun learning python <br/> <a href="/">Back Home</a>' % (first_name, last_name)
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 3000)
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="static/test.css">
<title>
CV - Rogier
</title>
</head>
<body>
<h3>
Study
</h3>
<p>
At my study we learn Python.<br>
This is a sall example:<br>
<form action="/hello" method="post">
First Name: <input type="text" name="first_name"> <br />
Last Name: <input type="text" name="last_name" />
<input type="submit" name= "form" value="Submit" />
</form>
</p>
</body>
</html>
CSS(如果要样式化表单吗?)
CSS (If you want to style your form?)
p {
font-family: verdana;
font-size: 20px;
}
h2 {
color: navy;
margin-left: 20px;
text-align: center;
}
根据您的问题此处制作了一个基本示例 希望这可以帮助您走上正确的轨道,祝您好运.
Made a basic example based on your question here Hopefully this helps get you on the right track, good luck.
这篇关于将输入从html传递到python并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!