[해결] 나는 혼합 클래스 방법과 인스턴스 방법을하기 전에. 그래서 StringProperty에 strptime (string, format)을 적용하는 방법은 무엇입니까?
는 실제로TS = ndb.StringProperty() 또는 StringProperty ('의 TS')하지만
하지 발신하고, @classmethod 함수 내부 strptime (cls.ts 포맷)를 호출하려고 할 때 ts 안에 문자열 값.올바른 하나 @ 데코를 제거
strptime (self.ts 포맷)을 호출한다.
1 아래
원래 질문 : 토큰의 인스턴스가 생성 될 때,() init_created 문자열로 TS를 inititalize하기 위해 호출됩니다.
2 : 사용자가 토큰을 arg로 사용하여 확인 핸들러를 요청하면 토큰을 사용하여 Token 인스턴스를 검색합니다.
3 : 저는 is_valid()를 호출하기 위해 Token 인스턴스를 사용합니다. 다른 시간대 obj와 비교하기 위해 ts를 datetime obj로 다시 변환합니다 ( ).
오류 1 : 내가 설정 한 경우
합니다 (STR() 메소드)
delta = (now-datetime.strptime(str(cls.ts),'%Y-%b-%d/%H:%M:%S:%f')).total_seconds()
은 내가
ValueError: time data "StringProperty('ts')" does not match format '%Y-%b-%d/%H:%M:%S:%f'
오류 2를 얻을 : 그래서 내가 다른 방법을 시도해보십시오. 내가 설정 (STR없이())
delta = (now-datetime.strptime(cls.ts,'%Y-%b-%d/%H:%M:%S:%f')).total_seconds()
내가
TypeError: must be string, not StringProperty
을받을 것은 그래서 제 질문은 정확하게 방법을 strptime하는 stringproperty을 전달하는 방법이다.
대단히 감사합니다.
class Token(ndb.Model):
ts = ndb.StringProperty()
@classmethod
def init_created(cls):
ts = (datetime.utcnow() + timedelta(hours=8)).strftime(format='%Y-%b-%d/%H:%M:%S:%f')
return ts
@classmethod
def is_valid(cls):
now = (datetime.utcnow() + timedelta(hours=8))
delta = (now-datetime.strptime(cls.ts,'%Y-%b-%d/%H:%M:%S:%f')).total_seconds()
return (delta<expire_time)
class Verification(webapp2.RequestHandler):
def get(self , token):
token_cls = Token.query(Token.token == token).get()
if not (token_cls and token_cls.is_valid()) :
template = jinja_environment.get_template('verification_error.html')
pass
must be string, not StringProperty
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s/1.374948928179626607/main.py", line 216, in get
if not (token_cls and token_cls.is_valid()) :
File "/base/data/home/apps/s~/1.374948928179626607/main.py", line 86, in is_valid
delta = (now-datetime.strptime(cls.ts,'%Y-%b-%d/%H:%M:%S:%f')).total_seconds()
TypeError: must be string, not StringProperty
Model 인스턴스 대신 Model 클래스의 속성에 액세스하고 있습니다. 자신의 실수를 파악하기에 충분하지 않은 경우 더 많은 코드를 보여주십시오. – Greg
정보가 부족하여 죄송합니다. 더 많은 코드를 추가했습니다. 알림을 보내 주셔서 감사합니다. 먼저 Token.query로 모델 인스턴스를 얻은 다음 인스턴스 (token_cls)를 사용하여 is_valid()를 호출합니다. 맞습니까? – user3168078