一周后禁用按钮

时间:2018-01-18 09:45:55

标签: python django

我做了一个你可以发表评论的页面。您也可以使用删除按钮删除评论。我想要的是,你发表评论后,你有一个星期的时间来删除它。所以一周后我想隐藏删除按钮。

尝试这样做时,我收到此错误: '<' 'datetime.timedelta'和'int'

的实例之间不支持

视图:

 class TechnologyDetailView(DetailView):
 model = Technology

 def get_queryset(self):
    group_permissions = 
 Permission.objects.filter(group__user=self.request.user)
    query = Technology.objects.filter(pk=self.kwargs['pk'], 
 is_active=True, permission__in=group_permissions)

    for tech in query:
        comments = Comment.objects.filter(technology=tech)
        now = datetime.now()

        for comment in comments:
            comment.timestamp = datetime.strptime('Jun 1 2005 1:33PM', 
 '%b %d %Y %I:%M%p')
            print(comment.timestamp)

            age = now - comment.timestamp

            if age < 604800:
                comment.is_removable = True
            else:
                comment.is_removable = False

    return query

模板:

 <h3>Comments</h3>
                {% for comment in technology.comment_set.all %}
                    <div class="row" style="border-bottom-style:solid; 
 border-bottom-width:1px; border-color:gray;">
                        <h6 style="font-weight:bold">Written by {{ 
 comment.user.name }}
                            on {{ comment.timestamp }}</h6>
                        <span>{{ comment.content|breaks }}</span>
                            <p>{{ comment.timestamp | timesince }}</p>


                        {% if comment.user == request.user %}
                            <a class="modal-trigger right" 
 href="#modal_{{ comment.pk }}">Delete Comment</a>
                        {% endif %}
                     <div id="modal_{{ comment.pk  }}" class="modal">
                                <div class="modal-content">
                                    <iframe frameborder="0" 
 id="encoder_iframe" height=300px width="100%" src="{% url 'delete-
 comment' comment.pk %}"></iframe>
                                </div>
                            </div>
                    </div>
                {% empty %}
                    <p>There are no comments</p>
                {% endfor %}
                <br>
                <h5>Add new Comment</h5>
                <iframe frameborder="0" id="encoder_iframe" 
height=300px width="100%" src="{% url 'add-comment' technology.pk %}">
</iframe>
            </div>
        </div>
    </div>

型号:

# Comment model
class Comment(models.Model):
user = models.ForeignKey(User, null=True)
technology = models.ForeignKey(Technology, null=True)
parent = models.ForeignKey('self', null=True, blank=True)
content = models.TextField(null=True)
timestamp = models.DateTimeField(null=True)
is_active = models.BooleanField(default=True)

我希望有人可以帮助我。

3 个答案:

答案 0 :(得分:1)

age不是整数,而是timedelta个实例。您需要将比较更改为:

if age.total_seconds() < 604800:

if age < timedelta(seconds=604800):

答案 1 :(得分:0)

不是真正的django问题,它是一个python日期时间问题:

>>> from datetime import datetime
>>> then = datetime.now()
>>> now = datetime.now()

两个日期时间之间的差异是timedelta:

>>> now - then
datetime.timedelta(0, 7, 452120)

您无法与数字进行比较:

>>> (now - then) < 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare datetime.timedelta to int

但你可以转换为秒(和分钟等)并比较:

>>> (now - then).seconds < 5
False
>>> (now - then).seconds 
7

可能的django问题更进一步说明,即使您可能没有删除旧评论的按钮,也不会阻止客户端发送删除请求 - 因此请确保您尝试删除旧评论在服务器上的视图处理程序中

答案 2 :(得分:0)

您无法比较int和datetime。尝试类似:

from datetime import datetime
now = datetime.now()
from datetime import timedelta
time_to_delete = timedelta(weeks=1)
comment1 = datetime(2018, 1, 15)
comment2 = datetime(2018, 1, 2)
now - time_to_delete <= comment1  # True
now - time_to_delete <= comment2  # False