Django - Static file not found

前端 未结 13 770
旧巷少年郎
旧巷少年郎 2020-11-30 20:46

I\'ve seen several posts for this issue but didn\'t found my solution.

I\'m trying to serve static files within my Django 1.3 development environment.

Here a

相关标签:
13条回答
  • 2020-11-30 21:18

    If your static URL is correct but still:

    Not found: /static/css/main.css

    Perhaps your WSGI problem.

    ➡ Config WSGI serves both development env and production env

    ==========================project/project/wsgi.py==========================
    
    import os
    from django.conf import settings
    from django.contrib.staticfiles.handlers import StaticFilesHandler
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
    if settings.DEBUG:
        application = StaticFilesHandler(get_wsgi_application())
    else:
        application = get_wsgi_application()
    
    0 讨论(0)
  • 2020-11-30 21:18
    1. You can remove the STATIC_ROOT line
    2. Or you can create another static folder in different directory. For suppose the directory is: project\static Now update:
        STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'project/static/')
        ]
        STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    

    Whatever you do the main point is STATICFILES_DIRS and STATIC_ROOT should not contain same directory.

    I know it's been a long time but hope the new buddies can get help from it

    0 讨论(0)
  • 2020-11-30 21:19

    I confused STATIC_ROOT and STATICFILES_DIRS

    Actually I was not really understanding the utility of STATIC_ROOT. I thought that it was the directory on which I have to put my common files. This directory is used for the production, this is the directory on which static files will be put (collected) by collectstatic.

    STATICFILES_DIRS is the one that I need.

    Since I'm in a development environment, the solution for me is to not use STATIC_ROOT (or to specify another path) and set my common files directory in STATICFILES_DIRS:

    #STATIC_ROOT = (os.path.join(SITE_ROOT, 'static_files/'))
    import os
    SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
    STATICFILES_DIRS = (
      os.path.join(SITE_ROOT, 'static/'),
    )
    

    Also don't forget to from django.conf import settings

    0 讨论(0)
  • 2020-11-30 21:19

    Another error can be not having your app listed in the INSTALLED_APPS listing like:

    INSTALLED_APPS = [
        # ...
        'your_app',
    ]
    

    Without having it in, you can face problems like not detecting your static files, basically all the files involving your app. Even though it can be correct as suggested in the correct answer by using:

    STATICFILES_DIRS = (adding/path/of/your/app)
    

    Can be one of the errors and should be reviewed if getting this error.

    0 讨论(0)
  • 2020-11-30 21:19

    STATICFILES_DIRS is used in development and STATIC_ROOT in production,

    STATICFILES_DIRS and STATIC_ROOT should not have same folder name,

    If you need to use the exact same static folder in development and production, try this method

    include this in settings.py

    import socket
    
    HOSTNAME = socket.gethostname()
    
    # if hostname same as production url name use STATIC_ROOT 
    if HOSTNAME == 'www.example.com':
        STATIC_ROOT = os.path.join(BASE_DIR, "static/")
    
    else:
        STATICFILES_DIRS = [
                os.path.join(BASE_DIR, 'static/'),
            ]
    
    0 讨论(0)
  • 2020-11-30 21:23

    {'document_root', settings.STATIC_ROOT} needs to be {'document_root': settings.STATIC_ROOT}

    or you'll get an error like dictionary update sequence element #0 has length 6; 2 is required

    0 讨论(0)
提交回复
热议问题