Django - 引用模板中的静态文件 [英] Django - referencing static files in templates

查看:135
本文介绍了Django - 引用模板中的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的模板中引用静态文件时遇到困难。我正在使用Twitter Bootstrap,并且具有坐在mysite / static的引导文件(css,img,js)。

I'm having difficulty referencing static files in my templates. I am using Twitter Bootstrap and have the bootstrap files (css, img, js) sitting at mysite/static.

我已经设置了 STATIC_URL STATIC_ROOT code> TEMPLATE_CONTEXT_PROCESSORS 根据本教程。我已经运行了 ./ manage.py collectstatic ,它复制了72个文件。我还将以下模板标签添加到我的模板( index.html )文件中,但这没有起作用。

I have set the STATIC_URL, STATIC_ROOT and TEMPLATE_CONTEXT_PROCESSORS according to this tutorial. I have run ./manage.py collectstatic which copied 72 files over. I have also added the below template tag to my template (index.html) file but this hasn't worked.

{% load staticfiles %}
<link rel="stylesheet" href="{% static user_stylesheet %}" type="text/css" media="screen"/>

任何有关如何引用文件的帮助,以便引导样式返回到模板将非常感谢

Any help on how to reference the files so that the bootstrap styling returns to the templates would be much appreciated!

推荐答案

应该是

{%load static from staticfiles%}

然后,像

< link href ={%static'bootstrap / css / bootstrap.css'%} =stylesheettype =text / css>

完整性更新

文件夹结构


  • proj

    • app1

    • app2

    • myproj_public

    • static

      • css

        • bootstrap .css

        • proj
          • app1
          • app2
          • myproj_public
          • static
            • css
              • bootstrap.css

              • xyz.js

              设置文件

              STATIC_ROOT = os.path.join(os.path.abspath(
                  os.path.join(PROJECT_ROOT, 'myproj_public', 'static')), '')
              
              STATIC_URL = '/static/'
              

              这篇关于Django - 引用模板中的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆