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

2024-07-04 05:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我跟着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代码:

^{pr2}$

以下是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文件来完成最后8行的操作,但是我们需要它能够在外部更新HTML文件和更改格式等

如果我运行最后八行,它运行得很好,但是如果我运行这个:

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

那就没用了。在

我需要做些什么才能让它生效?谢谢!在


Tags: 文件thetextnameimportdivforminput

热门问题