How to change Django date format to dd/ mm/ yy?

纵然是瞬间 提交于 2019-12-10 15:43:50

问题


In forms.py:

class DobForm(forms.ModelForm)  
    dob = forms.DateField(widget=forms.TextInput(attrs={'class':'datepicker'}),required=True,input_formats=['%d/%m/%Y',])

    class Meta:
        model = Dob

In my html:

$(function() {$(".datepicker").datepicker({ maxDate: '0',dateFormat: 'dd-mm-yy' })});</script>

Its thorwing error in the form: Enter a valid date


回答1:


You need to make the formats the same for both django and jquery.

Try this for the DateField (django):

...,input_formats=['%d-%m-%Y',])

and in the html use:

...,dateFormat: 'dd-mm-yyyy' })});</script>

I hope this works.



来源:https://stackoverflow.com/questions/37451479/how-to-change-django-date-format-to-dd-mm-yy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!