i want to prevent the user to update the post after 24 hours from the post date
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title= models.CharField(max_length=50)
post = models.TextField(max_length=400)
date = models.DateTimeField(auto_now_add=True)
edit = models.DateTimeField(auto_now=True)
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = '__all__'
class PostUpdateView(LoginRequiredMixin,SuccessMessageMixin,UpdateView):
model = Post
form_class = PostForm
success_message = "updated successfully"
template_name = 'store/create.html'
success_url = reverse_lazy('lists')
def form_valid(self,form):
return super().form_valid(form)
i want to let the user only be able to update the post before 24 hours during the Post date thanks
You can filter the queryset such that only the posts that it only contains Post
s that are written within the last 24 hours:
from datetime import timedelta
from django.utils.timezone import now
class PostUpdateView(LoginRequiredMixin,SuccessMessageMixin,UpdateView):
model = Post
form_class = PostForm
success_message = "updated successfully"
template_name = 'store/create.html'
success_url = reverse_lazy('lists')
def (self, *args, **kwargs):
return super().get_queryset(*args, **kwargs).filter(
)
def form_valid(self,form):
return super().form_valid(form)
This will return a HTTP 404 response in case the update period has been "expired".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.