<tfoot id='GTgfX'></tfoot>

    <small id='GTgfX'></small><noframes id='GTgfX'>

      <bdo id='GTgfX'></bdo><ul id='GTgfX'></ul>
  1. <legend id='GTgfX'><style id='GTgfX'><dir id='GTgfX'><q id='GTgfX'></q></dir></style></legend>
    1. <i id='GTgfX'><tr id='GTgfX'><dt id='GTgfX'><q id='GTgfX'><span id='GTgfX'><b id='GTgfX'><form id='GTgfX'><ins id='GTgfX'></ins><ul id='GTgfX'></ul><sub id='GTgfX'></sub></form><legend id='GTgfX'></legend><bdo id='GTgfX'><pre id='GTgfX'><center id='GTgfX'></center></pre></bdo></b><th id='GTgfX'></th></span></q></dt></tr></i><div id='GTgfX'><tfoot id='GTgfX'></tfoot><dl id='GTgfX'><fieldset id='GTgfX'></fieldset></dl></div>

      django html模板找不到静态css和js文件

      时间:2024-04-21
      <tfoot id='zK0MB'></tfoot><legend id='zK0MB'><style id='zK0MB'><dir id='zK0MB'><q id='zK0MB'></q></dir></style></legend>

      • <small id='zK0MB'></small><noframes id='zK0MB'>

          <bdo id='zK0MB'></bdo><ul id='zK0MB'></ul>

            <tbody id='zK0MB'></tbody>

                <i id='zK0MB'><tr id='zK0MB'><dt id='zK0MB'><q id='zK0MB'><span id='zK0MB'><b id='zK0MB'><form id='zK0MB'><ins id='zK0MB'></ins><ul id='zK0MB'></ul><sub id='zK0MB'></sub></form><legend id='zK0MB'></legend><bdo id='zK0MB'><pre id='zK0MB'><center id='zK0MB'></center></pre></bdo></b><th id='zK0MB'></th></span></q></dt></tr></i><div id='zK0MB'><tfoot id='zK0MB'></tfoot><dl id='zK0MB'><fieldset id='zK0MB'></fieldset></dl></div>
              1. 本文介绍了django html模板找不到静态css和js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个结构是这样的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 any templates 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 of vira_app to let collectstatic find it
                • collectstatic must be run before checking your styles and scripts in rendered page
                • after running collectstatic all the static files must persist in STATIC_ROOT so django will be able to map relative urls after STATIC_URL to relative paths after STATIC_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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

              2. <small id='Eik8B'></small><noframes id='Eik8B'>

                <i id='Eik8B'><tr id='Eik8B'><dt id='Eik8B'><q id='Eik8B'><span id='Eik8B'><b id='Eik8B'><form id='Eik8B'><ins id='Eik8B'></ins><ul id='Eik8B'></ul><sub id='Eik8B'></sub></form><legend id='Eik8B'></legend><bdo id='Eik8B'><pre id='Eik8B'><center id='Eik8B'></center></pre></bdo></b><th id='Eik8B'></th></span></q></dt></tr></i><div id='Eik8B'><tfoot id='Eik8B'></tfoot><dl id='Eik8B'><fieldset id='Eik8B'></fieldset></dl></div>
              3. <tfoot id='Eik8B'></tfoot>
                  <tbody id='Eik8B'></tbody>

                    <bdo id='Eik8B'></bdo><ul id='Eik8B'></ul>
                  • <legend id='Eik8B'><style id='Eik8B'><dir id='Eik8B'><q id='Eik8B'></q></dir></style></legend>