简体   繁体   中英

STATIC_ROOT setup in Django 1.8

I was tried n I can't set-up as per official documents... I am attaching IMG here, pls give me suggestions, Where is the problem. enter image description here

Or, give me simple steps for it with dictionary tree structure.

Thank you.

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root', 'static')


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    # '/var/www/static/',
)

STATIC_ROOT = 'os.path.join(BASE_DIR, 'static_root', 'static') can't work.

Try this :

# define your base directory
# It will be `absolute/path/to/demo3`
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
# define where your static files will be collected
# It will be `absolute/path/to/demo3/static`
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# keep it empty for the moment
STATICFILES_DIRS = (
)

You have to understand STATICFILES_DIRS role :

Your project will probably also have static assets that aren't tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files.

I recommend you to read carefully Django docs : Managing static files

STATIC_ROOT should be without quotes:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Also, the STATIC_ROOT folder shouldn't be named the same as the STATICFILES_DIR folder, so you should name it something like staticfiles instead:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')

The error is clear in stating the STATIC_ROOT is not a filesystem path. Django requires that STATIC_ROOT be an absolute path to a folder location on the machine. This error likely means that your resulting STATIC_ROOT is a partial or relative path. What is the value of BASE_DIR ? After the line that sets STATIC_ROOT and a print(STATIC_ROOT) to see what value it is.

Try setting BASE_DIR as follows:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Other Errors

On the line

STATIC_ROOT = 'os.path.join(BASE_DIR, 'static')'

You have the value surrounded by single quotes. os.path.join() is a function call. Remove the quotes and change the line like this:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Also, STATIC_ROOT cannot be included in the list of files in STATICFILES_DIRS. Consider setting the STATIC_ROOT folder to:

STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')

STATIC_ROOT = 'os.path.join(BASE_DIR, 'static')' is a string which is wrong.

it should be STATIC_ROOT = os.path.join(BASE_DIR, 'static')

when creating the project with latest version of python and django in settings.py file by default from pathlib import Path Statement is there.So in that case no need to use os.path .

from pathlib import Path # Import First

STATIC_ROOT = Path.joinpath(BASE_DIR, 'static_collected')

我添加了一行代码,它对我有用,我希望将来不会痛:)

import os

Remember to create a static folder in your root directory and also create the static folder in your project folder. Then your file structure would be:

demo3
    /demo3/staticfiles # collection folder for all static files
    /static            # root static folder
    /userForms/static  # your app static files
# OR
demo3
    /demo3
    /static_files     # collection folder for all static files
    /static           # root static folder
    /userForms/static # your app static files
    

Add this to your settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'demo3', 'staticfiles')
OR depending on where you want the collection folder
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

Run python manage.py collectstatic

Then it would bundle all static files in project staticfiles OR root static_files folder, depending on where you want them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM