将HTML文件加载和打印到Python CGI中

时间:2017-01-12 15:37:50

标签: python html cgi

我跟着this guide,但这不起作用。我有一个带有表单的HTML页面,该页面向该站点发送名称和号码。

这是Python代码:

#!/usr/bin/env python
import cgi, cgitb 
import os
import loadXML
import display
from xml.etree import ElementTree
form = cgi.FieldStorage() # instantiate only once!
#import file
file_name = 'testrecords.xml' 
full_name = os.path.abspath(os.path.join('xml', file_name))
# get variables from html 

htmlName = 'lewis' #form.getfirst('name', 'empty')
htmlNumber = 1 #form.getvalue('searchcrit')


#htmlName = cgi.escape(htmlName)

# initialize variables
search = ['blank','name','activity','year','title']
searchnumber = ['1','2','3','4']

searchXML = loadXML.searchingall(full_name) #finds all xml entries 
(name, activity, record, num, year) =  display.display(searchXML,htmlName,int(htmlNumber),search) 
#all vars coming out of the line above are arrays
x = 0
addedArrays = ["None"]
while name[x] != "Empty":
    addedArrays[x] = (str(name[x])+ "," + str(activity[x]) +","+ str(record[x]) +","+ str(num[x])+","+ str(year[x]))
    x = x + 1
    addedArrays.append(x)



with open('/home/ubuntu/workspace/test.html') as f: 
    htmlFile=(f.read())
print("""\
Content-Type: text/html\n 
"""+ htmlFile % addedArrays)

#print("""\
#Content-Type: text/html\n
#<html>
#<body>
 #<p class="page-description">The returned items were "%s"</p>
#</body>
#</html>
#""" % addedArrays)

这是HTML代码:

<html>

<body>

    <p class="page-description">The returned items were "%s"</p>
</body>

</html>

这是HTML文件:

<!DOCTYPE html>
<html>

<head>
    <!--under the black menu line in large text saying CLHS Records-->
    <title>CLHS Records</title>
    <link rel="stylesheet" type="text/css" href="Stylesheet.css">
    <!--This connects your stylesheet.css to html-->
</head>

<body>
    <!--this is the start of your body of the page-->
    <div class="nav">
        <!--This is what keeps seachbar in line with your names-->
        <div class="nav_wrapper">
            <!--This is what holds the search bar in place-->
            <form method="get" action="/cgi-bin/pyHandler.py" />
            <form class="form-wrapper">
                <input type="text" name="name" placeholder="Enter Name">
                <input type="radio" name="searchcrit" value="1">Name
                <input type="radio" name="searchcrit" value="3">Year
                <input type="radio" name="searchcrit" value="2">Activity
                <input type="radio" name="searchcrit" value="4">Records
                <input type="submit" value="Submit" a href="https://schoolrecordproject-jasonchristenson.c9users.io/html/Results.html" />
            </form>
        </div>
        <img class="lionimage" width="12%" src="http://clearlakeschools.org/wp-content/uploads/2016/12/Charging-Lion.png" />
        <!--this is the lion image above the title clhs records-->
        <h1 class="page-title">CLHS Records</h1>
        <p class="page-description"> On this webpage you will be able to view record holders over the various years at Clear Lake High school. You will be able to search the file by name,year,activity, and record. </p>
        <!--discription in middle of page describing-->
        <div class="navigation">
            <nav>
                <ul>
                    <li><a href="#"><b> Clear Lake Athletics </b></a></li>
                    <li><a href="http://clearlakeschools.org/about/"><b> About Clear Lake District </b></a></li>
                </ul>
                <div class="contact-btn">
                    <a href="mailto:">Contact</a>
                </div>
            </nav>
        </div>
</body>

</html>

我们只有一个基本的XML文件,它引用了所有python文件,如显示文件。

基本上我的问题在于Python文件,如果我取消注释最后8行,一切都很好,但我需要这个导入另一个HTML文件来做最后八行做的事情,但是我们需要它能够从外部更新html文件并更改格式等。

如果我运行最后八行,它运行正常,但如果我运行它:

with open('/home/ubuntu/workspace/test.html') as f: 
htmlFile=(f.read())
print("""\
Content-Type: text/html\n 
"""+ htmlFile % addedArrays)

然后它不起作用。

我需要做些什么才能让它发挥作用?谢谢!

0 个答案:

没有答案