Django 2.2 staticfiles do not work in development

可紊 提交于 2019-12-08 11:15:12

问题


I'm making a new django app where a user can upload images and then they can be displayed on a page. I've read the django docs 10000 times through and I still don't understand why my images aren't loading (DEBUG = True)

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/'

as i said, the images don't load on the page but to upload to static/media/img/image_name.png

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!


回答1:


Serving files uploaded by a user during development¶

During development, you can serve user-uploaded media files from MEDIA_ROOT using the django.views.static.serve() view.

This is not suitable for production use! For some common deployment strategies, see Deploying static files.

For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

https://docs.djangoproject.com/en/2.2/howto/static-files/




回答2:


STATICFILES_DIRS denotes where Django will look for your static files, this is where you should be putting you static files so that they will be served in development and be collected from by collectstatic when you deploy.

Looks like you have set this to src/static so this is where you should be putting your static files.

STATIC_ROOT is where your static assets will be collected to after you have run collectstatic

STATICFILES_FINDERS by default will look in STATICFILES_DIRS and every app/static directory

A typical layout is like this

project_root/
    project_name/
        settings.py
        urls.py
        static/ < This is usually STATICFILES_DIRS
    app_1/
        models.py
        views.py
        static/ < AppDirectoriesFinder will look here
    app_2/
        models.py
        views.py
        static/ < AppDirectoriesFinder will look here
    static/ < This is where assest will be collected by collectstatic



回答3:


I had the same issue, and in fact, I didn't specifed my static files in my nginx.conf.

If it can help you: sometimes we forget to check our basic configuration :D



来源:https://stackoverflow.com/questions/57282125/django-2-2-staticfiles-do-not-work-in-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!