Python迭代列表日期时间值

时间:2018-05-06 03:15:04

标签: python django python-3.x

我正在使用django-eventtools并将我的出现数据输出为元组。我尝试将其转换为列表以便能够遍历它,但我似乎无法让它工作。我的querytest变量是我的尝试,并尝试从原始元组迭代新列表。

这是我的类和方法,我只是调用django模板的上下文:

class MyDashboardView(LoginRequiredMixin, TemplateView):
template_name = 'index2.html'
login_url = 'login/'

def get_context_data(self, **kwargs):
    context = super(MyDashboardView, self).get_context_data(**kwargs)
    context['events'] = self.get_events_items_context()
    return context

def get_events_items_context(self):
context = {}

    query = list(RecurringAudits.objects.all().all_occurrences())
    querytest = [s for t in query for s in t]

    context['query'] = querytest

    return context

这是我的模板代码:

{% load static %}
{% block content %}
<div class="row">
      {{ events.query }}
  </div>
{% endblock %}

方法all_occurrences以生成器格式输出数据:

<generator object combine_occurrences at 0x7f07cee47258> 

以下是查询变量输出的内容:

[(datetime.datetime(2018, 3, 31, 0, 0, tzinfo=<UTC>), datetime.datetime(2018, 3, 31, 0, 0, tzinfo=<UTC>), {'title': 'Monthly Update Schedule'}), (datetime.datetime(2019, 3, 31, 0, 0, tzinfo=<UTC>), datetime.datetime(2019, 3, 31, 0, 0, tzinfo=<UTC>), {'title': 'Monthly Update Schedule'}), (datetime.datetime(2020, 3, 31, 0, 0, tzinfo=<UTC>), datetime.datetime(2020, 3, 31, 0, 0, tzinfo=<UTC>), {'title': 'Monthly Update Schedule'})]

以下是querytest输出的内容:

[datetime.datetime(2018, 3, 31, 0, 0, tzinfo=<UTC>), datetime.datetime(2018, 3, 31, 0, 0, tzinfo=<UTC>), {'title': 'Monthly Update Schedule'}, datetime.datetime(2019, 3, 31, 0, 0, tzinfo=<UTC>), datetime.datetime(2019, 3, 31, 0, 0, tzinfo=<UTC>), {'title': ' Monthly Update Schedule'}, datetime.datetime(2020, 3, 31, 0, 0, tzinfo=<UTC>), datetime.datetime(2020, 3, 31, 0, 0, tzinfo=<UTC>), {'title': 'Monthly Update Schedule'}]

我想要的输出(或类似的东西):

2018-3-31, 2018-3-31, Monthly Update Schedule, 2019-3-31, 2019-3-31, Monthly Update Schedule, 2020-3-31, 2020-3-31, Monthly Update Schedule

0 个答案:

没有答案