我知道这个问题被多次询问过,但我发现和尝试的答案都没有帮助我.
这些是我的静态文件设置:
STATIC_ROOT = os.path.abspath(SETTINGS_PATH+'/staticfiles/')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/staticfiles/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(__file__), 'static'),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
Run Code Online (Sandbox Code Playgroud)
在myapp/urls.py中: …
当我运行服务器时,django管理员缺少css.网络控制台说没有加载样式表,因为它的MIME类型"application/x-css"不是"text/css".这是我的settings.py文件
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'C:/djangorevision/mysite/sqlite3.db', database
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '',
'PORT': '',
}
}
ALLOWED_HOSTS = []
TIME_ZONE = 'America/Chicago'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the …Run Code Online (Sandbox Code Playgroud) Django 3.0.8
蟒蛇 3.7.x
我有一个带有一些应用程序的 Django 项目。我正在尝试为 400、403、404、500 错误创建一些“默认”错误页面。我已经完成并显示了适当的模板 - 但没有任何样式或 JS。
在 404 错误页面中,我尝试从其中一个应用程序链接到 CSS,以便应用正确的样式 - 但在控制台中,我看到此错误:
Refused to apply style from 'http://127.0.0.1:8000/static/launcher/dist/css/launcher.css' because of its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Run Code Online (Sandbox Code Playgroud)
但是,该文件存在于那里。
这个特定的 CSS 文件位于两个位置:在 app 目录中,它也位于 STATIC_ROOT 中,因为我运行了该python manage.py collectstatic命令。
STATIC_URL 设置为 /static/
CSS 文件位于:
project_dir/launcher/static/launcher/dist/css/launcher.css
project_dir/static/launcher/dist/css/launcher.css
我的 404 模板位于:
project_dir/templates/404.html
我的 CSS 链接如下所示:
<link rel="stylesheet" type="text/css" href="{% static 'launcher/dist/css/launcher.css' %}" />
我的项目网址如下所示:
urlpatterns = …Run Code Online (Sandbox Code Playgroud) 我正在使用 Django 1.10 版。项目在 Debug = True 上工作正常,但是当我将其设置为 False 时则不然。Django 就是找不到静态文件。
我的 Django 设置如下所示:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'master',
'update',
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
Run Code Online (Sandbox Code Playgroud)
和 urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^master/', include('master.urls')),
url(r'^update/', include('update.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Run Code Online (Sandbox Code Playgroud)
uwsgi.ini 文件
[uwsgi]
chdir = %v …Run Code Online (Sandbox Code Playgroud) 当前错误(调试=假)
"[16/Jan/2018 15:18:42] "GET /static/style.css HTTP/1.1" 404 90"
网站已加载但格式已损坏,因为未加载 CSS 文件
记录:
在 CMD 提示上它说
"[16/Jan/2018 15:49:05] "GET /beginners/ HTTP/1.1" 200 3760
[16/Jan/2018 15:49:05] "GET /static/style.css HTTP/1.1" 404 90
"
Run Code Online (Sandbox Code Playgroud)
我不确定为什么这不起作用:我的 style.css 位于我的静态文件夹中,并且静态文件夹与 manage.py 是同一个文件夹
当我设置 Debug = True 时,我重新加载页面并且它工作正常 - 我的静态文件夹处于活动状态并且我没有收到静态错误:
[16/Jan/2018 15:58:11] "GET /beginners/? HTTP/1.1" 200 3759
[16/Jan/2018 15:58:11] "GET /static/style.css HTTP/1.1" 200 5014
Run Code Online (Sandbox Code Playgroud)
请帮忙!!
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1']
BASE_DIR …Run Code Online (Sandbox Code Playgroud) 使用前端 react 开发 django 应用程序,并使用 django 提供 react 构建文件。项目结构是因为在 settings.py 中一切正常
DEBUG=True
Run Code Online (Sandbox Code Playgroud)
但是,当我将其更改DEBUG=False
为控制台中的静态文件时,出现以下错误
来自“http://localhost:8000/static/js/2.285a2b2f.chunk.js”的资源由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。
来自“http://localhost:8000/static/css/main.dde91409.chunk.css”的资源由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。
由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff),来自“http://localhost:8000/static/js/main.eade1a0b.chunk.js”的资源被阻止。
由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff),来自“http://localhost:8000/static/css/2.03372da4.chunk.css”的资源被阻止。
来自“http://localhost:8000/static/js/2.285a2b2f.chunk.js”的资源由于 MIME 类型(“text/html”)不匹配(X-Content-Type-Options:nosniff)而被阻止。
在我的 setting.py 文件中,我还设置了以下内容
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'frontend')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'frontend', "build", "static"), # update the STATICFILES_DIRS
)
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
Run Code Online (Sandbox Code Playgroud)
一切正常,除非 DJango …
我的 CSS 没有加载,然后我将 DEBUG 更改为 True,它起作用了。
我的问题是,如果我有一个实际的服务器并且 DEBUG = False,CSS 将不会加载。
DEBUG 设置与静态文件有什么关系?无论 DEBUG 是 True 还是 False,它都应该加载静态文件。
django ×7
python ×5
css ×1
django-1.4 ×1
file ×1
html ×1
python-2.7 ×1
reactjs ×1
server ×1
static ×1
static-files ×1