Thanks for the feedback.  The bit about it being iterable put me on
the right track and it turned out to be pretty simple.  For the
benefit of others, here's the basic code to select a CSV file on the
local machine and process it on the server using DictReader to manage
the columns:


FORM:
class ImportFileForm( forms.Form ):

        filename  = forms.FileField(
                label='Select File',
                required=True,
                help_text="Click Browse to find the file on your computer",
        )

(Template is just a standard form display)

VIEW:

def import_from_csv( request ):

        # =================================
        # Select File
        # =================================
        if request.POST:
                form = ImportFileForm( request.POST, request.FILES )
                if form.is_valid():
                        pass    # Fall through to process the file

        else:
                form = ImportFileForm()
                return render_to_response('admin_import_from_csv.html', {'form':
form,}, RequestContext(request) )

        errors = []     # List

        # =================================
        # Upload From File
        # =================================
        reader = csv.DictReader( request.FILES['filename'] )
        try:
                for row in reader:
                        group_name = str.strip( row['Group Name'] )
                        try:
                                job_name = str.strip( row['Job Name'] )
                        except KeyError:
                                job_name = ''
                        try:
                                shift_location = str.strip( row['Shift 
Location'] )
                        except KeyError:
                                shift_location = ''
                        try:
                                shift_date = str.strip( row['Shift Date'] )
                        except KeyError:
                                shift_date = ''
                        try:
                                start_time = str.strip( row['Start Time'] )
                        except KeyError:
                                start_time = ''
                        try:
                                end_time = str.strip( row['End Time'] )
                        except KeyError:
                                end_time = ''
                        try:
                                min_volunteers = str.strip( row['Min 
Volunteers'] )
                        except KeyError:
                                min_volunteers = ''
                        try:
                                max_volunteers = str.strip( row['Max 
Volunteers'] )
                        except KeyError:
                                max_volunteers = ''
                        try:
                                shift_notes = str.strip( row['Shift Notes'] )
                        except KeyError:
                                shift_notes = ''

                        # Do something with the row values here.....

        except Exception, e:
                errors += [( reader.line_num, "Serious Error Importing File: " +
str( e ) )]

        # Done!  Render a success screen or ?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to