如何在django中添加我自己的html模板

时间:2013-08-10 11:09:15

标签: python django

我是django的新手,我的问题是如何给出css和js的链接

{% load staticfiles %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>Welcome</title>   
     <link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
     <link rel="stylesheet" type="text/css" href="{% static "css/templatemo_style.css" %}" />
     <script type="text/javascript" src="js/jquery.min.js"></script>

css和js没有从实际路径加载,不知道如何在html中使用静态文件夹的实际路径 我正在尝试使用

import os
#DIRNAME = os.path.abspath(os.path.dirname(__file__))
DIRNAME = os.path.dirname(__file__)

但它给了我错误的道路

我提供简要说明:

TEMPLATE_DIRS = (
os.path.join(DIRNAME, 'static/'),
}

我的网址是

http://127.0.0.1:8000/blog/home/

错误显示

Template-loader postmortem

Django tried loading these templates, in this order:

Using loader django.template.loaders.filesystem.Loader:
    D:\Eclipse_JEE_new_workspace\testing\testing\static\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
    C:\Python27\lib\site-packages\django\contrib\auth\templates\index.html (File does not exist)
    C:\Python27\lib\site-packages\django\contrib\admin\templates\index.html (File does not exist)

显示:

D:\Eclipse_JEE_new_workspace\testing\testing\static\index.html

但实际

如果我在测试文件夹中移动了静态文件夹,那么它是一个没有css和js链接的html页面

D:\Eclipse_JEE_new_workspace\testing\static\index.html

很抱歉在此之前有更多说明,情况是:

当页面加载css链接给我一个错误404但是同一位置index.html页面加载
如何包含css / js?
在目前的情况下,我正在使用
{%include'header.html'%}
包含在index.html中

1 个答案:

答案 0 :(得分:1)

模板文件应该放在templates文件夹中。 DIRNAMEsettings.py的路径。

应该是这样的:

DIRNAME = os.path.join(os.path.dirname(__file__), '..')
TEMPLATE_DIRS = (
    os.path.join(DIRNAME, 'templates/'),
}

并确保您的index.html位于templates文件夹中。