2013-04-01 83 views
0

我的代碼是這樣的:谷歌應用程序引擎 - 在代碼中的錯誤

import webapp2 
import re 
form = """<html> 
<form method=post> 
<input name="username"><div id=div>%s</div><br> 
<input name="password"><div id=div>%s</div><br> 
<input name="verify"><div id=div>%s</div><br> 
<input name="email"><div id=div>%s</div> 
<input type="submit"> 
</form> 
</html>""" 
class MainPage(webapp2.RequestHandler): 
    def get(self): 
     self.response.headers['Content-Type'] = 'text/html' 
     self.response.write(form % ('','','','')) 

    def reply(self,user,password,verify,email): 
     if not user and password and verify and email: 
     self.redirect('/broski') 
     else: 
     self.response.write(form % (user,password,verify,email)) 

    def post(self): 
     self.usererror = '' 
     if not re.compile(r"^[a-zA-Z0-9_-]{3,20}$").match(self.request.get('username')): 
     self.usererror = "That's not a valid username." 
     self.passworderror = '' 
     if not re.compile(r"^.{3,20}$").match(self.request.get('password')): 
     self.passworderror = "That wasn't a valid password." 
     self.verifyerror = '' 
     if self.request.get('verify') != self.request.get('password') and self.request.get('password') != '': 
     self.verifyerror = "Your passwords didn't match." 
     self.emailerror = '' 
     if not re.compile(r"^[\S][email protected][\S]+\.[\S]+$").match(self.request.get('email')): 
     self.emailerror = "That's not a valid email." 
     self.reply(usererror,passworderror,verifyerror,emailerror) 

class ThankYo(webapp2.RequestHandler): 
    def get(self): 
     self.response.write('Welcome, ' + self.request.get('username')+'!') 

app = webapp2.WSGIApplication([('/', MainPage),('/broski', ThankYo)], 
           debug=True) 

這簡直是我第一次寫GAE代碼(和IM新的Python ..)大於10層的線條和着,當然, 他打破。 我發現了一些錯誤,但絕對幫助,因爲該網站仍然無法加載。 關於代碼的幫助非常感謝。 更新:我更新了它多一點(ThankYo的改變繼承)以及現在的形式加載成功,只要我ofcourse崩潰後的數據...

回答

1
class ThankYo(Mainpage): 

應該

class ThankYo(MainPage): 

(注意首都P)。


我發現這個通過觀察留在終端中的錯誤消息:

% dev_appser File "/home/unutbu/pysrc/project/main.py", line 38, in <module> 
... 
    class ThankYo(Mainpage): 
NameError: name 'Mainpage' is not defined 

另外改變

self.reply(usererror,passworderror,verifyerror,emailerror) 

self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror) 

我再次發現這個通過查看終端錯誤消息:

File "/home/unutbu/pysrc/project/main.py", line 39, in post 
    self.reply(usererror, passworderror, verifyerror, emailerror) 
NameError: global name 'usererror' is not defined 

如何看到錯誤消息(從終端):

有不止一種方法啓動GAE服務器。我這樣做的方式是打開一個終端並運行

dev_appserver.py /path/to/project/ 

終端然後發出有用的錯誤消息。


最後一個問題有點複雜。我們正在發送錯誤信息到reply方法:

self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror) 

但在reply方法定義我們稱之爲userpasswordverifyemail。這會導致一些認知失調。讓我們重命名這些usererrorpassworderrorverifyerroremailerror

def reply(self, usererror, passworderror, verifyerror, emailerror): 

現在檢查沒有錯誤使用方法:

if not any(usererror and passworderror and verifyerror and emailerror): 

但我們要重定向到的頁面需要用戶名! 如果我們只是重定向到/broksi

 self.redirect('/broski') 

那麼ThankYo處理程序不知道用戶名是什麼。 self.request.get('username')會空白(返回一個空字符串)。因此,我們需要沿着用戶名傳遞給broski

import urllib 

self.redirect('/broski?' + urllib.urlencode(
      {'username' : self.username})) 

因此,代碼最終看起來像這樣:

import urllib 
import webapp2 
import re 
form = """<html> 
<form method=post> 
<input name="username"><div id=div>%s</div><br> 
<input name="password"><div id=div>%s</div><br> 
<input name="verify"><div id=div>%s</div><br> 
<input name="email"><div id=div>%s</div> 
<input type="submit"> 
</form> 
</html>""" 


class MainPage(webapp2.RequestHandler): 
    def get(self): 
     self.response.headers['Content-Type'] = 'text/html' 
     self.response.write(form % ('', '', '', '')) 

    def reply(self, usererror, passworderror, verifyerror, emailerror): 
     if not any(usererror and passworderror and verifyerror and emailerror): 
      self.redirect('/broski?' + urllib.urlencode(
       {'username' : self.username})) 
     else: 
      self.response.write(form % (user, password, verify, email)) 

    def post(self): 
     self.username = self.request.get('username') 
     self.password = self.request.get('password') 
     self.verify = self.request.get('verify') 
     self.email = self.request.get('email') 
     self.usererror = '' 
     if not re.compile(r"^[a-zA-Z0-9_-]{3,20}$").match(self.username): 
      self.usererror = "That's not a valid username." 
     self.passworderror = '' 
     if not re.compile(r"^.{3,20}$").match(self.password): 
      self.passworderror = "That wasn't a valid password." 
     self.verifyerror = '' 
     if self.verify != self.password and self.password != '': 
      self.verifyerror = "Your passwords didn't match." 
     self.emailerror = '' 
     if not re.compile(r"^[\S][email protected][\S]+\.[\S]+$").match(self.email): 
      self.emailerror = "That's not a valid email." 
     self.reply(self.usererror, self.passworderror, self.verifyerror, self.emailerror) 


class ThankYo(MainPage): 
    def get(self): 
     self.response.write('Welcome, ' + self.request.get('username') + '!') 

app = webapp2.WSGIApplication([('/', MainPage), ('/broski', ThankYo)], 
           debug=True) 
+0

我固定的,而是有它inherit.Although一兩件事,怎麼你看到終端了嗎?這是一種愚蠢的,但我是新的 –

+0

謝謝,幾乎在那裏。現在只有當我提交有效的細節時,其他所有工作都在工作,它仍然只是重新呈現表單(我希望它會重定向到我的歡迎頁面D :)。 –

+0

更新:我想我找到了致命的錯誤,我將其設置爲=時,應當== -.-! –