How to save images posted by application users

41 views
Skip to first unread message

Ram

unread,
Jul 16, 2020, 1:08:08 AM7/16/20
to django...@googlegroups.com
Hi,

We need to save images posted by our application users and the images will be displayed in application UI once they are saved successfully. We are trying with these parameters in our settings.py but they are not working and our OS platform is Windows 10 OS. We know file paths are different in Windows OS when compared to non Windows OS platforms. Did anyone use Window OS in this community and able to save image successfully?

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'


MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Another challenge that we have is  that the above question is related to development, becase we use Windows laptop for our application evelopment, but once the code is integrated we need to deploy the code in our hosting server that is running with Linux OS. So we are not sure how to handle the case of multi OS platforms?

I'm sure many people could have resolved this in their applications. I appreciate if you can share suitabe recommendations?

Best regards
 

Mottaz Hegaze

unread,
Jul 16, 2020, 3:18:44 AM7/16/20
to Django users
You need a model with ImageField or FileField

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BOi5F095b3_rr2qv02qrneLKs%2BySO1AjXedUv%3D653LY3cfDnw%40mail.gmail.com.

Ram

unread,
Jul 17, 2020, 1:08:40 AM7/17/20
to django...@googlegroups.com
Hi Mottaz,

Thank you for your suggestion. We already tried a model with ImageField but not luck yet. Here is our code snippet

image = models.ImageField(upload_to='media',
null=True, blank=True) image2 = models.ImageField(upload_to='media',
null=True, blank=True) image3 = models.ImageField(upload_to='media',
null=True, blank=True)

settings.py has the related settings

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
and also we have urls patterns like this in urls.py

from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
....
....
if settings.DEBUG is True:
         urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

 ran the following commands after the above changes

1. makemigrations
2. migrate
3. collectstatic
4. runserver

Then we posted an Ad in front end of the application with images and we getting this error and images did not show up in front end,

Not Found: /media/jpg1.jpg
[17/Jul/2020 10:27:27] "GET /media/bluejpg.jpg HTTP/1.1" 404 1816
[17/Jul/2020 10:27:27] "GET /media/jpg1.jpg HTTP/1.1" 404 1807
[17/Jul/2020 10:27:27] "GET /static/css/images/notice.png HTTP/1.1" 404 1689

 Thanks,
~Ram


Exactly musty

unread,
Jul 17, 2020, 1:18:23 AM7/17/20
to Django users
I also had the same problem long time ago I remember i felt like hanging myself,here is what I did, i deleted the media folder,created by django, try this if it will work

Mottaz Hegaze

unread,
Jul 17, 2020, 5:31:40 AM7/17/20
to Django users
Try to use subfolder in media, upload_to='/images'



On Fri, 17 Jul 2020, 7:18 am Exactly musty, <exactmu...@gmail.com> wrote:
I also had the same problem long time ago I remember i felt like hanging myself,here is what I did, i deleted the media folder,created by django, try this if it will work

--
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.

Liu Zheng

unread,
Jul 18, 2020, 10:28:05 AM7/18/20
to Django users
Hi. Here's a very nice video tutorial on how to handle images with media. https://www.youtube.com/watch?v=FdVuKt_iuSI&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p&index=8

Also to your question on how to deploy it in production, check this video. https://www.youtube.com/watch?v=Sa_kQheCnds&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p&index=13
Reply all
Reply to author
Forward
0 new messages