[英]Serving static files in development
I've got some static files that are currently serving from Amazon S3. 我有一些当前从Amazon S3服务的静态文件。 This obviously isn't effective because I can't collectstatic
every time I make a minor change. 这显然是无效的,因为每次进行较小的更改时我都无法collectstatic
。 Whenever I'm developing I want to serve from my project where the static files are collected from. 每当我进行开发时,我都希望从我的项目中为它提供静态文件。 The project structure looks partially like this: 项目结构看起来部分如下:
myproject
-app
--templates
-myproject
--static
---app
----css
----js
----img
My settings.py
looks partially like this: 我的settings.py
部分看起来像这样:
STATIC_ROOT = 'staticfiles'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/app'),
)
AWS_HEADERS = {
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
'Cache-Control': 'max-age=94608000',
}
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
if DEBUG:
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATIC_URL = '/static/'
else:
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
STATICFILES_LOCATION = 'static'
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
What I'm trying to achieve is that if I'm running development (when DEBUG
would be true) serve static files from the project, rather than fetching from S3. 我想要实现的是,如果我正在运行开发(当DEBUG
为true时)将从项目中提供静态文件,而不是从S3中获取。
At the end of my urlpatterns
in urls.py
I have + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
. 在urls.py
urlpatterns
末尾,我有+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
。
Finally, in my base.html
I have: 最后,在我的base.html
我有:
<!doctype html>
...
<link rel="stylesheet" href="{% static '/css/normalize.css'%}">
If I'm running with debug=True
and I view the source of the page I see <link rel="stylesheet" href="/css/normalize.css">
which isn't where the file is. 如果我使用debug=True
运行,并且查看页面的源代码,则会看到<link rel="stylesheet" href="/css/normalize.css">
不在文件中的位置。 If I run with debug=False
and view the source I get <link rel="stylesheet" href="<AMAZON BUCKET>/css/normalize.css">
. 如果我运行debug=False
并查看源代码, <link rel="stylesheet" href="<AMAZON BUCKET>/css/normalize.css">
得到<link rel="stylesheet" href="<AMAZON BUCKET>/css/normalize.css">
。
What am I missing to serve locally? 在本地服务时我缺少什么?
您必须将相对路径传递到static
模板标签,而不能使用斜杠:
{% static 'css/normalize.css' %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.