我无法访问&#c;'和' js'文件放在'静态'根文件夹下的文件夹。日志中显示以下错误 - [11 / May / 2018 11:07:33]" GET /static/home.css HTTP / 1.1" 404 1648
下面的是settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
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/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
我的html模板:
<head>
<title>HomePage</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href={% static '/home.css' %}>
<script type="text/javascript" src={% static '/home.js'%}></script>
</head>
urls.py
urlpatterns = [
.
.
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
当我尝试通过输入&#39; http://127.0.0.1:8000/static/home.css&#39;手动访问css时在浏览器中我得到了
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/home.css
Raised by: django.views.static.serve
'home.css' could not be found
另外,我在考虑如何在整个应用程序中使用相同的CSS? Aand,如果我直接在我的html模板中添加css和js怎么办?我在生产中部署时会出现问题吗?
答案 0 :(得分:0)
如果资源不在这里你需要收集静态:
资源需要在app/static
python manage.py collectstatic
并且不要使用{% static '/home.css' %}
但{% static 'home.css' %}