I'm working on a comment funktion in django. It should be possible for the user to edit the comment until 15 min after he posted the comment.
The editing of the comment works fine, but I don't know how I can handle it with the 15 min. My idea was it to use a Bullit-in template, so that the editing button just appear if the post is not older then 15 min. Kind of this:
{% if comment.publication_date - now() <= 15 min %}
<button type="">Edit comment</button>
{%endif%}
Is there any way to implement it like that or do I have to do it on a different way?
Thanks a lot!
Cheers
I would make a method on the Comment model that returns a boolean showing whether or not it is possible to edit.
class Comment(models.Model):
...
def can_edit(self):
fifteen_mins_ago = datetime.datetime.now() - datetime.timedelta(seconds=60*15)
return self.publication_date >= fifteen_mins_ago
Now you can use this directly in the model:
{% if comment.can_edit %}
<button type="">Edit comment</button>
{%endif%}