[英]How to get django static files in app directory?
I have some doubts about accessing django's static files.我对访问 django 的静态文件有一些疑问。
VERSION: 1.8.4版本:1.8.4
VARs in settings.py: settings.py 中的 VAR:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
# os.path.join(BASE_DIR, 'templates').replace('\\', '/')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
setting STATICFILES_DIRS, and adding django.template.context_processors.static
to context_processors in TEMPLATES
, so I can use 'static' tag in template.设置 STATICFILES_DIRS,并将
django.template.context_processors.static
添加到TEMPLATES
context_processors,因此我可以在模板中使用“静态”标签。 A src tag like: src="{{STATIC_URL}}js/example.js"
or src="{% static 'js/example.js' %}"
will generate a url to http://myhost/static/js/example.js
. src 标签如:
src="{{STATIC_URL}}js/example.js"
或src="{% static 'js/example.js' %}"
将生成一个指向http://myhost/static/js/example.js
的 URL http://myhost/static/js/example.js
。
If I want load static files in app directory, what should I do?如果我想在应用程序目录中加载静态文件,我该怎么做? As my understanding of django-official-doc,
根据我对 django-official-doc 的理解,
STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'django.contrib.staticfiles.finders.FileSystemFinder', )
the finders will search app/static
in default, but if a src="static/js/example.js"
in app/templates/index.html
, the server will return a 404 code.默认情况下,finder 将搜索
app/static
,但如果app/templates/index.html
的src="static/js/example.js"
,服务器将返回 404 代码。 Until I add urlpatterns += staticfiles_urlpatterns()
to app/urls.py
.直到我将
urlpatterns += staticfiles_urlpatterns()
添加到app/urls.py
。
What i want to know is:我想知道的是:
Why 404?为什么是404? When load static file form
app/static
without urlpatterns += staticfiles_urlpatterns()
.当加载静态文件表单
app/static
没有urlpatterns += staticfiles_urlpatterns()
。
If I want to get static file in app/static
, how to write url in a flexible way?如果我想在
app/static
获取静态文件,如何以灵活的方式编写 url? cause {{STATIC_URL}}
or {% static %}
both point to project/static.导致
{{STATIC_URL}}
或{% static %}
都指向 project/static。
I have spend hours in reading related post on stackoverflow.我花了几个小时阅读有关 stackoverflow 的相关文章。 Any help would be sooo greatly appreciated.
任何帮助将不胜感激。
Add the following code in your projects settings.py
在您的项目
settings.py
添加以下代码
STATIC_URL = '/public/static/'
STATICFILES_DIRS = (
location('static'),
)
STATIC_ROOT = location('public/static')
Then you should be able to access the contents in the url like :然后你应该能够访问 url 中的内容,如:
http://127.0.0.1:8000/public/static/js/bootstrap.min.js
Let say you have project named MyProject
provided you must have a folder named假设您有一个名为
MyProject
项目,前提是您必须有一个名为
Myproject->public->static->js->bootstrap.min.js
inside your projects directory .在您的项目目录中。
and in your webpages you can simply refer to it by :在您的网页中,您可以通过以下方式简单地引用它:
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
Hope the idea is very clear .希望思路很清晰。 In Case of doubts revert back.
如有疑问,请回复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.