使用django 1.3并进行一些模板继承。离开主页后,我的/static/
设置路径似乎有问题。问题是,当我加载从base.html继承的home.html时,CSS和Image链接工作正常。但是一旦我去了一个额外的URL(在这个例子中是vehicle.html),css和图像会因以下错误而丢失:
控制台错误
"GET /static/%20/static/images/logo_2.jpg HTTP/1.1" 404 1771
"GET /static/%20/static/css/default.css HTTP/1.1" 404 1765
查看页面来源
<link rel="stylesheet" type="text/css" href="/static/ /static/css/default.css">
<link rel="stylesheet" type="text/css" href="/static/ /static/css/default.css">
看起来有些空间出现在某个地方。此外,从vehilce.html文件中,页面源显示其向网址添加额外的/static/
会占用空间。我哪里可能出错?请参阅下面的文档:
settings.py
STATIC_ROOT = 'D:/dev/workspace/vehicle_request/vehicle_request/mvmanager/static/'
STATIC_URL = '/static/'
urls.py
urlpatterns = patterns('',
url(r'^$', home_page),
(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}), #Not in use in my code yet
(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT}),
(r'^vehicle/', vehicle),
(r'^driver/', driver),
base.html文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}/static/css/default.css">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<header>
<a href="http://localhost:8000" title="Home Page">
<img alt="logo2:" src="{{ STATIC_URL }}/static/images/logo_2.jpg"
style="float:left; margin:5px" height="100"; border=none"></a><br><br>
<h1>Vehicle Request System <span class="version">(Version 0.1 beta)</span></h1>
</header>
<hr style="clear: left">
<hr>
</body>
</html>
vehicle.html
{% extends "base.html" %}
{% block title %}Vehicle Registration{% endblock %}
{% block content %}
<html>
<head></head>
<body></body>
</html>
{% endblock %}
感谢。
编辑
1。我已将stati
中的href="{{STATIC_URL}} /static/css/default.css">
更改为static
,因为这就是我的代码中的内容。我必须在发帖时错误地退格。继续前进,
2。当我将{{STATIC_url}} /Static
更改为{{STATIC_URL}}/Static
时,即。我删除了这两个单词之间的空格 - 由下面的@sarnold建议,home.html
照常工作但vehicle.html
仍然没有加载css和图像,它在控制台中显示下面的错误:
File "c:\Python27\lib\site-packages\django\core\files\storage.py", line 234, in path
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
SuspiciousOperation: Attempted access to '\static\images\logo_2.jpg' denied.
[10/Feb/2012 06:11:55] "GET /static//static/images/logo_2.jpg HTTP/1.1" 500 1731
File "c:\Python27\lib\site-packages\django\core\files\storage.py", line 234, in path
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
SuspiciousOperation: Attempted access to '\static\css\default.css' denied.
[10/Feb/2012 06:23:29] "GET /static//static/images/logo_2.jpg HTTP/1.1" 500 1731
3。当我结合@sarnold和@cptphil建议时,vehicle.html
会完美地加载css和图像。然而home.html
失败了。查看以下base.html
的更改:
base.html(编辑看起来像这样)
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}}css/default.css">
<img alt="logo2:" src="{{STATIC_URL}}images/logo_2.jpg"
home.html现在没有'工作! (查看页面来源)
<link rel="stylesheet" type="text/css" href="css/default.css">
<img alt="logo2:" src="images/logo_2.jpg"
vehicle.html现在有效(查看页面来源)
<link rel="stylesheet" type="text/css" href="/static/css/default.css">
<img alt="logo2:" src="/static/images/logo_2.jpg"
这就是 home.html 在原始格式中的样子
{% extends "base.html" %}
{% block title %}GEL: Vehicle Request System{% endblock %}
{% block content %}
{% endblock %}
答案 0 :(得分:5)
我相信模板引擎正在做你要求它做的事情。
href="{{STATIC_URL}} /stati/css/default.css"
正确翻译为
/static/ /stati/css/default.css
尝试
href="{{STATIC_URL}}css/default.css"
如果您所追求的是,请在base.html中
/static/css/default.css
无法解释为什么home.html可以正常工作。
答案 1 :(得分:2)
在我看来,您手动将空格添加到base.html
:
<link rel="stylesheet" type="text/css" href="{{STATIC_URL}} /stati/css/defau...
<img alt="logo2:" src="{{ STATIC_URL }} /static/imag....
取出/stati/css
和/static/imag
之前的空格,看看问题是否消失。
答案 2 :(得分:0)
空间=%20。请务必从网址中省略空格......
like : <script src="{% static " js/dashboard/salesPieChart.js" %}"></script>
To: <script src="{% static "js/dashboard/salesPieChart.js" %}"></script>