To get a new object from a ModelForm you just save it, so the view
should be something like:
def uploadImage(request):
if request.method == 'POST': #if submitted
form = UploadNewImageForm(request.POST, request.FILES)
if form.is_valid():
ir = form.save()
rw = ResourceWrapper(name = form.cleaned_data['name'])
rw.save()
return HttpResponseRedirect('/Main_page/') # Redirect after
POST
form.save() will put the image in the right place.
Kip.
On Oct 26, 7:05 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Hello,
>
> I've been having some problems with FileField. I can create and save
> objects with the FileField file, but the file is never placed in any
> directory. Essentially, the database has the correct values, but no
> files are copied into the correct directory. I am using django-1.0.
>
> A temporary file is created in memory if the file is < 2.5M, and a
> temporary file is written to disk in /tmp/ if it is bigger. I have
> confirmed this. Do I have to specifically trigger the save to the hard
> disk to get the file to appear in my MEDIA_ROOT subfolder? From the
> documentation it appeared that this should be done automatically.
>
> Settings:
> MEDIA_ROOT = '/home/paul/proj/djangotest/uploads/'
>
> Model:
>
> class ImageResource(models.Model):
> wrapper = models.ForeignKey(ResourceWrapper)
> image = models.FileField(upload_to='images/')
> notes = models.CharField(max_length=200)
>
> class ResourceWrapper(models.Model):
> #incomplete class, for versioning later
> name = models.CharField(max_length=200)
> creationDate = models.DateField(auto_now_add=True)
>
> Form:
>
> class UploadNewImageForm(ModelForm):
> name = forms.CharField(max_length=200)
> class Meta:
> model = ImageResource
>
> View:
>
> def uploadImage(request):
>
> if request.method == 'POST': #if submitted
> form = UploadNewImageForm(request.POST, request.FILES) # A form
> bound to the POST data
>
> if form.is_valid(): # All validation rules pass
>
> # Process the data in form.cleaned_data
>
> #create a meta resource object
> rw = ResourceWrapper(name = form.cleaned_data['name'])
> rw.save()
>
> #this is either a TemporaryFile or MemoryFile depending on its
> size when I debug
> img = form.cleaned_data['image']
>
> ir = ImageResource(wrapper=rw,
> notes = form.cleaned_data['notes'],
> image = img, ##maybe the problem is here???
> )
> ir.save()
>
> #redirect to the main page
> return HttpResponseRedirect('/Main_page/') # Redirect after POST
>
> Any ideas? This is frustrating because there are no error messages to
> point me in the right direction, and everything else seems to work.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---