如果调查已完成或用户已通过此调查,那么我想关闭用户对调查页面的访问。我该怎么办?
我的模型。py
class Survey(models.Model):
title = models.CharField(max_length=1024)
start_date = models.DateTimeField(null=True, blank=True)
end_date = models.DateTimeField(null=True, blank=True)
description = models.CharField(max_length=1024, null=False, blank=True)
class Ballot(models.Model):
ip = models.GenericIPAddressField(default='127.0.0.1')
survey = models.ForeignKey('Survey', models.CASCADE, null=True)
我的views.py
from django.utils.timezone import now
class SurveyView(View):
def get(self, request, id):
survey = get_object_or_404(Survey, id=id)
if survey.end_date.date() < now().date():
return redirect('index')
respond = Ballot.objects.filter(survey_id=id)
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ipc = x_forwarded_for.split(',')[-1].strip()
else:
ipc = request.META.get('REMOTE_ADDR')
for ip in respond:
if ip == ipc:
return redirect('index')
else:
return render(request, 'polls/survey.html', {'survey': Survey.objects.get(id=id)})
在我的代码中,我得到错误:none类型对象没有属性日期; 而且,如果我删除了日期条件,那么ip条件什么也没做。