时间转换和密码,手机的re模块简单校验
import re,time
def check_userinfo(request):
pwd = request.POST.get("pwd")
if "_" in pwd or re.findall("\W",pwd):
return False,"pwd must be number or alpha"
tel = request.POST.get("tel")
if len(tel) != 11:
return False,"telephone number must be 11"
try:
#手机号码简单校验
res = re.search("^13\d{9}$",tel).group()
except AttributeError:
return False,"illegal telephone"
except ValueError:
return False,"telephone must be number"
combo_pk = request.POST.get("combo_pk")
combo_obj = Combo.objects.filter(pk=combo_pk)
if not combo_obj:
return False,"combo not exists"
#前台传回的格式化的字符串时间"2019-4-20"
consum_time = request.POST.get("consum_time")
#先转换为结构化的时间struct_time(tm_year=2019, tm_mon=4, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=110, tm_isdst=-1)
struct_time = time.strptime(consum_time, '%Y-%m-%d')
#再转化为时间戳进行比对
old_time = time.mktime(struct_time)
if time.time() < time.mktime(struct_time):
return False,"time error,bigger than current time"
return True,(pwd,tel,combo_pk,consum_time)
django模块的时区设置
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = False
在设置了USE_TZ=True之后,如果设置了TIME_ZONE = 'Asia/Shanghai',尽管数据库中存储的是UTC时间,但在模板显示的时候,会转成TIME_ZONE所示的本地时间进行显示。