简体   繁体   中英

blank django project serving static CSS file

I have created a new django project using the command

django-admin startproject mysite

this created the basic django directory structure. I then added a directory called static under /mysite/mysite/static and then created style.css within this.

PS C:\Users\Danny\Desktop\django-test> tree /f
Folder PATH listing
Volume serial number is B069-3254
C:.
└───mysite
    │   db.sqlite3
    │   manage.py
    │
    └───mysite
        │   settings.py
        │   settings.pyc
        │   urls.py
        │   urls.pyc
        │   wsgi.py
        │   wsgi.pyc
        │   __init__.py
        │   __init__.pyc
        │
        └───static
                style.css

settings.py (part of)

STATIC_ROOT = 'C:/Users/Danny/Desktop/django-test/mysite/mysite/static'
STATIC_URL = '/static/'

I then run the server using

python manage.py runserver

and navigate to http://127.0.0.1:8000/static/style.css and expect to see my stylesheet, however I am presented with 404 Page Not Found

I have read the documentation and I am struggling to see any steps I have missed? I just want it to work locally, and I am not bothered about a production environment at the moment.

Ok, on your yourprojectfolder/settings.py you have to set up a couple of variables:

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

STATIC_ROOT = os.path.join(PROJECT_FOLDER, "static")                                  
STATIC_URL = '/static/'                                                                                                                

STATICFILES_DIRS = (os.path.join(BASE_DIR, "staticfiles"), )              

STATICFILES_FINDERS = (                                                         
    'django.contrib.staticfiles.finders.FileSystemFinder',                      
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',                  
)  

And basically, that tells Django to look for your files in the folder staticfiles/ on the same directory as manage.py . So put your .css files there and that's pretty much it.

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