如何显示日期?

问题描述 投票:0回答:1

我想使用循环来创建列并在其中显示远期日期。下面的代码在控制台中正确显示所有内容,但我不知道如何向用户显示它。

list=[1,2,3,4,5,6]
    for l in list:
        date = datetime.date.today()
        next_date=date+timedelta(days=l)
        print(next_date)
{% for l in l %}
        <p>{{ l }} - {{next_date}}</p>
    {% endfor %} 

显示六行编号,但每行都有相同的日期,我想从今天的日期到 6 天内的日期。

我尝试了此方法,但收到错误:“datetime.date”对象不可迭代'

{% for l in next_data %}
        <p>{{ l }} </p>
{% endfor %} 

我想显示提前6天的日期,现在它的工作原理是每个日期都有一个变量,我希望它能够循环工作和显示

    def Schedule(request):
        date = datetime.date.today()
        next_date1=date+timedelta(days=1)
        next_date2=date+timedelta(days=2)
        next_date3=date+timedelta(days=3)
        next_date4=date+timedelta(days=4)
        next_date5=date+timedelta(days=5)
        next_date6=date+timedelta(days=6)
        report=Submissions.objects.filter(Q(execution_date=date) | Q(execution_date=next_date1) | Q(execution_date=next_date2) | Q(execution_date=next_date3) | Q(execution_date=next_date4) | Q(execution_date=next_date5) | Q(execution_date=next_date6), status=4)    
    
        context={
            'date':date,
            'report':report,
            'next_date1':next_date1,
            'next_date2':next_date2,
            'next_date3':next_date3,
            'next_date4':next_date4,
            'next_date5':next_date5,
            'next_date6':next_date6,
        }
        return render(request, 'staff/work_schedule.html', context)

<table class="table table-striped table-hover">
        <div style="width: 100%;background-color: rgb(0, 0, 0);color: white;font-weight: 800;font-size: 12px;">{{date|date:'d.m.Y'}}</div>
        <tbody>            
            {% for qs in report %}
                {% if qs.execution_date|date:'d.m.Y' == date|date:'d.m.Y' %}
                <tr style="font-size: 10px;text-align: center;">
                    <th style="width: 8%;"><a style="text-decoration: none;color: black;" href="{% url 'system:ReportCard' qs.submissions_number_id qs.created %}">{{qs.client.user.get_full_name}}, tel.:{{qs.client.phone_number}}</a></th>
                    <th style="width: 8%;">{{qs.client.post_code}} {{qs.client.city}}, ul.{{qs.client.street}}</th>
                    <th style="width: 30%;">{{qs.contents|linebreaks|truncatewords_html:50}}</th>
                    <th style="width: 30%;">{{qs.details_planned_service|linebreaks|truncatewords_html:50}}</th>
                    <th style="width: 20%;"><a style="text-decoration: none;" href="{% url 'system:StatusUpdate' qs.submissions_number_id qs.created %}?next={{ request.path|urlencode }}"><span style="font-weight: 600;color: green;">{{qs.get_status_display}}</span></a></th>
                </tr>
                {% endif %}
            {% endfor %}
        </tbody>
    </table>

django django-views django-templates
1个回答
0
投票

wiews.py

def about(request):
    list = [1, 2, 3, 4, 5, 6]
    dt = []
    for l in list:
        date = datetime.date.today()
        next_date = date + timedelta(days=l)
        dt.append(next_date)
        print(next_date)

    return render(request, 'about.html', {'next_data': dt})

模板

{% for b in next_data %}
<p>{{ b }}</p>
{% endfor %}
© www.soinside.com 2019 - 2024. All rights reserved.