2016-11-09 93 views
0

我正在製作一個基本的網頁,它有一個註冊頁面和一個登錄頁面。我正在使用谷歌應用程序引擎,Python和HTML。這是我的用戶功能:如何使從數據庫檢索用戶的登錄功能?

class User(db.Model): 
    name = db.StringProperty(required=True) 
    pw_hash = db.StringProperty(required=True) 
    email = db.StringProperty() 

    @classmethod 
    def by_id(cls, uid): 
     return cls.get_by_id(uid, parent=users_key()) 

    @classmethod 
    def by_name(cls, name): 
     u = cls.all().filter('name=', name).get() 
     return u 

    @classmethod 
    def register(cls, name, pw, email=None): 
     pw_hash = make_pw_hash(name, pw) 
     return cls(parent=users_key(), 
        name=name, 
        pw_hash=pw_hash, 
        email=email) 

    @classmethod 
    def login(cls, name, pw): 
     u = cls.by_name(name) 
     if u and valid_pw(name, pw, u.pw_hash): 
      return u 

當我註冊新用戶時,我把它們放在數據庫中。它們被存儲在數據庫中:

class Register(Signup): 
    def done(self): 
     #make sure the user doesn't already exist 
     u = User.by_name(self.username) 
     if u: 
      msg = 'That user already exists.' 
      self.render('signup-form.html', error_username = msg) 
     else: 
      u = User.register(self.username, self.password, self.email) 
      u.put() 

      self.login(u) 
      self.redirect('/blog') 

但是,當我用我的登錄功能,我無法從數據庫中檢索用戶使用的登錄功能。它說每次「登錄無效」:

class Login(BlogHandler): 
    def get(self): 
     self.render('login-form.html') 

    def post(self): 
     username = self.request.get('username') 
     password = self.request.get('password') 

     u = User.login(username, password) 
     if u: 
      self.login() 
      self.redirect('/blog') 
     else: 
      msg = 'Invalid login' 
      self.render('login-form.html', error = msg) 

當我註冊時,它應該認識到用戶已經在數據庫中。不過,我每次都可以使用相同的憑據進行註冊。這是我的註冊處理程序:

class Signup(BlogHandler): 
    def get(self): 
     self.render("signup-form.html") 

    def post(self): 
     have_error = False 
     self.username = self.request.get('username') 
     self.password = self.request.get('password') 
     self.verify = self.request.get('verify') 
     self.email = self.request.get('email') 

     params = dict(username = self.username, 
         email = self.email) 

     if not valid_username(self.username): 
      params['error_username'] = "That's not a valid username." 
      have_error = True 

     if not valid_password(self.password): 
      params['error_password'] = "That wasn't a valid password." 
      have_error = True 
     elif self.password != self.verify: 
      params['error_verify'] = "Your passwords didn't match." 
      have_error = True 

     if not valid_email(self.email): 
      params['error_email'] = "That's not a valid email." 
      have_error = True 

     if have_error: 
      self.render('signup-form.html', **params) 
     else: 
      self.done() 

    def done(self, *a, **kw): 
     raise NotImplementedError 

這裏是bloghandler功能,是所有其他功能的底線:

class BlogHandler(webapp2.RequestHandler): 
    def write(self, *a, **kw): 
     self.response.out.write(*a, **kw) 

    def render_str(self, template, **params): 
     params['user'] = self.user 
     return render_str(template, **params) 

    def render(self, template, **kw): 
     self.write(self.render_str(template, **kw)) 

    def set_secure_cookie(self, name, val): 
     cookie_val = make_secure_val(val) 
     self.response.headers.add_header(
      'Set-Cookie', 
      '%s=%s; Path=/' % (name, cookie_val)) 

    def read_secure_cookie(self, name): 
     cookie_val = self.request.cookies.get(name) 
     return cookie_val and check_secure_val(cookie_val) 

    def login(self, user): 
     self.set_secure_cookie('user_id', str(user.key().id())) 

    def logout(self): 
     self.response.headers.add_header('Set-Cookie', 'user_id=; Path=/') 

    def initialize(self, *a, **kw): 
     webapp2.RequestHandler.initialize(self, *a, **kw) 
     uid = self.read_secure_cookie('user_id') 
     self.user = uid and User.by_id(int(uid)) 

這裏是鏈接到GitHub的:

https://github.com/keldavis/registration

以下是網站的鏈接:

registr ation-148718.appspot.com

如何讓我的註冊頁面識別已經創建的用戶?我該如何讓我的登錄頁面從數據庫中檢索我的用戶並執行有效的登錄?

回答

0

你的線

u = cls.all().filter('name=', name).get()

要麼是

u = cls.all().filter('name =', name).get()

u = cls.all().filter('name', name).get()

per Google docs(正OTE缺少的空格字符/所述=字符是可選的):

property_operator:字符串由屬性名和一個可選的比較 運算符(=,=,<,< =,>,> = ,IN),用空格分隔:例如, 'age>'。如果僅指定屬性名稱而沒有比較 運算符,則默認情況下,篩選器會比較相等性(=)。

(也注意到,你應該使用的google.appengine.ext.ndb代替google.appengine.ext.db,真的不應該執行在實際應用認證/授權/散列/等你自己)

相關問題