Search code examples
djangodjango-viewsdjango-templates

How to display dates?


I would like to use a loop to create columns and display forward dates in them. The code below displays everything correctly in the console, but I don't know how to display it to the user.

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 %} 

Displays six numbered rows but each has the same date, and I would like to do from today's date to the date in 6 days.

I tried this method but I get an error: 'datetime.date' object is not iterable'

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

I want to display dates 6 days ahead, now it works on the principle that each date has a variable, and I want it to work and display on a loop basis

    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>


Solution

  • I get the same error if I pass the next_date variable to the dictionary. Need to save the values in a dt list and pass them to the dictionary. The loop in the template expects an iterable object that can be iterated over, but it receives a variable.

    views.py

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

    templates

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