0

enter image description here

I have a model table like above which contains two columns on which dates on which an item is being sold in a month.

I want to show the entire month table in Django template instead of showing only the dates of sale, and on each day showing "yes" if the sale is there and keeping blank if no sale is there on that day. I.e How to iterate in Template through all the days in a month.

How the code should be in Django "views.py" and in Template

models.py

class SaleDates(models.Model):
    date_of_sale = models.DateTimeField(blank=True, null=True)
    sold_choices = (
        ('yes', 'yes'),
        ('no', 'no'),       
    )
    sold_status = models.CharField(max_length=9, choices=sold_choices,)

template.html


                    {% for item in list %}
                        {% if forloop.first     %}
                            <table class="table table-hover table-bordered">
                                <tr>
                                        <th>Sl.NO</th>
                                        <th>date</th>
                                        <th>details</th>
                                </tr>
                        {% endif %}
                           <tr>
                               <td>
                                   {{ forloop.counter }} <br/>

                               </td>
                               <td> {{ item.date_of_sale }}</td>
                               <td>
                                    {{ item.sold_status }}

                               </td>                            

                           </tr>
                        {% if forloop.last %}
                            </table>
                        {% endif %}

                   {% empty %}
                   {% endfor %}
1
  • Can we see your view where you call the template? The provided answer is solid but doesn't show how to render the table. Are you wanting table ths for each day of the month 1-28/31? My first though is that this is going to be a huge table
    – ViaTech
    Commented Jul 6 at 14:16

1 Answer 1

0

You could do something like this where we first create a list with all the days in a month. After we have that list we fill the days we have a sale with those sales:

def get_month_dates(year, month):
    num_days = monthrange(year, month)[1]
    dates = [datetime(year, month, day) for day in range(1, num_days + 1)]
    return dates

def sales_calendar(request):
    today = datetime.today()
    year = today.year
    month = today.month

    month_dates = get_month_dates(year, month)
    sales = SaleDates.objects.filter(date_of_sale__year=year, date_of_sale__month=month)

    sales_dict = {sale.date_of_sale.date(): sale.sold_status for sale in sales}

    calendar = []
    for date in month_dates:
        status = sales_dict.get(date.date(), 'no')
        calendar.append({'date': date, 'sold_status': status})

    return render(request, 'template.html', {'calendar': calendar})

Also I would remove the {% if forloop.first %} from the template and just put this at the top

<table class="table table-hover table-bordered">
  <tr>
    <th>Sl.NO</th>
    <th>date</th>
    <th>details</th>
  </tr>

And then after the forloop you can do <table/>, it makes your code a lot more readable and works the same :)

1
  • Since the OP is a having confusion on rendering the table, can you please show the rendered template with the calendar dates added in the header? Other than that good answer!
    – ViaTech
    Commented Jul 6 at 14:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.