我试图用python和cherrypy创建一种web服务器。在
我希望将htmls放入单独的文件中,并将它们嵌入到python脚本中。我以前的密码是。在
@cherrypy.expose
def welcome(self, loginAttempt = None):
""" Prompt the user with a login form. The form will be submitted to /signin
as a POST request, with arguments "username", "password" and "signin"
Dispaly a login error above the form if there has been one attempted login already.
"""
#Debugging Process Check
print "welcome method called with loggedIn = %s" % (loginAttempt)
if loginAttempt == '1':
""" If the user has attempted to login once, return the original login page
with a error message"""
page = get_file("loginPageE.html")
return page
else:
page = """
<form action='/signin' method='post'>
Username: <input type='text' name='username' /> <br />
Password: <input type='password' name='password' />
<input type='submit' name='signin' value='Sign In'/>
</form>
"""
return page
在哪里登录名.html是
^{pr2}$但是我一直收到一条错误消息
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py", line 606, in respond
cherrypy.response.body = self.handler()
File "/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py", line 25, in __call__
return self.callable(*self.args, **self.kwargs)
File "proj1base.py", line 74, in welcome
page = get_file("loginPageE.html")
NameError: global name 'get_file' is not defined
我想知道是否有人能帮忙?在
提前谢谢
get_file不是标准Python函数之一,因此它必须是您以前拥有的自定义函数。您可以创建一个简单的函数来读取文件并将其内容作为字符串返回,如下所示:
您可以在http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files上阅读Python文件管理
好吧,从错误中,显然python不知道
get_file()
函数是什么。您确定在调用welcome()
函数内的这个函数的时候,get_file()
已经被定义了吗?在但是,请考虑使用适当的模板引擎。Jinja2真的很好,它允许你在模板中使用条件句等,这是你在某个时候肯定想要的。除此之外,如果你要求的话,它还可以为你做一些很好的事情,比如变量自动转义。在
相关问题 更多 >
编程相关推荐