实时通知应用程序DateTimeField issu

2024-05-16 02:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我用沼泽龙写了一个通知程序。不幸的是,pub\u date变量包含微秒,我不希望它出现在那里。我如何在管理站点(a.m p.m版本)中将其格式化为“dd-MM-yyyy HH:MM:ss”或类似格式? 我的控制器.js地址:

var AnnounceControllers = angular.module('AnnounceControllers', []);


AnnounceControllers.controller('AnnounceListCtrl', ['$scope', '$dragon', function ($scope, $dragon) {
$scope.announceList = {};
$scope.announcements = [];
$scope.channel = 'announce';

$dragon.onReady(function() {
    $dragon.subscribe('announcements', $scope.channel, {announce_list__id: 1}).then(function(response) {
        $scope.dataMapper = new DataMapper(response.data);
    });

    $dragon.getSingle('announce-list', {id:1}).then(function(response) {
        $scope.announceList = response.data;
    });

    $dragon.getList('announcements', {list_id:1}).then(function(response) {
        $scope.announcements = response.data;
    });
});

$dragon.onChannelMessage(function(channels, message) {
    if (indexOf.call(channels, $scope.channel) > -1) {
        $scope.$apply(function() {
            $scope.dataMapper.mapData($scope.announcements, message);
        });
    }
});
}]);

我的应用程序.js地址:

var AnnounceApp = angular.module('AnnounceApp', [
'SwampDragonServices',
'AnnounceControllers'
]);

在HTML中:

<h3 ng-repeat="item in announcements">
        <div class="alert alert-info" role="alert">
          {{ item.content }} {{item.pub_date| date:'dd-MM-yyyy HH:mm:ss'}}
          <br>
        </div>
      </h3>

你知道吗型号.py地址:

pub_date = models.DateTimeField(auto_now_add = True)

你知道吗序列化程序.py地址:

class AnnounceSerializer(ModelSerializer):
class Meta:
    model = "post.Announce"
    publish_fields = ("content","pub_date")

Tags: iddateresponse地址channelfunctionlistmm
2条回答

您必须将DateTimeField转换为ISO 8061格式,这样AngularJS日期过滤器才能工作:

pub_date = models.DateTimeField(auto_now_add = True).isoformat(' ')

See this for more info.

编辑:

在进一步说明之后,问题是您得到的时间戳格式不正确,javascript无法将其识别为日期。See this plunkr for to how fix that.

我用了丹尼尔·科通的一些信息,做了如下工作:

class Announce(SelfPublishModel, models.Model):
serializer_class = AnnounceSerializer
announce_list = models.ForeignKey(AnnounceList)
content = models.CharField(max_length=100)
pub_date = models.DateTimeField(default=datetime.now)
date = models.TextField(null=True)

def __unicode__(self):
    return self.content


@receiver(pre_save,sender=Announce)
   def date_handler(sender,instance,*args,**kwargs):
       instance.date = instance.pub_date.isoformat()

不带参数的isoformat对我有用。你知道吗

相关问题 更多 >