为什么背景:与Django的工作URL在CSS? [英] Why is background:url in CSS not working with Django?
问题描述
我有以下的CSS code为我的导航栏:
#页脚导航{
背景:#1841c8 URL('../图像/ nav_background.gif');
高度:40像素;
填充:0 0 0 20px的;
}
然而,当我启动我的本地开发服务器,它给了我以下,即使图像中的位置存在:
GET /images/nav_background.gif HTTP / 1.1404 1795
我运行Django的1.3.1,和我使用django.views.static.serve。
GET /images/nav_background.gif HTTP / 1.1404 1795
您想要使用的图片是找不到的。我建议你做以下步骤:
您可以尝试以下步骤:
-
打开你的settings.py以及
-
添加此在你的文件的第一行:
进口os.path中
-
更改STATIC_ROOT的值设置为:
STATIC_ROOT = os.path.join(PROJECT_DIR,'静/')
-
更改STATIC_URL的值设置为:
STATIC_URL ='/静态/
-
-
创建一个在您的项目命名为根静态文件夹中。
- 创建一个文件夹为您的静态文件如CSS,JavaScript和等等,我建议你使用一个不同的文件夹不同类型的文件。
-
打开项目的urls.py
- 添加到您的导入:导入设置
-
这添加到URL模式:
(R'(* /)(P<路径>(CSS | jQuery的| jscripts |图片)/.+)$????','django.views .static.serve',{'DOCUMENT_ROOT':settings.STATIC_ROOT})
请注意:在这个例子中,有一个名为里面我的静态文件夹中的CSS,jQuery的,jscripts和图像文件夹
。 -
在您的模板补充一点:
有css文件:(在该例子中,default.css是CSS文件的名称)
<链接HREF =/ {{STATIC_ROOT}} CSS / default.css的rel =stylesheet属性类型=文/ CSS媒体=所有/>
JavaScript的:
<脚本类型=文/ JavaScript的SRC =/ {{STATIC_ROOT}} jQuery的/ jquery.js和>< / SCRIPT>
然后更改您的code这样:
#页脚导航{
背景:#1841c8网址(图片/ nav_background.gif);
高度:40像素;
填充:0 0 0 20px的;
}
I have the following CSS code for my navigation bar:
#footer-navigation {
background: #1841c8 url('../images/nav_background.gif');
height: 40px;
padding: 0 0 0 20px;
}
However, when I launch my local development server, it gives me the following even though the image exists in that location:
"GET /images/nav_background.gif HTTP/1.1" 404 1795
I am running Django 1.3.1, and I am using django.views.static.serve.
"GET /images/nav_background.gif HTTP/1.1" 404 1795
The image you want to use is not found. I suggest you do follow these steps:
You can try these steps:
open your settings.py and
add this at the first line of your file:
import os.path
change your STATIC_ROOT's value to:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
change your STATIC_URL's value to:
STATIC_URL = '/static/'
create a folder named "static" in your project root.
- create a folder for your static files like css, javascript and etc. I recommend you use a different folder for different types of files.
open the urls.py of your project
- add this to your imports: import settings
add this to the url patterns:
(r'(?:.*?/)?(?P<path>(css|jquery|jscripts|images)/.+)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }),
NOTE: In this example, there are folders named css, jquery, jscripts and images inside my static folder.
In your template add this:
for css files: (in this example, default.css is the name of the css file)
<link href="/{{ STATIC_ROOT }}css/default.css" rel="stylesheet" type="text/css" media="all" />
for javascript:
<script type="text/javascript" src="/{{ STATIC_ROOT }}jquery/jquery.js"></script>
then change your code to this:
#footer-navigation {
background: #1841c8 url(images/nav_background.gif);
height: 40px;
padding: 0 0 0 20px;
}
这篇关于为什么背景:与Django的工作URL在CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!