2011-11-01 87 views
2

在我的Django项目有一些神秘的(至少对我来说作为一个beinner)输出在我的开发环境中工作时,我不明白。 我想有一个基本模板,其中包括在静态媒体文件夹的样式表......到目前为止是这种情况......但只是地址http://localhost/所有其他URL都从基本模板继承的模板。Django模板继承不提供css?

现在http://localhost/的样式表看起来不错...如果我去http://localhost/hello/包含的样式表有一个完整的html DOM结构体,doctype等。为什么?他莫名其妙地解析HTML的网站,而不是采取CSS文件...

这里我的代码:任何想法?

urls.py:

from django.views.static import * 
from django.conf import settings 
admin.autodiscover() 

urlpatterns = patterns('', 
    ('^$',home_view), 
    ('^hello/$', hello), 
    (r'^admin/', include(admin.site.urls)), 
    ('^useragent/$',ua_display_good1), 
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', 
) 

views.py

from django.http import HttpResponse 
from django.shortcuts import render_to_response 

def hello(request): 
    pagetitle = "Hello World" 
    return render_to_response('hello.tpl', {'pagetitle': pagetitle}) 

def home_view(request): 
    pagetitle = "Something" 
    return render_to_response('home.tpl', {'pagetitle': pagetitle}) 

def ua_display_good1(request): 
    try: 
     ua = request.META['REMOTE_ADDR'] 
    except KeyError: 
     ua = 'unknown' 
    return render_to_response('base.tpl',{'ageone': ua}) 

基本模板:

<!DOCTYPE html> 
<html lang="de"> 
<meta name="description=" content="{{metadescription}}"> 
<head> 
<link rel="stylesheet" type="text/css" href="media/style.css"> 

<title>{% block title %}{{pagetitle}}{% endblock %}</title> 
</head> 
<body> 
<h1>{% block h1 %}{{ageone}}{% endblock %}</h1> 
{% block content %}{% endblock %} 
{% block footer %}{% include "footer.tpl" %} 
{% endblock %} 
</body> 
</html> 

你好模板:

{% extends "base.tpl" %} 
{% block h1 %}Home{% endblock %} 
{% block content %}Welcome{% endblock %} 

回答

2

现在你已经链接设置为CSS为相对"media/style.css" 。在家里它解析为"/media/style.css",但在招呼它解析为"/hello/media/style.css"(这给hello页)。

只需使用绝对CSS链接是这样的:"/media/style.css"

+0

现在我明白了谢谢你! – Jurudocs

3

可能是因为您对CSS文件有相对引用。

尝试改变:

<link rel="stylesheet" type="text/css" href="media/style.css"> 

<link rel="stylesheet" type="text/css" href="/media/style.css"> 

所以它看起来总是在根媒体/ style.css中

+0

这就是它!但我不明白这个问题......用root斜线他得到的数据,而不是只显示它?坦克很多! – Jurudocs

1

的正确方法包括样式表将

<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}style.css">