Jason F. McBrayer wrote:
> I'm almost certainly missing something very, very simple here, but I
> haven't been able to find what it is on my own.
>
> One view in my app should allow users to upload an OPML file, which the
> app will parse and use to import feeds, but which won't otherwise be
> stored. As with most other non-model-based forms, I'm using a custom
> validator for this form:
>
> class AddFromOPMLManipulator(formfields.Manipulator):
> def __init__(self):
> self.fields = (
> formfields.FileUploadField(field_name="opml_file",
> is_required=True,),
> )
>
>
> When the form is submitted, I get this traceback:
>
> Traceback (most recent call last):
>
> File
> "/usr/lib/python2.3/site-packages/django-1.0.0-py2.3.egg/django/core/handlers/base.py",
> line 64, in get_response
> response = callback(request, **param_dict)
>
> File
> "/home/jmcbray/Documents/topical/business/site_test/apps/engulf/views/util.py",
> line 231, in _checklogin
> return view_func(request, *args, **kwargs)
>
> File
> "/home/jmcbray/Documents/topical/business/site_test/apps/engulf/views/engulf.py",
> line 333, in add
> opml_errors = opml_manipulator.get_validation_errors(opml_data)
>
> File
> "/usr/lib/python2.3/site-packages/django-1.0.0-py2.3.egg/django/core/formfields.py",
> line 70, in get_validation_errors
> validator(new_data.get(field.field_name, ''), new_data)
>
> File
> "/usr/lib/python2.3/site-packages/django-1.0.0-py2.3.egg/django/core/formfields.py",
> line 491, in isNonEmptyFile
> if not field_data['content']:
>
> TypeError: string indices must be integers
>
> The form enctype is multipart/form-data. Obviously the value of the
> field the validator is seeing is a string, and obviously it shouldn't
> be. So what's going wrong here? It's surely something that should be
> completely obvious, but somehow I'm just managing to miss it.
>
> --
> +----------------------------------------------------------------+
> | Jason F. McBrayer [EMAIL PROTECTED] |
> | "If you wish to make Pythocles wealthy, don't give him more |
> | money; rather, reduce his desires." -- Epicurus |
It took me a while but I figured out how to do this:
if request.POST:
new_data = request.POST.copy()
new_data.update(request.FILES) # This has to be added
errors = manipulator.get_validation_errors(new_data)
if not errors:
new_message = manipulator.save(new_data)
return HttpResponseRedirect("%i/" % new_message.id)