Thursday, June 26, 2014

Re: Django image upload not saving

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.





On 27 June 2014 03:58, Bobby Gulshan <bamabacho@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/')

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}))

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAHqTbjmy4pSevANwSRu9pTjWDs5Cosu%3DZj1jxVCo3L5Unn7BOw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment