Last active
December 10, 2015 00:29
-
-
Save allieus/4351558 to your computer and use it in GitHub Desktop.
Django 에서 json.JSONEncoder 에 datetime 형식 추가
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from datetime import datetime | |
from django.http import Http404 | |
from django.http import HttpResponse | |
class DateTimeJSONEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, datetime): | |
return obj.strftime('%Y-%m-%d %H:%M') # FORMAT | |
else: | |
return super(DateTimeJSONEncoder, self).default(obj) | |
def json_encode(data): | |
return DateTimeJSONEncoder().encode(data) | |
def view_function(request): | |
data = {} | |
json_string = json_encode(data) | |
return HttpResponse(json_string, mimetype='application/javascript') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment