-1

i write make an xml file and save it to the database with filefield and i want to make a download link to template. it is my view:

def xmlFile(request):
    all = request.POST
    data = dict(all)
    username = User.objects.get(username=request.user)
    xml = xmlFirst.dataToXml(data)
    with open('web/data/Fuzzy'+ str(username.username)+'1.xml','w') as myfile:
       myfile.write(xml)
       xmlFile = XmlFile(username= username, upload='Fuzzy'+ str(username.username)+'1.xml')
       xmlFile.save()
    xmlLink = XmlFile.objects.first()
    a =  xmlLink.upload
    context = {'data':a}
    return render(request, 'xmlFile.html', context) 

how can i reach the download link? and my template is :

<!DOCTYPE html>
<html>
<head>
    <title>aa</title>
</head>
<body>
    <a href="{{data}}">{{data}}</a>
</body>
</html>

when i click on that link it has http://localhost:8003/xmlFile/FuzzyMirab1.xml that i want to be my file path that is in

web
   data
      FuzzyMirab1.xml

i need my link will be http://localhost:8003/data/FuzzyMirab1.xml

  • Possible duplicate of [Django download a file](https://stackoverflow.com/questions/36392510/django-download-a-file) – gonczor Aug 29 '18 at 08:59

1 Answers1

1

Html5 offers a new attribute named "download" update your anchor tag as below

<a href="{{data}}" download="file_name.xml">

Don't need to write any view for downloading file

Parth Modi
  • 291
  • 1
  • 5