评论发布一周后隐藏删除按钮遇类型错误求助
Hey there! Let's fix that error you're running into and get your comment delete functionality working as expected.
What's Causing the Error?
The error '<' not supported between instances of 'datetime.timedelta' and 'int' happens because:
age = now - comment.timestampgives you atimedeltaobject (representing the time difference between now and the comment's creation time)- You're trying to compare this
timedeltadirectly to the integer604800(the number of seconds in a week) — Python doesn't know how to compare these two different types.
Step-by-Step Fixes
1. Fix the View Code
First, we need to compare age to another timedelta object instead of an integer. We'll also remove that hardcoded test timestamp (you don't want all comments to look like they're from 2005!).
Here's the updated view:
from datetime import datetime, timedelta # Make sure to import timedelta from django.views.generic import DetailView from django.contrib.auth.models import Permission from .models import Technology, Comment 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 ) now = datetime.now() for tech in query: comments = Comment.objects.filter(technology=tech) for comment in comments: # Use the actual timestamp from the database (remove the hardcoded test line!) age = now - comment.timestamp # Compare to a timedelta representing 1 week (much more readable than 604800) comment.is_removable = age < timedelta(weeks=1) return query
2. Update the Template to Respect is_removable
Right now your template only checks if the user owns the comment — we need to add the is_removable check to hide the button after a week.
Here's the modified template section:
<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> <!-- Add the is_removable check here --> {% if comment.user == request.user and comment.is_removable %} <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>
Bonus: Improve Your Comment Model
To avoid issues with empty timestamp values, update your Comment model to automatically set the timestamp when a comment is created:
class Comment(models.Model): user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL) technology = models.ForeignKey(Technology, null=True, on_delete=models.SET_NULL) parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL) content = models.TextField(null=True) # Auto-set timestamp when comment is created timestamp = models.DateTimeField(auto_now_add=True) is_active = models.BooleanField(default=True)
This way you'll never have to worry about a comment missing its timestamp value.
内容的提问来源于stack exchange,提问作者Xiem Om

