Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:使用L10N将DateTimeField转换为字符串_Python_Django_Datetime_Datetime Format - Fatal编程技术网

Python Django:使用L10N将DateTimeField转换为字符串

Python Django:使用L10N将DateTimeField转换为字符串,python,django,datetime,datetime-format,Python,Django,Datetime,Datetime Format,使用Django模板标记,DateTimeField将如下所示: July 25, 2016, 7:11 a.m. 2016-07-23 14:10:01.531736+00:00 问题是,我的网站有一个无限长的滚动条,新的数据是通过AJAX获得的,所以我不能使用Django的模板标签。使用此选项获取日期: str(self.date_created) 我得到这样的东西: July 25, 2016, 7:11 a.m. 2016-07-23 14:10:01.531736+00:00

使用Django模板标记,DateTimeField将如下所示:

July 25, 2016, 7:11 a.m.
2016-07-23 14:10:01.531736+00:00
问题是,我的网站有一个无限长的滚动条,新的数据是通过AJAX获得的,所以我不能使用Django的模板标签。使用此选项获取日期:

str(self.date_created)
我得到这样的东西:

July 25, 2016, 7:11 a.m.
2016-07-23 14:10:01.531736+00:00

看起来不太好。。。是否有任何方法可以使用Django的默认格式转换DateTimeField值?谢谢。

实际上,对于ajax响应,您仍然可以使用Django内置的日期过滤器。在视图中,使用render_to_字符串,然后假设js需要json,将其作为json发送

import json
from django.template.loader import render_to_string

class YourAjaxResponseView(View):
    template_name = 'your_ajax_response_template.html'

    # I ASSUMED IT'S A GET REQUEST
    def get(self, request, *args, **kwargs):
        data = dict()
        data["response"] = render_to_string(
           self.template_name,
           {
            "your_date": your_date
           },
           context_instance=RequestContext(request)
        )
       return HttpResponse(
         json.dumps(data),
         content_type="application/json",
         status=200
      )
你的模板可以是这样的

 # your_ajax_response_template.html
 {{ your_date|date:"YOUR_FORMAT" }}

实际上,对于ajax响应,您仍然可以使用Django内置的日期过滤器。在视图中,使用render_to_字符串,然后假设js需要json,将其作为json发送

import json
from django.template.loader import render_to_string

class YourAjaxResponseView(View):
    template_name = 'your_ajax_response_template.html'

    # I ASSUMED IT'S A GET REQUEST
    def get(self, request, *args, **kwargs):
        data = dict()
        data["response"] = render_to_string(
           self.template_name,
           {
            "your_date": your_date
           },
           context_instance=RequestContext(request)
        )
       return HttpResponse(
         json.dumps(data),
         content_type="application/json",
         status=200
      )
你的模板可以是这样的

 # your_ajax_response_template.html
 {{ your_date|date:"YOUR_FORMAT" }}

您可以使用self.date\u created.strftime%B%d,%Y,%I:%M%p在后端格式化字段,也可以在前端格式化字段

var dateCreated = new Date(item.date_created);
dateCreated.toLocaleString()

您可以使用self.date\u created.strftime%B%d,%Y,%I:%M%p在后端格式化字段,也可以在前端格式化字段

var dateCreated = new Date(item.date_created);
dateCreated.toLocaleString()

非常感谢。我用了两个答案,但你的答案正是我想要的,尽管另一个答案也有帮助。谢谢!我用了两个答案,但你的答案正是我想要的,尽管另一个答案也有帮助。