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.
{{ value|date:"d/m/Y" }}- Ben