1

我知道这个问题之前已经被问过,我已经完成了各种答案以及文档:http ://docs.python.org/2/library/datetime.html#datetime-objects但我仍然没有能够计算两个日期时间字段之间的分钟数。

这是模型:

class Activity(models.Model):
    user = models.ForeignKey(User)

    created_date = models.DateTimeField(default=datetime.now)

    activity_type = models.IntegerField(default=0) 

    def time_posted_description(self):
        now = datetime.now()
        then = datetime.fromtimestamp(self.created_date)
        tdelta = now - then
        minutes = tdelta.total_minutes()

        return minutes

风景:

def portal_game_activity_page(请求):

activity = Activity.objects.filter(user=request.user).order_by('-created_date')

pending_requests_list = FriendshipRequest.objects.filter(to_user=request.user, accepted=False)
number_pending_requests = len(pending_requests_list)

return render(request, 'portal/portal_game_activity.html', {'game_activity_page': True,'number_pending_requests':number_pending_requests,'activity':activity, })

和模板:

{% extends "portal/portal_base.html" %}

{% block content %}
    <section>
        <table class="table">
            <tbody>
                {% for event in activity %}
                <tr>
                    <td>{{event.user}}</td>
                    <td>{{event.created_date}}</td>
                    <td>{{event.time_posted_description}}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>    
    </section>
{% endblock %}

非常感谢任何反馈/帮助。

4

2 回答 2

6
now = datetime.now()
then = self.created_date
tdelta = now - then
minutes = tdelta.total_seconds() / 60

return minutes
于 2013-04-24T21:19:26.590 回答
0

datetime.fromtimestamp get timestamp argument:

    then = datetime.fromtimestamp(self.created_date.second)

'datetime.timedelta' object has no attribute 'total_minutes'

于 2013-04-25T05:32:48.103 回答