HTML打印是错误的

时间:2016-09-10 00:22:17

标签: python class variables webapp2

所以我一直在看这个超过一个小时,我无法弄清楚到底发生了什么。

脚本只打印">"

假设打印完整的HTML,然后在提交表单后打印" print_after"

import webapp2

class MainHandler(webapp2.RequestHandler):
def get(self):
    p = Page()

    if self.request.GET:
        name = self.request.GET['name']
        age = self.request.GET['age']
        time = self.request.GET['time']
        model = self.request.GET['model']
        radio = self.request.GET['trade']
        self.response.write(p.print_after(name, age, time, model, radio))
        print name + age + time + model + radio
    else:
        self.response.write(p.print_one)

class Page(object):
def __init__(self):
    self.page_body = '''
    <!DOCTYPE HTML>
        <html>
        <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <title>Audi Test Drive Request</title>
        </head>
        <body>

<img src="assets/custom/images/logo.png" title="logo" alt="" width="200px" height="150px"/>

<h3>It's awesome that you want to test-drive one of our vehicles</h3>

<form method="GET" action="">

<label>Name</label>
<br>
<input type="text" name="name" required>
<br>
<label>Age</label>
<br>
<input type="text" name="age" required>
<br>
<label>Time</label>
<br>
<select name="time" required>
    <option value="12:00 PM">12:00 PM</option>
    <option value="12:30 PM">12:30 PM</option>
    <option value="1:00 PM">1:00 PM</option>
</select>
<br>
<label>Model</label>
<br>
<select name="model" required>
    <option value="2008 Audi A4">2008 Audi A4</option>
    <option value="2008 Audi S4">2008 Audi S4</option>
    <option value="2008 Audi RS4">2008 Audi RS4</option>
</select>
<br>
<label>Are you trading in a vehicle?</label>
<br>
<input type="radio" name="trade" value="yes" required>Yes<br>
<input type="radio" name="trade" value="no" required>No<br>
<br>
<input type="submit" value="Request Test Drive">

        </form>
</body>
</html>
'''
    self.page_after = '''
    <!DOCTYPE HTML>
        <html>
        <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <title>Audi Test Drive Request</title>
        </head>
        <body>

<img src="assets/custom/images/logo.png" title="logo" alt="" width="200px" height="150px"/>

<h3>It's awesome that you want to test-drive one of our vehicles</h3>
</body
</html>
'''

def print_one(self):
    page_content = self.page_body
    page_content = page_content.format(**locals())
    return page_content

def print_after(self, name, age, time, model, radio):
    after_page_content = self.page_after
    after_page_content = after_page_content.format(**locals())
    return after_page_content

app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)

1 个答案:

答案 0 :(得分:0)

我测试了你的代码并重新安排了一下。我使用http post提交表单,然后打印表单变量。

import webapp2

class HelloWebapp2(webapp2.RequestHandler):
    def get(self):
        self.response.write('''<!DOCTYPE HTML>
        <html>
        <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="css/main.css">
        <title>Audi Test Drive Request</title>
        </head>
        <body>

<img src="assets/custom/images/logo.png" title="logo" alt="" width="200px" height="150px"/>

<h3>It's awesome that you want to test-drive one of our vehicles</h3>

<form method="POST" action="">

<label>Name</label>
<br>
<input type="text" name="name" required>
<br>
<label>Age</label>
<br>
<input type="text" name="age" required>
<br>
<label>Time</label>
<br>
<select name="time" required>
    <option value="12:00 PM">12:00 PM</option>
    <option value="12:30 PM">12:30 PM</option>
    <option value="1:00 PM">1:00 PM</option>
</select>
<br>
<label>Model</label>
<br>
<select name="model" required>
    <option value="2008 Audi A4">2008 Audi A4</option>
    <option value="2008 Audi S4">2008 Audi S4</option>
    <option value="2008 Audi RS4">2008 Audi RS4</option>
</select>
<br>
<label>Are you trading in a vehicle?</label>
<br>
<input type="radio" name="trade" value="yes" required>Yes<br>
<input type="radio" name="trade" value="no" required>No<br>
<br>
<input type="submit" value="Request Test Drive">

        </form>
</body>
</html>
''')
    def post(self): 
        if self.request.POST:
            name = self.request.POST['name']
            age = self.request.POST['age']
            time = self.request.POST['time']
            model = self.request.POST['model']
            radio = self.request.POST['trade']
            self.response.write(name +" " + age +" " + time +" " + model +" " + radio)

app = webapp2.WSGIApplication([
    ('/', HelloWebapp2),
], debug=True)

def main():
    from paste import httpserver
    httpserver.serve(app, host='127.0.0.1', port='8080')

if __name__ == '__main__':
    main()

上面的代码在8080端口启动了一个本地网络服务器。它可能不会完全符合您的要求,但存在很多。您也可以在appengine中运行它。