2

I have a template where I render a response from a web service.

def XMLResponseView9request):
    ...
    with open(archivo_request,"r") as archivo:
         request_data = archivo.read()

    headers = {'Content-type':'text/xml'}
    data_response = requests.post(target_url, data=request_data, headers=headers)
    jdato = xmltodict.parse(data_response.content)
    data_xml = data_response.text
    data_template = json.dumps(jdato)

    return render_to_response('response.html',
                              {'data':data_template,'dataxml':data_xml},
                              context_instance=RequestContext(request))

Beside of render the response I want to serve the response as a file (xml or txt) to be downloaded.

I try passing the response as text (variable: data_xml) and served through a simple javascript function, but the file is empty or only have one line "[object Object]".

Is possible serve the response as file from the DJANGO view instead of passing the text to the template.

Thanks in advance

2
  • 1
    So you want the xml file to be downloaded by the browser, right?
    – utkbansal
    Dec 16, 2015 at 22:29
  • yes, I want be able to choose to download the file or only read the file in the template, because sometimes the response have a lot of information or a little information. The response depends of the WS previously selected for the user
    – joselegit
    Dec 17, 2015 at 15:09

1 Answer 1

0

To make a file downloadable, you just need to change one HTTP Header, Content-Disposition.

def some_view(response):
    xmlFile = open(pathout, 'r')
    myfile = FileWrapper(xmlFile)
    response = HttpResponse(myfile, content_type='application/xml')
    response['Content-Disposition'] = 'attachment; filename='+filename
    return response
3
  • I have a doubt. I need to create other view to pass the response and made it able to download or I can use the that I am using actually. Following your suggestion how can pass the variables and the template name.
    – joselegit
    Dec 18, 2015 at 15:23
  • So you want to show it on a page and there give a download button?
    – utkbansal
    Dec 18, 2015 at 17:05
  • yes. I try passing the data to the template, but when I download the file only say [object object]
    – joselegit
    Dec 21, 2015 at 14:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.