Python Django - 在模板中引用静态文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14013436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django - referencing static files in templates
提问by Jess
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_ROOTand TEMPLATE_CONTEXT_PROCESSORSaccording to this tutorial. I have run ./manage.py collectstaticwhich copied 72 files over. I have also added the below template tag to my template (index.html) file but this hasn't worked.
我已经设置了STATIC_URL,STATIC_ROOT并TEMPLATE_CONTEXT_PROCESSORS根据本教程. 我已经运行了./manage.py collectstatic它复制了 72 个文件。我还将以下模板标记添加到我的模板 ( 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!
任何有关如何引用文件以便引导程序样式返回模板的帮助将不胜感激!
采纳答案by super9
It should be
它应该是
{% load static from staticfiles %}
And then something like
然后像
<!-- path -->
<link href="{% static 'bootstrap/css/bootstrap.css' %}" rel="stylesheet" type="text/css">
<!--->
Update for Completeness
完整性更新
Folder Structure
文件夹结构
- proj
- app1
- app2
- myproj_public
- static
- css
- bootstrap.css
- js
- xyz.js
- css
- 项目
- 应用程序1
- 应用程序2
- myproj_public
- 静止的
- css
- bootstrap.css
- js
- xyz.js
- css
Settings File
设置文件
STATIC_ROOT = os.path.join(os.path.abspath(
os.path.join(PROJECT_ROOT, 'myproj_public', 'static')), '')
STATIC_URL = '/static/'
回答by ersran9
Are you setting the user_stylesheetcontext variable in your view? You need to set that before you can pass it onto templates.
您是否user_stylesheet在视图中设置上下文变量?您需要先设置它,然后才能将其传递给模板。
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_stylesheetcontext 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))
回答by WesternGun
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. It is a new mechanism introduced in Django 1.3, to easy the static file processing: it will collect all files in each STATICFILES_DIRand all staticdir under each installed app, so you copy them once in all.
,所有文件都复制到那里,而不是在project_root/static. 它是在Django 1.3中引入,以方便静态文件处理的新机制:它会收集各的所有文件STATICFILES_DIR和所有static目录每个已安装的应用程序下,所以你复制他们曾经在所有。
Refer the css/js files with static_rootpath.
引用带有static_root路径的 css/js 文件。

