【发布时间】:2019-03-31 14:28:15
【问题描述】:
所以问题是 django 根本无法识别静态文件。无论我尝试过什么,它都不起作用。我尝试将全局搜索方式更改为 css 文件的直接 url,这似乎都不起作用。
我的项目结构如下:
这是我的代码:
DEBUG = True
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
from django.contrib import admin
from django.urls import path
from something.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', home),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Monkeys</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/file.css' %}"/>
</head>
可以在互联网上的任何主题中找到答案,所以我希望你能提供帮助。
【问题讨论】:
-
欢迎来到 StackOverflow!你跑
python manage.py collectstatic了吗?此外,问题是在调试模式还是实时模式(或两者)中出现? -
是的,应该如此
-
据我所知,您已经收集了静态文件。
-
请从渲染页面发布生成的文件 url,尝试手动打开它并发布它会返回的错误消息/http 状态。还有
print(STATIC_ROOT). -
显示此文件的完整路径。请回答第一条评论中的问题:您的情况是 DEBUG=False 还是 True?你是在 dev 还是 prod conf 上运行?