RnR*_*ger 4 html python cgi flask python-3.x
我需要为作业制作一个网页,它不必上传到网络,我只是使用本地 .html 文件。我做了一些阅读并想出了以下 html 和 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>
Run Code Online (Sandbox Code Playgroud)
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>")
Run Code Online (Sandbox Code Playgroud)
然而,这只是将打印作为文本而不是作为我想要的 1 行的正确 html 文件。
你有一个像 apache setup 这样的 web 服务器正在运行吗?如果你不这样做,我不确定这会起作用,所以你可能想看看Mamp要让它执行你的 python 脚本,你还需要编辑httpd.conf文件
从:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
Run Code Online (Sandbox Code Playgroud)
到:
<Directory "/var/www/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .py
Order allow,vdeny
Allow from all
</Directory>
Run Code Online (Sandbox Code Playgroud)
或者
如果您只是想在不设置服务器的情况下制作一个实际的 HTML 文件,那么一种非常基本但粗略的方法就是将所有内容简单地写入您创建的 HTML 文件中,如下所示:
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()
Run Code Online (Sandbox Code Playgroud)
这将yourfile.html在与您的 python 项目相同的目录中创建一个 HTML 文档。
我不建议这样做,但我意识到因为这是一项任务,您可能无法选择使用库。万一你,更优雅的方法是使用像yattag这样的东西,这将使它更易于维护。
从他们的网站复制 Hello World 示例。
from yattag import Doc
doc, tag, text = Doc().tagtext()
with tag('h1'):
text('Hello world!')
print(doc.getvalue())
Run Code Online (Sandbox Code Playgroud)
更新:
如果您没有本地 Web 服务器设置,另一种选择是使用Flask 作为您的 Web 服务器。你需要像这样构建你的项目:
/yourapp
basic_example.py
/static/
/test.css
/templates/
/test.html
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
CSS(如果您想设置表单样式?)
p {
font-family: verdana;
font-size: 20px;
}
h2 {
color: navy;
margin-left: 20px;
text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
在这里根据您的问题制作了一个基本示例 希望这有助于您走上正轨,祝您好运。
| 归档时间: |
|
| 查看次数: |
52036 次 |
| 最近记录: |