[英]How to convert JS date string into django datetime format?
I have a Bootstrap datepicker and on date selection, I make an ajax call and send in the user selected date, I want to change the date of the object in the database, but I get this error from Django: 我有一个Bootstrap datepicker和日期选择,我进行ajax调用并发送用户选择的日期,我想更改数据库中对象的日期,但我从Django得到此错误:
[u"'04/23/2018' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
How do I convert it to the specified format? 如何将其转换为指定的格式? This is my code:
这是我的代码:
Template: 模板:
$('#project_date').on('changeDate',function()
{
$.ajax({
url: btn.attr("data-url"),
type: 'get',
dataType: 'json',
data: {
new_date: $('#project_date_input').val()
},
success: function(data){
//do something
}
views.py views.py
def project_change_date(request, pk):
data = dict()
project = get_object_or_404(Project, pk=pk)
project.end_date = request.GET.get('new_date')
opp.save()
import datetime
project.end_date = datetime.datetime.strptime(request.GET.get('new_date'), '%m/%d/%Y').strftime('%Y-%m-%d')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.