其实你仍然可以使用Django内置的date
过滤器来实现ajax响应。在视图内部利用render_to_string
然后发送为json(假设您的js预计为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" }}
谢谢!我用了两个答案,但你的是我正在寻找的,但另一个也有帮助。 – Antoni4040