[Answered ]-Filter by month in django view

1đź‘Ť

âś…

You can’t use dot in arguments names so this part UserCommission.objects.filter(created_time.month=year) causes SyntaxError
so you must change it to UserCommission.objects.filter(created_time__month=year)

for more info read exac in Django site !

Django site says :

In MySQL, a database table’s “collation” setting determines whether exact comparisons are case-sensitive. This is a database setting, not a Django setting. It’s possible to configure your MySQL tables to use case-sensitive comparisons, but some trade-offs are involved. For m
ore information about this, see the collation section in the databases documentation.

and for last error you can change the value of your inputs in your template:

                <option value="1">January</option>
                <option value="2">February</option>
                <option value="3">March</option>
                <option value="4">April</option>
                <option value="5">May</option>
                <option value="6">June</option>
                <option value="7">July</option>
                <option value="8">August</option>
                <option value="9">September</option>
                <option value="10">October</option>
                <option value="11">November</option>
                <option value="12">December</option>
👤Mazdak

1đź‘Ť

The problem is that the month that’s stored and retrieved by django is the month number not the month name. There are many reasons for this, but one of the basic ones is that you may be developing a site for multiple languages, so its a lot easier to store the number and then display January, janvier or Januar or enero depending on what language the user speaks.

The easiest way to fix this is to change the template so that the value is the month number:

<select name="year">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
</select>

You might also want to consider that the name is year but you are actually sending the month, and rename it accordingly.

👤Burhan Khalid

Leave a comment