gpt4 book ai didi

python - Django 模板继承不提供 css?

转载 作者:行者123 更新时间:2023-11-28 21:28:30 50 4
gpt4 key购买 nike

在我的 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',
)

View .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 %}

最佳答案

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

尝试改变:

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

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

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

关于python - Django 模板继承不提供 css?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7973872/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com