Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames J. McCombie
Python Web Development Techdegree Graduate 21,199 Pointsdjango file upload issue
Has anyone had experience of seeing the message 'Choose a valid type' when submitting a form with an file uploaded in django?
the filed in question has the FileInput widget, no modifications done.
message seen on the page, rendered as HTML
anyone know how to investigate the issue? would be handy if it caused an actual error, and I had the error page displayed.
Are there default file types for the FileInput?
Please do not suggest reading the docs, I have done this, the docs are actually a bit sparse in this area I feel
James
2 Answers
john larson
16,594 PointsHi James, I don't have an answer. I haven't officially started django yet. I was however looking up form submissions to databases with django (which I have installed) and one of the two imports needed to do the project were unrecognized. I don't know if this is similar to your issue, or if bugs and/or idiosyncrasies are part of this whole process.
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Pointstrial and error, honestly. The documentation is geared I think to doing things in a particular way, if you stray, there does not seem to be much coverage of the problems you might encounter, the docs do define a basic file upload without a model, and one with a model and a model form, since I was falling between the two, I dont think they thought to cover an imbecilic trying to do a bit of both, but after a while seeing there were two clearly defined ways to have done something led me to think that the problem was my 'unorthodox' approach. Lesson - of you are doing a Django project with an aspect you have not covered before, read the docs and other sources carefully before you start building. This was a project for the techdegree, so I guess a certain amount of 'homework' is expected, creating user profiles In Django, so hooking up a user profile with the existing user model, users have a profile picture. I am still learning Django, so I am not familiar with the best practices for uploading user media, so I thought to have a field in the model that stores the filepath for the profile picture, so that it can be accessed from the file system via the 'link' when needed. Model forms hook a form to your model, you do this in the class Meta by passing in the model you want it to use and the fields to include. So my user profile model form was getting confused as the model field was essentially a CharField, and on the form I was uploading a file, so I made the model form exclude that field, and defined it again as a 'normal' form field, and it worked, this made the view logic a lot more complex but hey, it works. If a was not using a model form I dont think I would have encountered the issue I think normal practice is to use an image/file field in the model, and then use a model form which will auto render the field as a file upload field, with the little browse button for the user to click and upload (I had to use a widget to make this happen, should have been a clue). You define MEDIA_ROOT in settings.py, and in your model the file field has an upload_to argument which you complete, when you do form.save() in your view the file goes magically to MEDIA_ROOT/<whatever you have in uploads_to> Im not really sure on the correct technical lingo, but that's my best attempt at explaining it...
john larson
16,594 PointsWell... holy crap. OK, so all that being said, would you recommend the Python techDegree?
James J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsJames J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsThanks for the reply. I have solved the issue now. It was a model form, the model had a FilePathField, I was trying to upload a file using the same field name on the form as the model, despite specifying the FileInput widget it seems as though the model form inner workings was expecting not to get a file hence the builtin validators were triggering. It seems the widgets do not override the base cleans or validators the model form applies based on the type of field you have in the model. I solved the issue by setting the file upload field as a 'class' field, outside the class Meta. File uploads were not covered in the course material, hence my foray into the docs proved to be a muddle.
James
john larson
16,594 Pointsjohn larson
16,594 PointsThat sounds like a bit of a roundabout. How did you figure out the solution? (considering the impasse wasn't covered in the course)