I'm quite new to using Django and I am trying to develop a website where the user can download file at the end of the post. Post model:
class Article(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
pub_date = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(upload_to='code_files/')
def __str__(self):
return self.title
I have a views:
def download(request):
response = HttpResponse(open(f"media/code_files/tests.py", 'rb').read())
response['Content-Type'] = 'text/plain'
response['Content-Disposition'] = f'attachment; filename=tests.py'
return response
How can I download the file linked with the post?