我想使用图像文件作为背景图像Django.但是我不知道怎么做.首先,我读了这篇文章并尝试在css文件中写下这样的内容.
#third{
background: url("img/sample.jpeg") 50% 0 no-repeat fixed;
color: white;
height: 650px;
padding: 100px 0 0 0;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
{% load staticfiles %}
#third{
background: url({% static "img/sample.jpeg" %}) 50% 0 no-repeat fixed;
}
Run Code Online (Sandbox Code Playgroud)
和
#third{
background: url("../img/sample.jpeg") 50% 0 no-repeat fixed;
}
Run Code Online (Sandbox Code Playgroud)
也不行.
当你在css文件上使用background-image时,你通常如何编写css文件?你能给我一些建议吗?
C:\~~~~~~> dir hello\static\img
2016/09/28 19:56 2,123 logo.png
2016/09/24 14:53 104,825 sample.jpeg
C:\~~~~~~> dir hello\static\css
2016/09/29 20:27 1,577 site.css
C:\~~~~~~> more lemon\settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') …Run Code Online (Sandbox Code Playgroud) 处理用户自定义样式的最佳方法是什么?正如我正在寻找的结果的一个例子,这就足够了:
body {
color: {{ user.profile.text_color }};
}
Run Code Online (Sandbox Code Playgroud)
但是,将CSS作为视图提供似乎会导致不断请求的文件中的大量开销,因此这可能不是一个好的解决方案.
用户无权访问CSS文件,我们必须假设他们没有Web开发知识.
我已经通过使用亚马逊s3在Heroku上设置我的Django应用程序来设置静态和媒体文件.我一直遵循本指南https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/和似乎成千上万的其他资源,collectstatic已经工作,heroku正在部署它 - 但显示400错误.当我尝试在本地运行它时,我获得更多信息:
Attempted access to '/css/reset.css' denied.
Run Code Online (Sandbox Code Playgroud)
这是突出显示的行:
<link rel="stylesheet" type="text/css" href="{% static '/css/reset.css' %}">
Run Code Online (Sandbox Code Playgroud)
我可以直接从URL加载静态文件,如果我从我的s3管理面板中获取它,所以我认为这不是一个存储桶策略问题,我已经搞砸了https/http选项,但没有快乐.所以我认为它必须是在代码中以某种方式被调用的错误路径 - 我只是看不到哪里!
任何帮助非常感谢,我不认为我已经眨了大约4个小时.
追溯:
File "/home/devtop/webdev/projects/intro/myvenv/lib/python3.5/site-packages/storages/backends/s3boto.py" in _normalize_name
358. return safe_join(self.location, name)
File "/home/devtop/webdev/projects/intro/myvenv/lib/python3.5/site-packages/storages/backends/s3boto.py" in safe_join
59. raise ValueError('the joined path is located outside of the base path'
During handling of the above exception (the joined path is located outside of the base path component), another exception occurred:
File "/home/devtop/webdev/projects/intro/myvenv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/home/devtop/webdev/projects/intro/myvenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. …Run Code Online (Sandbox Code Playgroud)