【问题标题】:real time notification app DateTimeField issue实时通知应用程序 DateTimeField 问题
【发布时间】:2015-07-15 12:39:53
【问题描述】:

我使用 swampdragon 编写了一个通知应用程序。不幸的是, pub_date 变量包括我不希望它存在的微秒。如何在管理站点(a.m p.m 版本)中将其格式化为 'dd-MM-yyyy HH:mm:ss' 或类似? 我的controller.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);
        });
    }
});
}]);

我的 app.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>

models.py:

pub_date = models.DateTimeField(auto_now_add = True)

serializers.py:

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

【问题讨论】:

  • 如果角度日期过滤器不起作用,可能是pub_date 是一个字符串,请尝试将其解析为 JavaScript 日期?
  • 我在问题中添加了 serializers.py 我也尝试了新的 models.TextField 它实际上是使用 pre_save 信号将 pub_date 转换为 isoformat,但它没有工作

标签: javascript python angularjs django swampdragon


【解决方案1】:

您必须将 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.

【讨论】:

  • 我正在使用 swampdragon 教程,在本教程中使用 ModelSerializer 序列化对象,我知道“iso-8601”是输出格式。我看到输出为 2015-07-20 16:52:51.347082
  • 啊,好的,我现在看到你的问题了。您必须将要获取的数据解析为适当的时间戳,现在它只是一个字符串。 See this plunkr.
  • 它真的有效吗,因为当我运行这个 plunker 时输出日期为空?
  • Yeah, here's a screenshot. 我不知道为什么它不适合你。
  • 我尝试了 Windows 和 Lubuntu 并且日期为 null 没有改变。我可以用不同的方法做到这一点吗?
【解决方案2】:

我使用了 Daniel Cottone 的一些信息并做了这些:

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对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    相关资源
    最近更新 更多