【发布时间】: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 %}
【问题讨论】: