Python - AttributeError:' Page'对象没有属性' print_out'

时间:2015-12-05 19:30:44

标签: python-2.7

我正在学习大学课程的python,这是我以前从未使用过的。我在启动此代码时遇到了问题。我能得到一些帮助吗?

    import webapp2


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


class Page(object):
    def __init__(self):
        self.css = "css/style.css"
        self.title = "Simple Form"
        self.head = """
           <!DOCTYPE HTML>
         <html>
         <head>
         <title>{self.title}</title>
         <link href="{self.css}" rel="stylesheet" type="text/css"/>
         </head>
         """
        self.body = """
         <body>
         <div id="form">
         <form method="GET">
         <h1>Contact Us</h1>
         <input id="name" name="name" type="text" size="80" value="Name"/>
         <br/>
         <input id="phone" name="phone" type="text" size="80" value="Phone Number"></input>
         <br/>
         <input id="email" name="email" type="text" size="80" value="E-Mail"></input>
         <br/>
         <label for="contact">Contact Time:</label>
         <input type="radio" name="contact" id="contact" value="Morning">Morning</input>
         <input type="radio" name="contact" id="contact" value="Mid-Day">Mid-Day</input>
         <input type="radio" name="contact" id="contact" value="Afternoon">Afternoon</input>
         <br/>
         <label for="type">Prefered Contact Method:</label>
         <select name="type">
         <option value=""></option>
         <option value="Phone">Phone</option>
         <option value="email">E-Mail</option>
         <option value="txtmsg">Text Message</option>
         </select>
         <br/>
         <label for="gender">Gender:</label>
         <input type="checkbox" name="gender" value="Male">Male</input>
         <input type="checkbox" name="gender" value="Female">Female</input>
         <input type="checkbox" name="gender" value="Neutral">Neutral</input>
         <br/>
         <label for="message">Message:</label>
         <br/>
         <textarea></textarea>
         <br/>
         <input type="submit" value="Submit"/>
         </form>
         </div>
         </body>
         """
        self.close = """
         </html>
         """

        def print_out(self):
            all = self.head + self.body + self.close
            all = all.format(**locals())
            return all


app = webapp2.WSGIApplication({
    ('/', MainHandler)
}, debug=True)

如果重要的话,我正在使用Chrome。我尝试在其他浏览器中启动。我读了它给我的错误,这就是它:

文件&#34; /Users/judia_krakowski/Desktop/Python/simple-form/main.py" ;,第23行,获取     self.response.write(p.print_out()) AttributeError:&#39; Page&#39;对象没有属性&#39; print_out&#39;

1 个答案:

答案 0 :(得分:0)

我能在代码中找到的唯一错误是构造函数和print_out函数的缩进。您的print_out函数似乎在构造函数中声明。我认为当你在与构造函数相同的级别缩进它时它会起作用。

class Page(object):
    def __init__(self):
        pass
    def print_out(self):
        pass

而不是:

class Page(object):
    def __init__(self):
        #Some code
    def print_out(self):
         pass

`