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

时间:2022-10-31 18:55:33

I am new in django, my problem is how to give a link to css and js

我是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 and js not load from actual path, dont know how to use actual path of static folder in html i am trying to use

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

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

But it give me the wrong path

但它给了我错误的道路。

i am providing a brief description:

我提供一个简短的描述:

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

my url is

我的网址是

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

error showing

错误显示

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)

It showing:

它显示:

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

but actual

但实际

If i moved static folder in testing folder, its me an html page without css and js link

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

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

Sorry for more description before now situation is:

不好意思,之前的情况是:

when page load css link give me an error 404 but same position index.html page load
how to include css/js?
In current situation, i am using
{% include 'header.html' %}
to include in index.html

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

1 个解决方案

#1


1  

Template files should go in the templates folder. And DIRNAME has the path to the settings.py.

模板文件应该放在模板文件夹中。DIRNAME有到达settings。py的路径。

Should probably be something like:

应该是这样的:

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

And make sure your index.html is in the templates folder.

确保你的索引。html在模板文件夹中。

#1


1  

Template files should go in the templates folder. And DIRNAME has the path to the settings.py.

模板文件应该放在模板文件夹中。DIRNAME有到达settings。py的路径。

Should probably be something like:

应该是这样的:

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

And make sure your index.html is in the templates folder.

确保你的索引。html在模板文件夹中。