2

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).

11
  • 2
    Ah, you updated your post... please ignore my answer Commented Jul 8, 2011 at 17:19
  • According to the docs (docs.djangoproject.com/en/dev/ref/request-response/…) it should work. Maybe post your view code? Commented Jul 8, 2011 at 17:21
  • The "other" methods are the right ones, as described in the documentation: docs.djangoproject.com/en/dev/topics/http/file-uploads/… . You should define your file upload form in the model rather than directly in the HTML. Commented Jul 8, 2011 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. Commented Jul 8, 2011 at 17:23
  • 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. Commented Jul 8, 2011 at 17:48

1 Answer 1

3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

+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. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.