我想阻止高级计划在 30 天后过期的用户(尽管我可能会在几天后更改)。并将他们重定向到付款页面。如果到期日期已到,我通过检查数据库来做到这一点,这样它就可以阻止他们查看独家内容。我编写了以下代码来执行操作,但出现此错误:
ValidationError at /owo/
[u"'expire' value has an invalid date format. It must be in YYYY-MM-DD format."]
我也想知道这种方法是否是执行操作的最佳方式,或者我应该将代码移动并放在middleware.py中。
楷模
class Paid(models.Model):
user=models.ForeignKey(User)
pub_date=models.DateTimeField()
expire = models.DateField(_('expire'), default=None, blank=True, null=True, db_index=True)
active=models.BooleanField(_('active'), default=False, db_index=False)
sid=models.CharField(max_length=100,default='1303908')
mode=models.CharField(max_length=10, default='2CO')
li_0_type=models.CharField(max_length=100, default='Product')
li_0_name=models.CharField(max_length=100, default='Monthly Subscription')
li_0_price=models.CharField(max_length=100, default='500')
def __unicode__(self):
return self.user
def is_active(self):
return self.active
def is_expired(self):
if self.expire is None:
return False
else:
return self.expire < date.today()
拯救付费用户的观点:
def two_check(request):
if request.method=="POST":
form=PaidForm(request.POST)
if form.is_valid():
data=form.cleaned_data
newtwo=Paid(
user=request.user,
pub_date=datetime.datetime.now(),
expire=datetime.datetime.now()+ datetime.timedelta(days=29),
active=data['active'],
sid=data['sid'],
mode=data['mode'],
li_0_type=data['li_0_type'],
li_0_name=data['li_0_name'],
li_0_price=data['li_0_price'])
newtwo.save()
return HttpResponseRedirect('/confirm_two/')
else:
return HttpResponse('Oops error')
else:
return render_to_response('two.html'{'PaidForm':PaidForm},context_instance=RequestContext(request))
用于检查过期用户的视图
def check_use(request):
d=datetime.date.today()
if request.user.is_authenticated():
if Paid.objects.get(expire='expire') >= d.strptime("%Y-%m-%d"):
return HttpResponseRedirect('/pay_plan/')
else:
return HttpResponseRedirect('/owo/')
return render_to_response('eyowo.html',{'Paid':Paid},context_instance=RequestContext(request))