django html模板找不到静态的css和js文件 [英] django html template can't find static css and js files
问题描述
我有一个django项目,其结构如下:
I have a django project that structure is like this:
>vira
>vira
-__init.py
-settings.py
-urls.py
-wsgi.py
>vira_app
>migrations
>template
-index.html
>static
>vira_app
>assets
>css
>js
>vendor
>aos
>bootstrap
>bootstrap-icons
>isotope-layout
>swiper
-__init__.py
-admin.py
-apps.py
-models.py
-tests.py
-urls.py
-views.py
-db.sqlite3
-manage.py
我用过引导程序.index.html如下所示:
I have used bootstrap. index.html is like below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{% load static %}
<link href="{% static 'vira_app/assets/vendor/aos/aos.css' %}" rel="stylesheet">
<link href="{% static 'vira_app/assets/vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<link href="{% static 'vira_app/assets/vendor/bootstrap-icons/bootstrap-icons.css' %}" rel="stylesheet">
<link href="{% static 'vira_app/assets/vendor/swiper/swiper-bundle.min.css' %}" rel="stylesheet">
{% load static %}
<link href="{% static 'vira_app/assets/css/style.css' %}" rel="stylesheet">
</head>
<body>
<main id="main">
<div id="portfolio-grid" class="row no-gutter" data-aos="fade-up" data-aos-delay="200">
{% if catalogue_list %}
{% for Catalogue in catalogue_list %}
<div class="item web col-sm-6 col-md-4 col-lg-4 mb-4">
<a href="{{ Catalogue.link }}" class="item-wrap fancybox">
<div class="work-info">
<h3>{{ Catalogue.title }}</h3>
<span>{{ Catalogue.source }}</span>
</div>
<img class="img-fluid" src="{{ Catalogue.image }}">
</a>
</div>
{% endfor %}
{% endif %}
</div>
</div>
</main>
<a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>
<script src="assets/vendor/aos/aos.js"></script>
<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="assets/vendor/php-email-form/validate.js"></script>
<script src="assets/vendor/swiper/swiper-bundle.min.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>
settings.py:
settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'vira_app', 'template')
],
'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',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = '/vira_app/template'
当我运行服务器并转到index.html时,从db检索的数据可以很好地显示,但是没有任何样式!
When I run the server and go to index.html, data retrieved from db and show well, but without any style!
我尝试了一些解决方案,请检查每个静态网址,但不能正常工作
I have tried some solution, check every static url, but not working
实际上,css,js和供应商未应用.有什么问题吗?
In fact, css, js and vendors not applied. What's the problem?
推荐答案
某些设置被滥用:
STATIC_URL ='/static/'
-很好
STATIC_ROOT ='/vira_app/template'
-不,这应该是与项目结构无关的文件夹.最后,在产品上,它可以是CDN URL或其他服务器上的文件夹.因此,一开始尝试将其更改为 STATIC_ROOT = os.path.join(BASE_DIR,'static')
之类的东西.
STATIC_ROOT = '/vira_app/template'
- nope, this is supposed to be some folder not really related to the project structure. In the end, on prod it can be a CDN URL or a folder on different server. So try changing it to something like STATIC_ROOT = os.path.join(BASE_DIR, 'static')
for the start.
如评论中所述,您可能需要定义 STATICFILES_DIRS
-必须从收集来自静态文件的文件夹列表,到
As mentioned in comments you might need to define STATICFILES_DIRS
- a list of folders where from static files must be collected to STATIC_ROOT by collectstatic
command. This means you can have many subfolders containing static files in different places. However you'll need to collect those files all together to deploy somewhere. STATICFILES_DIRS
+ collectstatic
will collect all of those files for you into given STATIC_ROOT
(and contents of this folder should be deployed somewhere, to CDN for example).
请注意, STATICFILES_FINDERS 的默认集合已经包含 AppDirectoriesFinder
,它将自动在任何项目应用中找到所有 static
子文件夹.这意味着,如果将 static
子文件夹从 templates
移到 vira_app
根目录-您无需在 STATICFILES_DIRS
.
Note, default set of STATICFILES_FINDERS already contains AppDirectoriesFinder
which will automatically find all the static
subfolders in any of the project apps. This means if you move static
subfolder from templates
to the vira_app
root - you won't have to mention it in the STATICFILES_DIRS
.
所以:
-
STATIC_ROOT
不应指向任何templates
子文件夹,而应将其更改为更全局"的名称. -
STATICFILES_DIRS
应该链接到您现在保留静态文件的文件夹,或者应将此文件夹移到vira_app
的根目录以让collectstatic
找到 在检查渲染页面中的样式和脚本之前,必须先运行 -
collectstatic
- 运行
collectstatic
后,所有静态文件必须保留在STATIC_ROOT
中,因此django将能够将STATIC_URL
之后的相对URL映射到之后的相对路径STATIC_ROOT
,这些文件将被加载
STATIC_ROOT
should not point to anytemplates
subfolders, change it to something more "global"STATICFILES_DIRS
should link to the folder(s) where you keep your static files now or this folder should be moved to the root ofvira_app
to letcollectstatic
find itcollectstatic
must be run before checking your styles and scripts in rendered page- after running
collectstatic
all the static files must persist inSTATIC_ROOT
so django will be able to map relative urls afterSTATIC_URL
to relative paths afterSTATIC_ROOT
and those files will be loaded
PS
请注意,您显示的某些静态文件在显示的模板中以错误的方式链接:
Note, some of your static files are linked in wrong way in the shown template:
<script src="assets/vendor/aos/aos.js"></script>
这也应该更改为 {%static ...
.
这篇关于django html模板找不到静态的css和js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!