1

When someone clicks Submit after selecting a file with the <input type="file"> element, how do I access the contents of the file in Django?

(It seems like the request sent to the request handler has no trace of the file anywhere -- not even in request.FILES.)

Currently my template is like:

<form method="post" enctype="multipart/form-data">
 <input type="file" enctype="multipart/form-data" name="file" accept="text/csv"/>
 <input type="submit" value="Upload" />
</form>

View:

def HandleRequest(request):
  print "**** request:", request

I don't see anything being printed about the file.

Note:

There's probably other ways to do this in Django, but I'm looking a solution using the simple input tag, and not something else (which would probably involve Javascript).

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 2
    Ah, you updated your post... please ignore my answer – Yuji 'Tomita' Tomita Jul 08 '11 at 17:19
  • According to the docs (https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.FILES) it should work. Maybe post your view code? – Jack M. Jul 08 '11 at 17:21
  • The "other" methods are the right ones, as described in the documentation: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs . You should define your file upload form in the model rather than directly in the HTML. – George Cummins Jul 08 '11 at 17:23
  • 2
    @George: Those may be better, but my method isn't "wrong", so I want to learn how to do things by hand before resorting to tools that automate it for me. If I don't know what a tool is doing, I usually don't use it. – user541686 Jul 08 '11 at 17:23
  • What is the output of printing request.FILES? – Jack M. Jul 08 '11 at 17:24
  • @Mehrdad: Django is the wrong tool for you if you plan to "do things by hand." Django is all about automation of common task. – George Cummins Jul 08 '11 at 17:25
  • Wait, `enctype` is an attribute of `` and of `
    `? Typo?
    – mechanical_meat Jul 08 '11 at 17:25
  • @JackM: It's an empty dictionary. @George: Thanks for the input, I'll keep that in mind. Though I really wish you could actually help, rather than just saying "urdoinitwrong"... @Adam: ooh that might be something... lemme see... – user541686 Jul 08 '11 at 17:26
  • @Adam: Nope, no difference... I still get `` for it. – user541686 Jul 08 '11 at 17:28
  • @Mehrdad Could you pop in the Python chat room? http://chat.stackoverflow.com/rooms/6/python – Jack M. Jul 08 '11 at 17:34
  • 1
    @George: actually, I find that Django is really nice precisely *because* it lets you do things by hand when you want/need to. I remember reading something in the Django book to the effect of "almost all parts of Django are optional" because they don't want the framework to get in the way. – André Caron Jul 08 '11 at 17:48

1 Answers1

2

The code you posted, as it is posted, works fine. The HTML is sound (though I think the enctype on the <input> is redundant at best), and a very simple view shows an InMemoryFile after the POST. The problem must lie in something between the browser and your view. Some things to check:

  • Middleware.
  • Apache.
  • Nginx.
  • Decorators on your view.
  • mod_wsgi configuration.
Jack M.
  • 30,350
  • 7
  • 55
  • 67
  • +1 I have no idea what happened, but after playing with some middleware stuff, it's working (though I'm not sure that's the issue... I think I missed something else but whatever). Thanks. :) – user541686 Jul 08 '11 at 17:50