日期未在Django模板中呈现

时间:2019-08-19 21:33:40

标签: python html django django-templates

我正在尝试从views.py传递日期值(从managers.py传递日期值),但是它们未在我的模板中呈现。

我通过将日期值打印到控制台并将其添加到模板中来确保日期值正确。无需任何过滤器,它可以很好地呈现,但是当我在项目早期使用的语法完全相同时(在它起作用的地方),我得到的都是空白值。

managers.py

tz = pytz.timezone('America/Chicago')

class ProfileManager(Manager):
    def index(self, request):
        profile = models.Profile.objects.get(user__pk=request.session['id']) \
            if 'id' in request.session else None

        appts = []
        next_appt = None

        if profile != None:
            try:
                next_appt = Appointment.objects.get(
                    profile=profile,
                    date_end__gt=datetime.now(pytz.utc),
                )
            except Appointment.DoesNotExist:
                next_appt = None
            except MultipleObjectsReturned:
                next_appt = Appointment.objects.filter(
                    profile=profile,
                    date_end__gt=datetime.now(pytz.utc),
                ).first()

            appts = Appointment.objects \
                .filter(date_end__gt=datetime.now(pytz.utc)) \
                .exclude(profile__user=None)

        return {
            'profile': profile,
            'next_appt': next_appt,
            'appts': appts,
            'TIME_ZONE': TIME_ZONE,
            'current_date': datetime.now(tz),
        }

views.py

def index(request):
    response = Profile.objects.index(request)

    return render(request, 'users/index.html', response)

index.html

<div id="datePickerDate">
  {{ current_date }}
  <input type="hidden" name="year" value="{{ current_date|date:'Y' }}" autocomplete="off">
  <input type="hidden" name="month" value="{{ current_date|date:'n' }}" autocomplete="off">
</div>

结果

<div id="datePickerDate">
  Aug. 19, 2019, 4:27 p.m.
  <input name="year" value="" autocomplete="off" type="hidden">
  <input name="month" value="" autocomplete="off" type="hidden">
</div>

我想不到我所缺少的。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我发现问题出在我的JavaScript中。我通过jQuery将值添加到输入字段中而没有意识到,这些值是undefined。看来,当我从项目的其他部分或任何其他来源复制并粘贴代码时,我应该更清楚我实际上需要什么代码。