Validate/clean a FileField on a non-model form in Django? -
i'm trying validate filefield extension type. i'm having trouble getting clean method field pickup posted value.
from django.forms.forms import form django.forms.fields import filefield django.forms.util import validationerror class testform(form): file = filefield(required=false) def clean_file(self): value = self.cleaned_data["file"] print "clean_file value: %s" % value return none @localhost def test_forms(request): form = testform() if request.method == "post": form = testform(request.post) if form.is_valid(): print "form valid" return render_to_response("test/form.html", requestcontext(request, locals()))
when run code, i'm getting following output:
clean_file value: none form valid
in other words, clean_file method not able file data. likewise, if returns none, form still valid.
here form html:
<form enctype="multipart/form-data" method="post" action="#"> <input type="file" id="id_file" name="file"> <input type="submit" value="save"> </form>
i have seen couple snippets solutions problem, cannot them work non-model form. both declare custom field type. when that, same problem; calling super() returns none object.
you're not passing request.files
form when instantiate in post.
form = testform(request.post, request.files)
see the documentation.
also note you're instantiating form twice on post, unnecessary. move first 1 else clause @ end of function (at same level if request.method == 'post'
).
Comments
Post a Comment