Python Django: You're using the staticfiles app without having set the STATIC_ROOT setting

前端 未结 3 1239
盖世英雄少女心
盖世英雄少女心 2021-02-02 07:53

I\'m trying to deploy my Django application to the web, but I get the following error:

You\'re using the staticfiles app without having set the STATIC_ROO

相关标签:
3条回答
  • 2021-02-02 08:37

    If you are using Django 2.2 or greater, your settings file already has a line similar to this:

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    

    Therefore you can easily set static like so:

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
    0 讨论(0)
  • 2021-02-02 08:39

    What is the production.py file? How do you import your settings?

    Depending on how you got this error (serving django through a wsgi server or on the command line), check for manage.py or wsgi.py to see what is the name of the default settings file.

    If you want to manuallly set the settings to use, use something like this:

    ./manage.py --settings=production
    

    Where production is any python module.

    Moreover, your settings file should not import anything django related. If you want to split your settings for different environments, use something like this.

    A file settings/base.py

    # All settings common to all environments
    PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
    

    Files like settings/local.py, settings/production.py

    # Production settings
    from settings.base import *
    DEBUG = False
    DATABASES = …
    
    0 讨论(0)
  • 2021-02-02 08:40

    Set the STATIC_ROOT setting to the directory from which you’d like to serve these files, for example:

    STATIC_ROOT = "/var/www/example.com/static/"

    The settings you are using are for development. Check the Django docs for more information here

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