使用django频道时的时间格式

时间:2017-11-07 14:34:58

标签: django django-templates django-channels

我正在使用Django频道更新我网站上的实时数据。

class VisitorBinding(WebsocketBinding):

    model = VisitorInfo
    stream = "livelist"
    fields = ["user_id", "ip","device_type","time_stamp","last_active","active"]

    @classmethod
    def group_names(cls, *args, **kwargs):
        print("grouped");
        return ["binding.values"]

    def has_permission(self, user, action, pk):
        return True

我在前端的时间是2017-11-07T14:27:37.683Z,其中通过视图呈现的实际时间是2017年11月7日,下午07:56 如何让时区识别并使用同一时间格式化?

html代码:

<script>
        $(function () {
            var ws_path =  "/stream/";
            console.log("Connecting to " + ws_path);

            var webSocketBridge = new channels.WebSocketBridge();
            webSocketBridge.connect(ws_path);
            webSocketBridge.listen();
            webSocketBridge.demultiplex('livelist', function(payload, streamName) {
                // Handle different actions
               // Handle different actions
                console.log("a");
                if (payload.action == "create") {
                    // Create the new integer value
                    console.log("b");
                var content = "<tr id='" + payload.pk + "'><td id=1><a href=\"/action_map/"+payload.pk+"\">" + 
    payload.data.user_id + "</a></td> <td id=2><a href=\"/action_map/"+payload.pk+"\">" + 
    payload.data.ip + "</a></td><td id=3><a href=\"/action_map/"+payload.pk+"\">" + 
       payload.data.device_type + "</a></td> <td id=4><a href=\"/action_map/"+payload.pk+"\">" + 
payload.data.last_active +  "</a></td> <td id=5><a href=\"/action_map/"+payload.pk+"\">" + payload.data.active + "</a></td> </tr>";

                    $("#table2").prepend(content);



                } else if (payload.action == "update") {


                    if($('#'+payload.pk).length){
                             if(payload.data.active==false)
                    {   
                            $("tr[id=" + payload.pk + "]").remove();

                    }else{


                    $("tr[id=" + payload.pk + "] td[id="+1+"]a").text(payload.data.user_id);
                    $("tr[id=" + payload.pk + "] td[id="+2+"]a").text(payload.data.ip);
                     $("tr[id=" + payload.pk + "] td[id="+3+"]a").text(payload.data.device_type);
                    $("tr[id=" + payload.pk + "] td[id="+4+"]a").text(payload.data.last_active);
                    $("tr[id=" + payload.pk + "] td[id="+5+"]a").text(payload.data.active);
                    } 
                }else{

                   if(payload.data.active==true)
                   {        var content = "<tr id='" + payload.pk + "'><td id=1><a href=\"/action_map/"+payload.pk+"\">" +
        payload.data.user_id + "</a></td> <td id=2><a href=\"/action_map/"+payload.pk+"\">" +
        payload.data.ip + "</a></td><td id=3><a href=\"/action_map/"+payload.pk+"\">" +
       payload.data.device_type + "</a></td> <td id=4><a href=\"/action_map/"+payload.pk+"\">" + 
payload.data.last_active +  "</a></td> <td id=5><a href=\"/action_map/"+payload.pk+"\">" + payload.data.active + "</a></td> </tr>";


                    $("#table2").prepend(content);
                   }
                }


                } else if (payload.action == "delete") {

                    $("tr[id=" + payload.pk + "]").remove();

                } else {

                    console.log("Unknown action " + payload.action);

                }
            });

});

网络套接字

class Demultiplexer(WebsocketDemultiplexer): 
    consumers = {
        "livelist": VisitorBinding.consumer,
        # "actionmap": SessionBinding.consumer,
    }

    groups = ["binding.values"]

settings.py中的Variabeles

TIME_ZONE =&#39; Asia / Kolkata&#39;

USE_I18N = True

USE_L10N = True

USE_TZ = True

感谢您的时间。

0 个答案:

没有答案