5

I looked at the various questions similar to mine, but I could not find anything a fix for my problem.

In my code, I want to serve a freshly generated excel file residing in my app directory in a folder named files

excelFile = ExcelCreator.ExcelCreator("test")
excelFile.create()
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'
return response

So when I click on the button that run this part of the code, it sends to the user an empty file. By looking at my code, I can understand that behavior because I don't point to that file within my response...

I saw some people use the file wrapper (which I don't quite understand the use). So I did like that:

response = HttpResponse(FileWrapper(excelFile.file),content_type='application/vnd.ms-excel')

But then, I receive the error message from server : A server error occurred. Please contact the administrator.

Thanks for helping me in my Django quest, I'm getting better with all of your precious advices!

trinchet
  • 6,753
  • 4
  • 37
  • 60
Speccy
  • 694
  • 8
  • 30
  • What type is `excelFile.file`? Also, have you tried running this with the development `runserver` and `DEBUG = True`? That should get more useful feedback. – dgel Dec 21 '12 at 16:55
  • in the excelFile class, I open a file using open() so it is a file. Like I said, it return the server error thing not the regular debug view. – Speccy Dec 21 '12 at 17:04
  • Is `DEBUG = True` in your `settings.py`? – dgel Dec 21 '12 at 17:06
  • Can you provide more of the view file than just what you've included? From the response you mentioned (A server error occurred) it doesn't sound like DEBUG is set to True even though you said it was, perhaps you can provide more of the error message? My first thought was that you didn't import `FileWrapper` from `django.core.servers.basehttp` in your views.py because that would result in an error like you mentioned. – zzzirk Dec 27 '12 at 14:55
  • Hi zzzirk, you can view more of it here: http://pastebin.com/7Dn5JFiU I'm serious, DEBUG is set to True, I wonder why I don't get the debug view in that particular case, for other errors it works just fine – Speccy Dec 27 '12 at 16:07
  • I believe you have DEBUG set but the fact that it is behaving as it is makes me question if that setting is being overridden somewhere. That said, looking at your pastebin I can't see anything that really looks wrong. I would suggest care be given when using a naked `except` as you are as that can hide problems. I will continue thinking on this. – zzzirk Dec 27 '12 at 18:06
  • Oh, also, this is the views.py for the formulaire app? – zzzirk Dec 27 '12 at 18:11
  • I just had one more thought, are you running this locally through `manage.py runserver`? If not can you? This would allow you to see errors on the console that you can't see from the page since you are getting just a base error page with no details. For example, when testing your `views.py` I got a `TemplateDoesNotExist` exception due to my not having your template. – zzzirk Dec 27 '12 at 18:48
  • Thanks zzzirk for your help, I'm using runserver, and this is the view for formulaire app. In my code, I commented the try line, so I can see the errors. In fact, all that I want to do with this code is let the user download a excel file that is automatically generated in the formulaire/files directory, I'm sure it cannot be that hard... – Speccy Dec 27 '12 at 18:51
  • Did you see any errors on the console when you attempted to download the file? – zzzirk Dec 27 '12 at 19:03
  • Is `ExcelCreate` from a public module, or something written by you (or in house)? Does excelFile.file provide a handle to the file, or is it the data of the file? FileWrapper expects a handle, so if that's not what excelFile.file is it could definitely cause the behavior you are seeing. – zzzirk Dec 27 '12 at 19:22
  • I was using pyDev with Eclipse, so did not see the console output at first... You're right, I should have checked that before. It said that the file needed to be open so it can be sent. Thanks, problem resolved! (you can answer the question zzzirk so you can earn the bounty) – Speccy Dec 27 '12 at 19:32

2 Answers2

7

First, you need to understand how this works, you are getting an empty file because that is what you are doing, actually:

response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'

HttpResponse receives as first arg the content of the response, take a look to its contructor:

def __init__(self, content='', mimetype=None, status=None, content_type=None):

so you need to create the response with the content that you wish, is this case, with the content of your .xls file.

You can use any method to do that, just be sure the content is there.

Here a sample:

import StringIO
output = StringIO.StringIO()
# read your content and put it in output var
out_content = output.getvalue()
output.close()
response = HttpResponse(out_content, mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'
trinchet
  • 6,753
  • 4
  • 37
  • 60
0

I would recommend you use:

python manage.py runserver

to run your application from the command line. From here you will see the console output of your application and any exceptions that are thrown as it runs. This may provide a quick resolution to your problem.

zzzirk
  • 1,582
  • 2
  • 14
  • 18
  • Thanks, this helped me solve my problem. **The file has to be left opened to use FileWrapper** – Speccy Dec 27 '12 at 21:02