Django AttributeError:'datetime.date'对象没有属性'date'

时间:2015-12-24 20:40:32

标签: django date datetime

我正在尝试制作一个我发现在线工作的日历模板标签,但是它正在使用日期。我不确定,日期时间对我来说真的很混乱。有时它有效,我不知道为什么,有时它不起作用,我不知道为什么。

我的模特:

class Event(models.Model):
    unique_id               = models.IntegerField(default=1)
    subject                 = models.CharField(max_length=140, blank=True, null=True)
    body                    = models.TextField(null=True, blank=True)
    start_date              = models.DateField(null=True, blank=True)
    start_time              = models.TimeField(null=True, blank=True)
    end_date                = models.DateField(null=True, blank=True)
    end_time                = models.TimeField(null=True, blank=True)

以及模板标记中的违规代码:

from django import template
from datetime import date, timedelta
from cal.models import Event    

def cal_month(year=date.today().year, month=date.today().month):
        event_list = Event.objects.filter(start_date__year=year, start_date__month=month)

        first_day_of_month = date(year, month, 1)
        last_day_of_month = get_last_day_of_month(year, month)
        first_day_of_calendar = first_day_of_month - timedelta(first_day_of_month.weekday()+1)
        last_day_of_calendar = last_day_of_month + timedelta(7 - last_day_of_month.weekday())

        cal_month = []
        week = []
        week_headers = []

        i = 0
        day = first_day_of_calendar
        while day <= last_day_of_calendar:
            if i < 7:
                week_headers.append(day)
            cal_day = {}
            cal_day['day'] = day
            cal_day['event'] = False
            for event in event_list:
                if day >= event.start_date.date() and day <= event.end_date.date():
                    cal_day['event'] = True
            if day.month == month:
                cal_day['in_month'] = True
            else:
                cal_day['in_month'] = False  
            week.append(cal_day)
            if day.weekday() == 5:
                cal_month.append(week)
                week = []
            i += 1
            day += timedelta(1)

        return {'calendar': cal_month, 'headers': week_headers}

    register.inclusion_tag('calendar.html')(cal_month)

就是这条线:  if day&gt; = event.start_date.date()和day&lt; = event.end_date.date(): 一直在失败。我不明白为什么。这是一个常规的DateField。我如何告诉Django将其视为约会?

以下是我尝试将行更改为以下时获得的最大递归错误:“if day&gt; = event.start_date and day&lt; = event.end_date:”

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/webapps/programgr/cal/views.py", line 37, in view_calendar
    return render(request, 'calendar.html', args)
  File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py", line 67, in render
    template_name, context, request=request, using=using)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 99, in render_to_string
    return template.render(context, request)
  File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py", line 74, in render
    return self.template.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 209, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 135, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 65, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1292, in render
    return t.render(new_context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 211, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 135, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 65, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1292, in render
    return t.render(new_context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 211, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 135, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 65, in render
    result = block.nodelist.render(context)

这只会循环到日志结尾,并显示“最大递归错误。”

0 个答案:

没有答案