-1

I'm making a new django app where a user can upload images and then they can be displayed on a page. I have read the django staticfiles deployment docs a bunch and don't understand why they still dont work.

All my images are going to where they're supposed to be and in the admin in my models django has the right path that leads to the image but it just won't load properly.

my setup in settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = 'static' # live cdn such as AWS S3

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

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

my directories:

|- src
    |- myproject
        |- settings.py, views.py, urls.py etc
    |- myapp
        |- views.py urls.py models.py forms.py etc
    |- static (this folder is empty and appeared after running collectstatic
    |- templates
    |- db.sqlite3
    |- manage.py
|- static (this folder is OUTSIDE src)
    |- admin
        |- django admin stuff
    |- media
        |- img
            |- myimage.png

in models.py of myapp the imagefield upload_to = 'img/'

if i am in debug mode and i add the reccomended code to make it work:

if settings.DEBUG:
    from django.conf.urls.static import static

    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

everything works. this is only in debug mode though. I'm aware the docs say that this only works in debug mode but i tried it anyway. I'm trying everything.

I've been trying to fix this for ages but i can't get any help from anyone and django docs and other stackoverflow questions/answers have been of no help too.

I thank you for your help!

NOTE: this is a DIFFERENT question to my previous one, please don't close it again.trying to

Lumiobyte
  • 65
  • 1
  • 8
  • You claimed that _you had read the django docs 10000 times_ in [this question](https://stackoverflow.com/questions/57282125/django-2-2-staticfiles-do-not-work-in-development) too but obviously you haven't read it even once yet. It is clearly indicated in the docs: `This helper function works only in debug mode`. – Selcuk Aug 02 '19 at 00:52
  • @Selcuk i was going to delete that because i knew someone would mark it as dupe but stackoverflow said "don't delete answered questions". i have read that part and i know but i'm including everything ( i mean EVERYTHING) i know. – Lumiobyte Aug 02 '19 at 00:55
  • I am not suggesting that you should delete your other question. You should really read the documentation. See the duplicate question for your answer. – Selcuk Aug 02 '19 at 00:57

1 Answers1

-1
|- src
    |- myproject
        |- settings.py, views.py, urls.py etc
        |- static # This is where your static files should be...
    |- myapp
        |- views.py urls.py models.py forms.py etc
    |- static # This is where all your static files will be collected and served from
    |- templates
    |- db.sqlite3
    |- manage.py
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50