我无法从根目录下的statcfiles导入css和js文件



我尝试了整个互联网上建议的所有方法,并在这里停留了5天以上这里是我的settings.py

STATIC_URL = '/static/'
# Add these new lines
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
我有一个静态文件目录,我创建了
python manage.py collectstatic

我所有的css都在那个文件

这里是我的网址

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('accounts.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的应用程序的url

from django.urls import path
from accounts.views import *
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', SignUpView.as_view(), name='signup'),
path('otp/', OTPView.as_view(), name='otp'),
path('login/', LoginView.as_view(), name='login'),
path('home/<int:id>', HomeView.as_view(), name="home"),
path('logout/', UserLogout.as_view(), name="logout"),
]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是我的模板base。html我在其他模板

中继承了它
{% csrf_token %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/User_Authentication/staticfiles/admin/css/responsive.css">
</head>

我得到这个错误

Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:05:51] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211
Not Found: /User_Authentication/staticfiles/admin/css/responsive.css
[11/Jul/2022 12:06:31] "GET /User_Authentication/staticfiles/admin/css/responsive.css HTTP/1.1" 404 4211

为什么我仍然不能使用staticfiles

文档,静态文件

{% load static %}
{% csrf_token %}
{% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{% static 'admin/css/responsive.css' %}">
</head>

你需要先加载它,这三个的加载顺序可能要改变。

您可以运行应用程序,到页面上,检查并找出django url转换到什么。你基本上是要求django/jinja动态地将这个静态路径转换为相应的静态url,而不是硬编码它。

注意:每个使用"{% static '...' %}"的模板文件中都需要添加{% load static %}。只是将其添加到base.html或其他模板扩展的等效文件中是行不通的。

最新更新