0
votes

I have a simple view function:

def booking_request_list(request):
    #filter between dates...
    #or filter if new bookings.. (is booked = false) AND A DATE
    room_list = Room.objects.all()
    object_list = HousingRequest.objects.all()

    return render(request, 'housing/housing_requests_lists.html', {'booked_list':object_list, 'room_list':room_list})

Then in my template I just display the pieces like this:

<table class="table">      
        <tr>           
            <th>Check In Date</th>           
        </tr>
        {% for hou in not_booked_list %}
        <tr value={{hou.pk}}>
             <td> {{hou.checkin_date}} </td>   <td><input type="text" value="{{ hou.checkin_date}}" name="checkin_date_{{hou.pk}}" data-date-format="M/dd/yyyy" class="datepicker form-control dateinput form-control" tabindex="6" autocomplete="off" required id="id_checkin_date_{{hou.pk}}" bid="{{hou.pk}}"></td>
        </tr>
        {% endfor %}
      </table>

base.html has this portion in the end of the html file

 <script type="text/javascript">


            $(function() {
              var j = jQuery.noConflict();    
              j('.dateinput').datepicker({ format: "yyyy/dd/M" });
            });
          </script>
    </body>
    </html>

But it seems it is not respected by the date picker at all.

The displayed date for example is:

Nov. 11, 2019

In the database it is stored as YYYY-MM-DD

The definition of the model is:

class HousingRequest(models.Model):
    objects = models.Manager()

    room = models.ForeignKey('Room', null=True, blank=True, on_delete=models.CASCADE)
    user = models.ForeignKey('accounts.APOUser', on_delete=models.CASCADE)
    checkin_date = models.DateField('checkin date')

This works great when the form first loads, but when the users clicks on the datepicker and selects a date then inside the input box it puts:

MM/DD/YYYY

I tried above to set the format of the date picker via the data tag and it doesn't seem to take. I also don't know how to just force the date from django in the view to be MM/DD/YYYY (making it work like the date picker since the data setting doesn't seem to work). I can even just display the date:

<td> {{hou.checkin_date}}</td>  

And it still displays it as abbreviated month day, year I don't really care the displayed format but I just want it to be consistent between what is loaded and what the date picker puts. (With the knowledge to have more power/control in the future of my dates look and feel)

So not sure if it is a django configuration (I tried this in my settings file, not knowing if this was even right:

 DATE_FORMAT = {
    '%m/%d/%Y'
}

Or is it some more code in my view when I retrieve the record I need? Or do I have play with the datepicker still, which is a bootstrap4 datepicker I believe.

1
Most datepickers I've worked with require non-default values to be set within the javascript, so my suggestion would be to check the manual for where the over-ride values must be declared. - Hayden Eastwood
This may be helpful: docs.djangoproject.com/en/dev/ref/templates/builtins/#date It will at least let you format your date in the template {{ value|date:"d/m/Y" }} - Ben
what is your desired date format? - engin_ipek
I like the current one that is loaeded up by django eg: Nov. 5th, 2019 - Codejoy
@HaydenEastwood I found a piece in my base.html but it seems to not work put the code example in edit above, just a sample date format, I can put in anything and the datepicker always returns mm/dd/yyyy - Codejoy

1 Answers

1
votes

Try formatting the date as follows:

<script> 
$('#datepicker1').datepicker({ format: 'yyyy-mm-dd', uiLibrary: 'bootstrap4' });
</script>