Django unable to load static files

后端 未结 3 773
粉色の甜心
粉色の甜心 2021-01-27 06:09

Completely new to django.

In my settings.py file, I have:

STATIC_URL = \'/static/\'
INSTALLED_APPS = [
    \'django.contrib.admin\',
    \'django.contri         


        
相关标签:
3条回答
  • 2021-01-27 06:24

    Add these in your settings.py:

    STATIC_URL = '/static/'
    
    MEDIA_URL = '/media/'
    
    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
    
    VENV_PATH = os.path.dirname(BASE_DIR)
    
    STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
    
    MEDIA_ROOT = os.path.join(VENV_PATH, 'media_root')
    
    0 讨论(0)
  • 2021-01-27 06:24

    Here is settings.py code:

    STATIC_URL = "/static/"
    STATICFILES_DIR = [
    os.path.join(BASE_DIR, "static")]
    LOGIN_URL = "account:login"
    LOGIN_REDIRECT_URL = "public:index"
    LOGOUT_REDIRECT_URL = "public:index"
    
    
     INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'website.apps.accounts',]
    
    0 讨论(0)
  • 2021-01-27 06:39

    You have to tell Django where to find the static files. It is done by setting the STATICFILES_DIRS on your settings.py module.

    Usually we do something like this, using the BASE_DIR:

    settings.py

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

    Some extra info on static settings:

    It is a little bit confusing in the beginning, because Django has three similar configuration:

    • STATIC_URL
    • STATIC_ROOT
    • STATICFILES_DIRS

    Basically, the STATIC_URL will be used to generate a URL to serve your static assets. It could be a relative URL like you had:

    STATIC_URL='/static/'
    
    {% static 'styles/custom.css' %} => /static/styles/custom.css
    

    It could be a full URL:

    STATIC_URL='https://static.yourdomain.com/'
    
    {% static 'styles/custom.css' %} => https://static.yourdomain.com/styles/custom.css
    

    Now, the STATICFILES_DIRS is where Django will find your static files. It's usually within the project folder. BUT! This is not from where Django will serve your static assets to the client. It's like, where is the source of your static files.

    That's because Django doesn't really serve this kind of files (.js, .css, .jpg). Well, not by default anyway. You could use a third-party app like WhiteNoise.

    And that's also why we have the STATIC_ROOT, which is to tell Django where to copy those files from the STATICFILES_DIRS so someone else can serve it for me. This someone else is usually Apache or NGINX.

    We only use the STATIC_ROOT for deployment.

    When we deploy a Django application to a productions server, we usually run the command python manage.py collectstatic. And that's where Django make a copy of the files from STATICFILES_DIRS to the STATIC_ROOT.

    But during the development phase you don't really have to care about this much detail, because while DEBUG=True, Django will make things easier for you and serve the static files.

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