python Web应用程序在不同的时间表现不同

时间:2015-03-15 06:56:17

标签: python web

此代码运行注册页面

它会检查它是否与正则表达式匹配

此处仅检查错误案例

如果出现错误,则应显示错误,例如无效的用户名,密码无效,密码不匹配,电子邮件无效

import webapp2
import escape
import regular
form="""
<html>
<head>
<title>Sign Up</title>
<style type="text/css">
  .label {text-align: right}
  .error {color: red}
</style>

</head>
<body>
<h2>Signup</h2>
<form method="post">
  <table>
    <tr>
      <td class="label">
        Username
      </td>
      <td>
        <input type="text" name="username" value="%(username)s">
      </td>
      <td class="error">
         %(user_error)s
      </td>
    </tr>

    <tr>
      <td class="label">
        Password
      </td>
      <td>
        <input type="password" name="password" >
      </td>
      <td class="error">
        %(password_error)s
      </td>
    </tr>

    <tr>
      <td class="label">
        Verify Password
      </td>
      <td>
        <input type="password" name="verify" >
      </td>
      <td class="error">
        %(verify_error)s
      </td>
    </tr>

    <tr>
      <td class="label">
        Email (optional)
      </td>
      <td>
        <input type="text" name="email" value="%(email)s">
      </td>
      <td class="error">
        %(email_error)s

      </td>
    </tr>
  </table>

  <input type="submit">
  </form>
 </body>

 </html>

类MainPage(webapp2.RequestHandler):

   def write_form(self,username="",email="",user_error="",password_error="",verify_error="",email_error=""):
    self.response.out.write(form % {"username" : username,
                                    "email": email,
                                    "user_error": user_error,
                                    "password_error": password_error,
                                    "verify_error": verify_error,
                                    "email_error": email_error})
def get(self):
    self.write_form()
def post(self):
    username=self.request.get('username')
    password=self.request.get('password')
    verify=self.request.get('verify')
    email=self.request.get('email')

    flag=True
    if not (regular.valid_username(username)):
          user_error="invalid username"
          flag=False
    if not (regular.valid_password(password)):
          password_error="invalid password"
          flag=False
    if not (password==verify):
          verify_error="password didnt match"
          flag=False
    if email and not (regular.valid_email(email)):
          email_error="invalid email"
          flag=False
    if not flag:      
       self.response.out.write(password_error)
       self.response.out.write(user_error)
           self.write_form(username,email,user_error,password_error,verify_error,email_error)
application = webapp2.WSGIApplication([
  ('/', MainPage),
 ], debug=True)

1 个答案:

答案 0 :(得分:0)

这是因为我在调用方法之前没有初始化所有错误

write_form(...   )
do this
     user_error=""
     password_error=""
     verify_error=""
     email_error=""

你会很高兴