问题描述
我有一个结构是这样的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!
我尝试了一些解决方案,检查每个静态 url,但没有工作
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'
- 不,这应该是一些与项目结构无关的文件夹.最后,在 prod 上,它可以是 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
- 必须收集来自静态文件的文件夹列表到STATIC_ROOT 通过 collectstatic
命令.这意味着您可以在不同的位置拥有许多包含静态文件的子文件夹.但是,您需要将这些文件全部收集起来以部署到某个地方.STATICFILES_DIRS
+ collectstatic
将为您收集所有这些文件到给定的 STATIC_ROOT
(this 文件夹的内容应该是部署到某个地方,例如 CDN).
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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!