Django image upload not saving

126 views
Skip to first unread message

Bobby Gulshan

unread,
Jun 26, 2014, 1:58:31 PM6/26/14
to django...@googlegroups.com
No errors when hitting upload. But the image doesn't appear where I have indicated it ought to. Put an absolute path in MEDIA_ROOT and referenced the same in (upload_to) param ImageField. Not sure what I am missing. 

Model:

    class FileUploadHandler(models.Model):
        title = models.CharField(max_length=100)
        file = models.ImageField(upload_to='/Python27/Lib/site-packages/django/bin/mideastinfo/wiki/static/')

View:

    from models import Article, Edit
    from forms import ArticleForm, EditForm
    from forms import *
    from PIL import Image
    from models import FileUploadHandler

    def image_upload(request):
        if request.method == 'POST':
            form = UploadImageForm(request.POST, request.FILES)
            if form.is_valid():
                FileUploadHandler(request.FILES['image'])
                return render_to_response('wiki/gallery.html')
        else:
            form = UploadImageForm()
        return render_to_response('wiki/gallery.html', RequestContext(request, {'form': form}))

Mario Gudelj

unread,
Jun 26, 2014, 4:59:38 PM6/26/14
to django...@googlegroups.com
I always follow a similar pattern for media uploads and it works, so here it is:

in settings.py

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')

in your model:

file = models.ImageField(upload_to='wiki')

say you upload "myfile.doc", it will end up in:

...your_project_path/media/wiki/myfile.doc

Then you just setup your nginx to serve from media folder.





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/29eff052-3181-4e0d-80fc-84fffe6ba029%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Darren Spruell

unread,
Jun 27, 2014, 1:13:29 AM6/27/14
to django...@googlegroups.com
On Thu, Jun 26, 2014 at 10:58 AM, Bobby Gulshan <bama...@gmail.com> wrote:
> No errors when hitting upload. But the image doesn't appear where I have
> indicated it ought to. Put an absolute path in MEDIA_ROOT and referenced the
> same in (upload_to) param ImageField. Not sure what I am missing.
>
> Model:
>
> class FileUploadHandler(models.Model):
> title = models.CharField(max_length=100)
> file =
> models.ImageField(upload_to='/Python27/Lib/site-packages/django/bin/mideastinfo/wiki/static/')

Your upload_to path looks like an absolute directory, which means that
Django will attempt to store the file in that subdirectory of the path
you've configured for MEDIA_ROOT.

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.storage

You want to do like Mario suggested and set 'upload_to' to a relative
path from the MEDIA_ROOT where you want images for that model stored.

--
Darren Spruell
phatb...@gmail.com




> View:
>
> from models import Article, Edit
> from forms import ArticleForm, EditForm
> from forms import *
> from PIL import Image
> from models import FileUploadHandler
>
> def image_upload(request):
> if request.method == 'POST':
> form = UploadImageForm(request.POST, request.FILES)
> if form.is_valid():
> FileUploadHandler(request.FILES['image'])
> return render_to_response('wiki/gallery.html')
> else:
> form = UploadImageForm()
> return render_to_response('wiki/gallery.html',
> RequestContext(request, {'form': form}))
>
Reply all
Reply to author
Forward
0 new messages