2011-07-06 29 views
1

我的問題是想顯示我的日期在%B%d格式python 實際上我用我的日期轉換和現在的問題是何時傳遞數組(日期數據)到json序列化它將不會work.here是我的代碼如下: 即時通訊新的django可以提前幫助thanx。json序列化器與jquery

def today_event(request): 
    today_event = scene() 

    now = datetime.now() 
    now = str(now.strftime("%d %B %Y")) 
    current_date_time = datetime.strptime(now,"%d %B %Y") 
    today_event = scene.objects.filter(startdate=current_date_time) 

    today_data = [] 
    for today in today_event: 
     today.startdate = today.startdate.strftime("%B %d") 
     today_data.append(today)  

    json_serializer = serializers.get_serializer("json")() 
    data_event = json_serializer.serialize(today_data, ensure_ascii=False) 
    return HttpResponse(data_event) 

回答

1

該問題與日期無關。簡單地說,serializers模塊僅用於查詢集。你有一個標準的名單,所以只使用基本的simplejson模塊:

from django.utils import simplejson 
data_event = simplejson.dumps(today_data) 
+0

其實我想顯示7月06日因此這就是爲什麼我使用today_event = scene.objects.filter(開始日期= current_date_time) today_data = [] 爲當今today_event: today.startdate = today.startdate.strftime( 「%B%d」) today_data.append(今天) json_serializer = serializers.get_serializer( 「JSON」)() data_event = json_serializer。 serialize(today_data,ensure_ascii = False) return HttpResponse(data_event) – gur

+0

你爲什麼1)忽略我的答案(這是正確的)並且2)重新發布相同的代碼(以不可讀的格式)?如果你沒有聽到回覆,詢問一個問題有什麼意義? –