Hi

I'm still trying to find a minimal example.
This http://www.djangosnippets.org/snippets/103/ is very specific to a zip file 
type.
A search of the Django Users Group using 'newforms upload' gives a few posts:
     newforms and FileInput
     Newform and File Upload problem
     "'BoundField' object is unsubscriptable " error when trying to upload file
     validating using newforms
and many of these deal with problems in trying to either do or validate image 
uploads. All seem to have problems and use very different methods.

I'd like to get a min example up and running and then add some validation (like 
checking that it is an image and say checking its size) after I have a small 
test 
working fine.

Michael Lake wrote:
> Hi all
> 
> Guillaume wrote in May "Newform and File Upload problem" ....
> 
> 
>>Well, my message was maybe a little long. I still have not found answers
>>to my problem. To make it short, Is there anywhere a short example of file
>>upload using newform ?
>>The most simple thing with a model with 1 FileField, the smallest
>>template and form associated and the handling code.
>>That would be great, and probably useful for some other django beginers.
> 
> 
> I'm also struggling with newforms to upload an image file(s). I have seen 
> that there
> are a few posts that deal with zipfiles and images but all are sufficiently 
> different
> that I'm more confused.
> 
> It would be really appreciated by myself and probably a few others if a 
> minimal but
> full working example could be posted for a general file upload using newforms.
> I realise there are two cases here: upload file to a subdirectory of 
> MEDIA_ROOT and
> upload to a database field. The latter is what I want to do with images.
> 
> MINIMAL MODEL:
> 
> class Test(models.Model):
>      name = models.CharField(maxlength=255)
>      image = models.ImageField(upload_to='images', blank=True, null=True)
>      # What to do if you wish to upload to the image field itself and not to a
>      # directory images/ ?
> 
>      # def save(): # Do we need a save here?
> 
> The data in this model might be something like:
> 
> +----+------+------------------+
> | id | name | image            |
> | 1  | A    | some_binary_data | <-- binary data if image is in database
> | 2  | B    |                  |     otherwise text data of an image filename.
> +----+------+------------------+
> 
> Here we want a form that allows us to select a row and upload an image say 
> into that 
> row's image field.
> 
> VIEW:
> 
> def test_fileupload(request):
>      ''' This is used for testing file upload. '''
> 
>      class UploadForm(forms.Form):
>          ''' A Django newforms class that handles uploading files. '''
> 
>          choices = []
>          for test in Test.objects.all():
>              choices.append((test.id, test.name))
> 
>          # The variable choices is now something like: [(1,'A'),(2,'B')]
>          choice = forms.CharField(widget=forms.Select(choices=choices))
>          uploadFile = forms.Field(widget=forms.FileInput)
> 
>          def check_file(self):
>              # Check if the file is an image file.
>              # If it's not do something.
>              pass
> 
>          def save(self):
>              pass
> 
>      form = UploadForm()
> 
>      if (request.POST):
>          # The request.POST does not contain file upload data.
>          # Access instead request.FILES
>          id = request.POST['choice']
>          #filename = request.POST['upload']
>          file_name = request.POST['uploadFile']   # <-- does not work
> 
>          if (request.FILES):
>              file_data = request.FILES['uploadFile']
> 
>          if form.is_valid():
>              form.save()
>      else:
>          form = UploadForm()
> 
>      data = { 'form': form,
>      }
>      return render_to_response('app/test_fileupload.html', data)
> 
> 
> MINIMAL TEMPLATE:
> 
> <form action="test_fileupload" method="POST" enctype="multipart/form-data">
> Select row:  {{ form.choice }}
> Select File: {{ form.uploadFile }} and press Upload.
> <input type=submit name=upload value= ' Upload '>
> </form>
> 
> Looks like this:
> 
> Select row:  [               ] <-- drop down list to select the row
> Select file: [               ] [Browse] and press upload [Upload]
> 


-- 
Michael Lake
Computational Research Support Unit
Science Faculty, UTS
Ph: 9514 2238




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

Reply via email to