我有一个Django
应用程序,我无法加载静态文件。我的模板已完全加载,但无法加载CSS和JS文件。这是我的文件:
zeinab@ZiZi:~/Desktop/MyProject$ tree
.
├── __init__.py
├── manage.py
├── myapp
│ ├── __init__.py
│ ├── lib
│ │ ├── decorators.py
│ │ ├── dorsa_forms.py
│ │ ├── __init__.py
│ │ └── utils.py
│ ├── log
│ ├── media
│ │ └── admin
│ │ └── background_login
│ │ └── bg1.jpg
│ ├── settings.py
│ ├── static
│ │ ├── calendar.js
│ │ └── inbox.css
│ ├── templates
│ │ ├── 404.html
│ │ ├── base.html
│ │ └── index.html
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
└── requirements.txt
8 directories, 18 files
当我runserver
时,我会为每个CSS和JS文件获得"GET /static/file/url HTTP/1.1" 404 1265
。这是 MyProject / manage.py :
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
这是 MyProject / myapp / settings.py :
import os
from django.contrib.messages import constants as messages
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'sorl.thumbnail',
'myapp.lib',
)
MIDDLEWARE_CLASSES = (
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
SITE_ID = 1
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
ROOT_URLCONF = 'myapp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
],
},
},
]
WSGI_APPLICATION = 'myapp.wsgi.application'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = '/myapp/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
LOG_PATH = os.path.join(BASE_DIR, 'log')
DEBUG = True
这是 MyProject / myapp / urls.py :
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from .views import default
urlpatterns = [
url(r'^$', default, name='default'),
url(r'^admin/', admin.site.urls),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是 MyProject / myapp / wsgi.py :
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
application = get_wsgi_application()
编辑1:
我已将 MyProject / myapp / urls.py 更改为以下内容:
from django.conf import settings
from django.conf.urls import include, url
from django.views import static
from django.contrib import admin
from .views import default
urlpatterns = [
url(r'^$', default, name='default'),
url(r'^admin/', admin.site.urls),
url(r'^public/(?P<path>.*)$', static.serve, {
'document_root': settings.MEDIA_ROOT
}, name='url_public'),
]
我最终得到了相同的结果。
编辑2:
我还尝试使用urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
代替urlpatterns += staticfiles_urlpatterns()
,然后尝试collectstatic
; collectstatic
已成功完成,但仍会打开网址返回"GET /static/file/url HTTP/1.1" 404 1265
。
答案 0 :(得分:1)
在你的urls.py
中,你忘了说如何“计算”静态路径文件。
而不是:
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
试试这个:
from django.views import static
urlpatterns = [
# [...]
url(r'^public/(?P<path>.*)$', static.serve, {
'document_root': settings.MEDIA_ROOT
}, name='url_public'),
]
并在您的模板文件中(您没有提供示例),请尝试:
{% load static %}
<h1>{% static 'calendar.js' %}</h1>
不要忘记使用PyCharm,因为它对这些事情有很大的帮助(它识别路径,你将在模板文件中获得自动完成功能(Ctrl + Space))。
答案 1 :(得分:1)
替换
urlpatterns += staticfiles_urlpatterns()
通过
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
staticfiles_urlpatterns()
仅用于调试以获得静态文件的显示视图。正如https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/所述,绝不应该在生产中使用。
注意:STATIC_ROOT
&amp; MEDIA_ROOT
仅在您收集静态时使用collectstatic
manage.py
。它指定存储静态和媒体文件的绝对路径(通常为/var/www/myapp/static
和/var/www/myapp/media
)