【问题标题】:Django template inheritance delivers no css?Django模板继承不提供css?
【发布时间】:2011-12-19 21:45:55
【问题描述】:

在我的 django 项目中,在我的开发环境中工作时,有一些我不理解的神秘(至少对我作为初学者而言)输出。 我想有一个基本模板,其中包含静态媒体文件夹中的样式表......到目前为止这有效......但仅针对地址http://localhost/,所有其他 url 都有一个从基本模板继承的模板。

现在http://localhost/ 的样式表看起来不错...如果我转到http://localhost/hello/,包含的样式表有一个完整的html DOM 结构,包括正文、文档类型等。这是为什么?他以某种方式解析了一个 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 %} 

【问题讨论】:

    标签: python django


    【解决方案1】:

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

    尝试改变:

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

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

    所以它总是在根目录中查找 media/style.css

    【讨论】:

    • 就是这样!但我不明白这个问题......他用根斜线获取数据而不只是显示它?坦克很多!
    【解决方案2】:

    现在您已将指向 css 的链接设置为相对 "media/style.css"。在 home 中,它解析为 "/media/style.css",但在 hello 中,它解析为 "/hello/media/style.css"(它给出了 hello 页面)。

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

    【讨论】:

      【解决方案3】:

      包含样式表的正确方法是

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

      【讨论】:

        【解决方案4】:

        如果这仍然不起作用..我的 css 文件名是 Stylesheet.css,我将其链接为 css/Stylesheet.css ..它不起作用...然后我将其更改为 css/Stylesheet.CSS ..它工作

        【讨论】:

          【解决方案5】:

          将 href="media/style.css" 更改为 href="media/style.CSS" 即可。

          【讨论】:

            猜你喜欢
            • 2021-02-13
            • 2014-08-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-09
            • 2021-03-10
            • 2018-08-18
            相关资源
            最近更新 更多