It should be:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
At last! Referring here, I learned that the static files directory must be located within the directory of an individual app. I had the static directory in the project root. I moved it into an app directory and it is working now.,This whole project will be using these same static files. If someone knows, do I need to have a copy of them in every App's directory, or can all of my project's apps share a static directory? Thanks!,You are also missing a comma in the setting. Without the comma it is treated as a string not a tuple.,Which strategy would be best: saving a value as a field or just computing it on hand with a method
It should be:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you’re confused, the findstatic command can help show you which files are found.,On subsequent collectstatic runs (if STATIC_ROOT isn’t empty), files are copied only if they have a modified timestamp greater than the timestamp of the file in STATIC_ROOT. Therefore if you remove an application from INSTALLED_APPS, it’s a good idea to use the collectstatic --clear option in order to remove stale static files.,you’ve collected all your static files by using the collectstatic management command,Management Commands collectstatic Customizing the ignored pattern list findstatic runserver
from django.contrib.staticfiles
import storage
class MyStaticFilesStorage(storage.StaticFilesStorage):
def __init__(self, * args, ** kwargs):
kwargs['file_permissions_mode'] = 0o640
kwargs['directory_permissions_mode'] = 0o760
super().__init__( * args, ** kwargs)
$ python manage.py collectstatic--help
...\ > py manage.py collectstatic--help
from django.contrib.staticfiles.apps
import StaticFilesConfig
class MyStaticFilesConfig(StaticFilesConfig):
ignore_patterns = [...] # your custom ignore list
$ python manage.py findstatic css / base.css admin / js / core.js
Found 'css/base.css'
here:
/home/special.polls.com / core / static / css / base.css /
home / polls.com / core / static / css / base.css
Found 'admin/js/core.js'
here:
/home/polls.com / src / django / contrib / admin / media / js / core.js
...\ > py manage.py findstatic css\ base.css admin\ js\ core.js
Found 'css/base.css'
here:
/home/special.polls.com / core / static / css / base.css /
home / polls.com / core / static / css / base.css
Found 'admin/js/core.js'
here:
/home/polls.com / src / django / contrib / admin / media / js / core.js
django.contrib.staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.,Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you’re confused, the findstatic command can help show you which files are found.,Overrides the core runserver command if the staticfiles app is installed and adds automatic serving of static files and the following new options.,you’ve collected all your static files by using the collectstatic management command
$ python manage.py collectstatic--help
$ python manage.py findstatic css / base.css admin / js / core.js Found 'css/base.css'
here: /home/special.polls.com / core / static / css / base.css / home / polls.com / core / static / css / base.css Found 'admin/js/core.js'
here: /home/polls.com / src / django / contrib / admin / media / js / core.js
$ python manage.py findstatic css / base.css--first Found 'css/base.css'
here: /home/special.polls.com / core / static / css / base.css
$ python manage.py findstatic css / base.css--verbosity 0 / home / special.polls.com / core / static / css / base.css / home / polls.com / core / static / css / base.css
django - admin.py runserver--nostatic
django - admin.py runserver--insecure
I'm not sure as to why Django cannot find the .css file when I have it clearly stated in the above files. No other StackOverflow questions/answers have helped me either., As a part of the Django tutorial series, we will learn to add CSS and other static files in Django. We will also see how you can debug if your static files are not properly loading in the Django template. , There are multiple ways of debugging it. Run manage.py with find static option. If Django project able tp locate the CSS, it will return path to your CSS file. If the CSS static file not found, it will throw an err as “No matching file found” , The current path, static/practice1/style.css, didn’t match any of these. You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page."
myproject / | --myproject / | | --boards / | | --myproject / | | --templates / | | --static / | | + --css / | | + --bootstrap.min.css < --here | + --manage.py
-your_django_app--static-- - your_django_app-- --css-- -- - bootstrap.min.css
However, if I override PIPELINE_ENABLED to be True and then run ./manage.py findstatic css/generated.css it returns:,I’m not sure if this is a bug or intended behavior but I couldn’t find anything that specifically answered this question. I’m trying to serve the compiled staticfiles through runserver for debugging. When DEBUG = True and PIPELINE_ENABLED is its default value, I can run ./manage.py findstatic css/generated.css and it will return the correct path.,If I have DEBUG True and PIPELINE_ENABLED True I get a systematic 404. Digging into it it seems that Django expects the PipelineFinder not to return [] as it is configured to do when piping is off.,If DEBUG=False and I have previously run collectstatic I have no problems (compiled versions are generated and served). If I don’t override the PIPELINE_ENABLED, I also have no problems – but individual source files are served rather than the compiled version.
However, if I override PIPELINE_ENABLED
to be True
and then run ./manage.py findstatic css/generated.css
it returns:
No matching file found
for 'css/generated.css'.
After this change, it basically requires that the files first are copied into STATIC_ROOT, whereas before they could just be sourced from the original directories. The only way I can get DEBUG=True to work without first running collectstatic
is like this:
DEBUG = True PIPELINE = { '' PIPELINE_ENABLED ": False, "PIPELINE_COLLECTOR_ENABLED": True }
I have figured out another (probably ugly) hack, but it works in only 4 lines of code:
from django.contrib.staticfiles.finders
import BaseStorageFinder
from django.contrib.staticfiles.storage
import staticfiles_storage
class FixedPipelineFinder(BaseStorageFinder):
storage = staticfiles_storage
Then use this FixedPipelineFinder
in your settings (I put the finder into a package hacks.pipeline
):
STATICFILES_FINDERS = (
...
'hacks.pipeline.FixedPipelineFinder',
)
I was running into the same issue. Looking through the source of PipelineFinder I realized we’re probably not really meant to use it as it seems to do a NO-OP:
class PipelineFinder(BaseStorageFinder):
storage = staticfiles_storage
def find(self, path, all = False):
if not settings.PIPELINE_ENABLED:
return super(PipelineFinder, self).find(path, all)
else:
return []
def list(self, ignore_patterns):
return []
The ManifestFinder does the following:
class ManifestFinder(BaseFinder): def find(self, path, all = False): "" " Looks for files in PIPELINE.STYLESHEETS and PIPELINE.JAVASCRIPT "" " matches = [] for elem in chain(settings.STYLESHEETS.values(), settings.JAVASCRIPT.values()): if elem['output_filename'] == path: match = safe_join(settings.PIPELINE_ROOT, path) if not all: return match matches.append(match) return matches def list(self, * args): return []
不久前,我尝试在 Django 中使用“collectstatic”来提供我的静态文件,但失败了。现在我终于尝试让我的静态文件在我的开发环境中正确服务。我无法弄清楚出了什么问题。当我跑 python manage.py findstatic images/add.png控制台返回:
不久前,我尝试在 Django 中使用“collectstatic”来提供我的静态文件,但失败了。现在我终于尝试让我的静态文件在我的开发环境中正确服务。我无法弄清楚出了什么问题。
当我跑 python manage.py findstatic images/add.png
控制台返回:
base path: C: \Projects\ AlmondKing\ AlmondKing
static files dirs: C: \Projects\ AlmondKing\ AlmondKing\ static
No matching file found
for 'images/add.png'.
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'AlmondKing.InventoryLogs',
'AlmondKing.FinancialLogs',
'AlmondKing.AKGenius',
)
STATIC_URL = '/static/'
STATIC_FILES_DIRS = (
os.path.join((BASE_DIR), "static")
)
print("base path:", BASE_DIR)
print("static files dirs:", STATIC_FILES_DIRS)
设置为 STATICFILES_DIRS
,不是 STATIC_FILES_DIRS
.
您还缺少设置中的逗号。没有逗号,它被视为字符串而不是元组。
它应该是:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)