i would like to secure downloadable files in my project but dont know how to accomplish that. Each time the post_detail view get's called a new download link should get generated with a validity of 60 min and which also can only be access ones.
models.py
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(verbose_name="Post Title", max_length=25)
content = models.TextField(verbose_name="Post Content", max_length=5000)
tag = models.CharField(verbose_name="Tags/Meta - (sep. by comma)", max_length=50, blank=True)
category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
postattachment = fields.FileField(
verbose_name="Post Attachment",
blank=True,
null=True,
upload_to=get_file_path_user_uploads,
validators=[file_extension_postattachment, file_size_postattachment]
published_date = models.DateField(auto_now_add=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
class Meta:
verbose_name = "Post"
verbose_name_plural = "Post(s)"
ordering = ['-title']
def __str__(self):
return self.title
views.py
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
list_comments = Comment.objects.get_queryset().filter(post_id=pk).order_by('-pk')
paginator = Paginator(list_comments, 10)
page = request.GET.get('commentpage')
comments = paginator.get_page(page)
return render(request, 'MyProject/post_detail.html', {'post': post, 'comments': comments})
If smb. maybe has some practice example it would be really helpful.
Thanks in advance