目标:我有一些前端页面(包括HTML和资产(图像、CSS、JS))。现在只想将这些页面移动到一个新的Django项目
问题: 在浏览器中,未应用所有静态文件,但已成功加载。 从chrome开发工具中,我可以看到一切都是202。此外,所有静态文件都是正确的
类似问题: CSS Loaded but not being applied
Static Files not getting loaded in Django Python
Django staic and admin static css files loaded but not applied to page
Django: CSS and Images (Static Files) loaded successfully but not applied
Django Static Files - CSS File Won't Load
它们都不适合我
代码详细信息:
Python 3.7.6 Django 3.1.7
项目树:
~/SETH/SETH_A$ tree -L 1
.
├── A_WEB
├── db.sqlite3
├── facial_simple
├── __init__.py
├── manage.py
├── node_modules
├── open
├── package.json
├── SETH_A
├── static
├── staticfiles
└── templates
静态目录(确保权限足够):
~/SETH/SETH_A$ ls static/assets
css demo img js test
~/SETH/SETH_A$ ls -l static/assets
total 20
drwxrwxrwx 2 kevin kevin 4096 Feb 9 17:46 css
drwxrwxrwx 2 kevin kevin 4096 Feb 9 17:46 demo
drwxrwxrwx 3 kevin kevin 4096 Feb 9 17:46 img
drwxrwxrwx 4 kevin kevin 4096 Feb 9 17:46 js
-rwxrwxrwx 1 kevin kevin 6 Feb 28 10:13 test
如何在模板中调用静态文件:
<link href="{% static 'assets/css/material-dashboard.min.css' %}" />
我尝试的是:
添加到settings.py:已安装的应用程序:
'django.contrib.staticfiles',
'A_WEB',
'SETH_A'
同样在settings.py中(注意,我也知道STATICFILES_DIRS用于开发,STATIC_ROOT用于生产。我还尝试对STATIC_ROOT进行注释,反之亦然):
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'staticfiles'),
)
STATICFILES_FINDERS = [
'django_node_assets.finders.NodeModulesFinder',
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
在主应用程序URL(SETH_A/url.py)中:
urlpatterns = [
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
我还尝试在另一个应用程序中添加该静态URL
结果: 与问题相同,我可以访问静态文件,但不应用于页面
我很好奇,所以我手动将django.views.static.py中的静态视图添加到我自己的视图中:
SETH_A/SETH_A/views.py(注意,我添加了一些print(),它们工作得很好):
def static_serve(request, path, document_root=None, show_indexes=False):
print("STATIC SERVE ACCESSED")
path = posixpath.normpath(path).lstrip('/')
fullpath = Path(safe_join(document_root, path))
if fullpath.is_dir():
if show_indexes:
return directory_index(path, fullpath)
print("ERROR 1")
raise Http404(_("Directory indexes are not allowed here."))
if not fullpath.exists():
print("ERROR 2")
raise Http404(_('“%(path)s” does not exist') % {'path': fullpath})
# Respect the If-Modified-Since header.
statobj = fullpath.stat()
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj.st_mtime, statobj.st_size):
return HttpResponseNotModified()
content_type, encoding = mimetypes.guess_type(str(fullpath))
content_type = content_type or 'application/octet-stream'
response = FileResponse(fullpath.open('rb'), content_type=content_type)
response["Last-Modified"] = http_date(statobj.st_mtime)
if encoding:
response["Content-Encoding"] = encoding
return response
正在从已安装的_APPS settings.py中删除'django.contrib.staticfiles'
main url.py:
urlpatterns = [
#Another URLs
re_path(r'^static/(?P<path>.*)$', static_serve, {
'document_root': settings.STATIC_ROOT,
}),
]
结果: 与问题相同,我可以访问静态文件,但不应用于页面
我还试着重新启动我的电脑,删除所有的pycache目录,在一个匿名窗口中打开它们,仍然一样
我对Django并不陌生,但这个问题对我来说是新的,我已经不知道了。任何帮助都将不胜感激:)
目前没有回答
相关问题 更多 >
编程相关推荐