简体   繁体   English

Django - 在模板中引用静态文件

[英]Django - referencing static files in templates

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.我正在使用 Twitter Bootstrap 并将引导程序文件(css、img、js)放在 mysite/static 中。

I have set the STATIC_URL , STATIC_ROOT and TEMPLATE_CONTEXT_PROCESSORS according to this tutorial .我已经根据本教程设置了STATIC_URLSTATIC_ROOTTEMPLATE_CONTEXT_PROCESSORS I have run ./manage.py collectstatic which copied 72 files over.我已经运行了./manage.py collectstatic它复制了 72 个文件。 I have also added the below template tag to my template ( index.html ) file but this hasn't worked.我还将以下模板标记添加到我的模板 ( index.html ) 文件中,但这没有用。

{% 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!任何有关如何引用文件以便引导程序样式返回模板的帮助将不胜感激!

It should be它应该是

{% load static %}

And then something like然后像

<!-- path -->
<link href="{% static 'bootstrap/css/bootstrap.css' %}" rel="stylesheet" type="text/css">
<!--->

Update for Completeness完整性更新

Folder Structure文件夹结构

  • proj项目
  • app1应用程序1
  • app2应用程序2
  • myproj_public myproj_public
  • static静止的
    • css css
      • bootstrap.css bootstrap.css
    • js js
      • xyz.js xyz.js

Settings File设置文件

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

STATIC_URL = '/static/'

Are you setting the user_stylesheet context variable in your view?您是否在视图中设置user_stylesheet上下文变量? You need to set that before you can pass it onto templates.您需要先设置它,然后才能将其传递给模板。

I usually just use the {{ static_url }} tag for doing this stuff, so my code for including bootstrap components would be like.我通常只使用{{ static_url }}标签来做这些事情,所以我的包含引导程序组件的代码就像。

<link href="{{ STATIC_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="{{ STATIC_URL }}bootstrap/js/jquery.js"></script>

Assuming that bootstrap folder is present inside static.假设 bootstrap 文件夹存在于 static 中。

EDIT编辑

For your case, to set user_stylesheet context variable, you'll need to do something like对于您的情况,要设置user_stylesheet上下文变量,您需要执行以下操作

dict["user_stylesheet"]= <path to your file>
#add other context variables you might have to
render_to_response(<your template name>, dict, context_instance=RequestContext(request))

When you set static_root , for example:当您设置static_root ,例如:

STATIC_ROOT = os.path.join(BASE_DIR, "static_root/")

, all files are copied there, instead of in project_root/static . ,所有文件都复制到那里,而不是在project_root/static It is a new mechanism introduced in Django 1.3, to easy the static file processing: it will collect all files in each STATICFILES_DIR and all static dir under each installed app , so you copy them once in all.这是 Django 1.3 中引入的一种新机制,用于STATICFILES_DIR静态文件处理:它将收集每个STATICFILES_DIR所有文件以及每个已安装应用程序下的所有static目录,因此您将它们全部复制一次。

Refer the css/js files with static_root path.使用static_root路径引用 css/js 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM