为什么django静态文件没有加载
why the django static file not loading
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'authn',
]
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'first.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('',views.home,name="home"),
path('register/', views.registerPage, name="register"),
path('login/', views.loginPage, name="login"),
path('logout/', views.logoutUser, name="logout"),
]
html 我正在尝试处理的文件
<head>
<title>Nura Admin - Charts</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="shortcut icon" href= "{% static 'assets/images/atomic.png'%}">
<link href="{% static 'assets/css/bootstrap.min.css'%}" rel="stylesheet" type="text/css" />
<link href="{% static 'assets/font-awesome/css/all.css'%}" rel="stylesheet" type="text/css" />
<link href=" {%static 'assets/css/style.css' %}" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="{%static 'assets/plugins/chart.js/Chart.min.css'%}" />
</head>
文件位置
在 运行 服务器上获取
[29/Apr/2021 04:03:15] "GET / HTTP/1.1" 302 0
[29/Apr/2021 04:03:15] "GET /login/?next=/ HTTP/1.1" 200 21957
[29/Apr/2021 04:03:16] "GET /static/assets/css/bootstrap.min.css HTTP/1.1" 404 1710
[29/Apr/2021 04:03:16] "GET /static/assets/js/modernizr.min.js HTTP/1.1" 404 1704
[29/Apr/2021 04:03:16] "GET /static/assets/css/style.css HTTP/1.1" 404 1686
[29/Apr/2021 04:03:16] "GET /static/assets/js/jquery.min.js HTTP/1.1" 404 1695
[29/Apr/2021 04:03:16] "GET /static/assets/plugins/chart.js/Chart.min.css HTTP/1.1" 404 1737
[29/Apr/2021 04:03:16] "GET /static/assets/font-awesome/css/all.css HTTP/1.1" 404 1719
[29/Apr/2021 04:03:16] "GET /static/assets/js/moment.min.js HTTP/1.1" 404 1695
[29/Apr/2021 04:03:16] "GET /static/assets/js/bootstrap.min.js HTTP/1.1" 404 1704
[29/Apr/2021 04:03:16] "GET /static/assets/js/popper.min.js HTTP/1.1" 404 1695
[29/Apr/2021 04:03:16] "GET /static/assets/js/detect.js HTTP/1.1" 404 1683
[29/Apr/2021 04:03:16] "GET /static/assets/js/jquery.min.js HTTP/1.1" 404 1695
...
将此添加到 URLS.py 中“urlpatterns”变量的末尾:
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
所以它应该看起来像:
urlpatterns = [
path('',views.home,name="home"),
path('register/', views.registerPage, name="register"),
path('login/', views.loginPage, name="login"),
path('logout/', views.logoutUser, name="logout"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
然后确保您的 settings.py 中包含以下内容:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = "/static/"
STATIC_URL = '/static/'
这应该可以解决您的问题。
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'authn',
]
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'first.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls.static import static
from . import views
urlpatterns = [
path('',views.home,name="home"),
path('register/', views.registerPage, name="register"),
path('login/', views.loginPage, name="login"),
path('logout/', views.logoutUser, name="logout"),
]
html 我正在尝试处理的文件
<head>
<title>Nura Admin - Charts</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="shortcut icon" href= "{% static 'assets/images/atomic.png'%}">
<link href="{% static 'assets/css/bootstrap.min.css'%}" rel="stylesheet" type="text/css" />
<link href="{% static 'assets/font-awesome/css/all.css'%}" rel="stylesheet" type="text/css" />
<link href=" {%static 'assets/css/style.css' %}" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="{%static 'assets/plugins/chart.js/Chart.min.css'%}" />
</head>
文件位置
在 运行 服务器上获取
[29/Apr/2021 04:03:15] "GET / HTTP/1.1" 302 0
[29/Apr/2021 04:03:15] "GET /login/?next=/ HTTP/1.1" 200 21957
[29/Apr/2021 04:03:16] "GET /static/assets/css/bootstrap.min.css HTTP/1.1" 404 1710
[29/Apr/2021 04:03:16] "GET /static/assets/js/modernizr.min.js HTTP/1.1" 404 1704
[29/Apr/2021 04:03:16] "GET /static/assets/css/style.css HTTP/1.1" 404 1686
[29/Apr/2021 04:03:16] "GET /static/assets/js/jquery.min.js HTTP/1.1" 404 1695
[29/Apr/2021 04:03:16] "GET /static/assets/plugins/chart.js/Chart.min.css HTTP/1.1" 404 1737
[29/Apr/2021 04:03:16] "GET /static/assets/font-awesome/css/all.css HTTP/1.1" 404 1719
[29/Apr/2021 04:03:16] "GET /static/assets/js/moment.min.js HTTP/1.1" 404 1695
[29/Apr/2021 04:03:16] "GET /static/assets/js/bootstrap.min.js HTTP/1.1" 404 1704
[29/Apr/2021 04:03:16] "GET /static/assets/js/popper.min.js HTTP/1.1" 404 1695
[29/Apr/2021 04:03:16] "GET /static/assets/js/detect.js HTTP/1.1" 404 1683
[29/Apr/2021 04:03:16] "GET /static/assets/js/jquery.min.js HTTP/1.1" 404 1695
...
将此添加到 URLS.py 中“urlpatterns”变量的末尾:
+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
所以它应该看起来像:
urlpatterns = [
path('',views.home,name="home"),
path('register/', views.registerPage, name="register"),
path('login/', views.loginPage, name="login"),
path('logout/', views.logoutUser, name="logout"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
然后确保您的 settings.py 中包含以下内容:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = "/static/"
STATIC_URL = '/static/'
这应该可以解决您的问题。