Read from an uploaded file and put data in a form for automatic Django validation

31 views
Skip to first unread message

forthfan

unread,
Jul 28, 2012, 10:43:15 PM7/28/12
to django...@googlegroups.com
Hi all,

Being lazy, I want Django to validate the stuff I read from an uploaded file as if it were being posted.  Is there a nice way to do this?

Derek

unread,
Jul 29, 2012, 12:37:13 PM7/29/12
to django...@googlegroups.com

Bill Beal

unread,
Jul 30, 2012, 9:24:37 PM7/30/12
to django...@googlegroups.com
This works:

In forms.py:

class UploadForm(forms.Form):
  name = forms.CharField(label='name')
  addr1 = forms.CharField(label='addr1')

Interactive:

>>> from django import *
>>> from app.forms import *
>>> d = {'name': 'Fibber McGee', 'addr1': '79 Wistful Vista'}
>>> form = UploadForm(d)
>>> form.is_bound
True
>>> form.is_valid()
True
>>> form.cleaned_data
{'name': u'Fibber McGee', 'addr1': u'79 Wistful Vista'}
>>>

I get a bound form from this, and Django does the validation for me.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/kGJ40VUVY90J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages