'How can I download the exact uploaded file in Django?
I was writing a code to download my file using a link which I have uploaded through the model in Django.
i.e in my model.py
class uploadFile(models.Model):
id=models.CharField("Book id ",max_length=200,null=True,default="")
name=models.CharField("Book name",max_length=200,null=True,default="")
file=models.FileField(upload_to="media/ebooks")
and in my view.py I have 2 functions
def getdata(request):
books=uploadFile.objects.all()
n=len(QPapers)
print("hello",QPapers)
params={'QPapr':QPapers,'total_items':n}
return render(request,'index.html',params)
def getdata2(request,nameparam):
books=uploadFile.objects.all().filter(name=nameparam)
n=len(QPapers)
print("hello",QPapers)
params={'QPapr':QPapers,'total_items':n}
return render(request,'index.html',params)
and in index.html
<a href="{% 'getdata' %}" download>download1</a>
for this urls.py is path('getdata',views.pdfNotes,name='pdfNotes'),
here the file downloads correctly using above code
but when i am using the below code and calls to 2nd function with the name of book then instead of downloading file i have uploaded it downloads the index.html page.
<a href="{% 'getdata2' 'book1' %}" download>download2</a>
for this urls.py is path('getdata2/<str:name>/',views.pdfNotes,name='pdfNotes'),
Is it because of filter I am using, or is the problem somewhere else? I want to be download the specific file also that's why I am using the filter here...
Solution 1:[1]
The name of the parameter of your view is nameparam, not , you thus specify this in the URL patterns with:name
# ↓ not name
path('getdata2/<str:nameparam>/',views.pdfNotes,name='pdfNotes'),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willem Van Onsem |
