3
部分的結構大致是這樣的:驗證邏輯,我編碼應用的
class PostModel(db.Model):
some_property = db.WhateverProperty()
some_other_property = db.WhateverProperty()
class PostHandler(webapp.RequestHandler):
def get(self):
#code to generate form
def post(self):
#code to validate input from form
#create entity and put() it to datastore if input passes the validation
現在,從我瞭解MVC,這個驗證邏輯應該在模型中,對不對?那麼,我應該這樣做嗎?
class PostModel(db.Model):
some_property = db.WhateverProperty()
some_other_property = db.WhateverProperty()
@staticmethod
def validation_logic(form_input):
#throw exceptions if validation fails
@staticmethod
def save_to_datastore(form_input):
#this would assume data already passed validation
#create entity and save it
class PostHandler(webapp.RequestHandler):
def get(self):
#code to generate form
def post(self):
try:
PostModel.validation_logic(form_input)
except CustomException,e:
self.redirect('/errorpage?msg='+e.msg)
PostModel.save_to_datastore(form_input)
這是不錯的MVC形式嗎?