一、extends使用方法

首先 extends 也就是继承,子类继承父类的一些特性。在Django 模板中通过继承可以减少重复代码。

首先我们建立一个app,名字叫做 hello。别忘了在 settings.py 中的 INSTALLED_APPS 注册这个app。不注册会出现 hello 目录下的 templates 中的模板无法调用。

1.在根目录下的 templates 创建 base.html

{% block title %}{% endblock %}

{% block content %}

{% endblock %}

 这里的 block 就是继承后要被替换的内容,其他的则与该模板相同.

 

2.在hello这个app中继承这个模板

在hello目录下新建templates目录,再在此目录下新建hello目录。hello目录中新建 hello.html。目录结构就像这样,理解一下django模板引用路径的规则

 

hello.html 内容如下

{% extends 'base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}{{ content }}{% endblock %}

 

首行代码就是先继承 base.html,也就是有了除了block的剩下内容。后面两个则是在为 block 填空,填入的内容是要传入的参数。这里分别是 title 和 content。这里可以看出模板语句是单个大括号加百分号,而模板变量是双大括号,没有百分号。

3.添加路由和方法

路由文件 urls.py 内容如下

from django.contrib import admin

from django.urls import path

from hello import views

urlpatterns = [

path('hello///',views.hello),

path('admin/', admin.site.urls),

]

 注意在路径中获取参数的方法,str还可以换为int。我觉得这种传参的方法比较简单,还有正则表达式的传参方法。

 

views.py

from django.shortcuts import render

# Create your views here.

def hello(request,title,content):

context={'title':title,'content':content}

return render(request,'hello/hello.html',context)

 

这里函数的形参名就是在路由中获取的参数名。通过传递参数会render给hello.html这个模板,而hello.html这个模板又会继承base.html这个父模板。

实现效果如下

 

4.总结extends

通过 extends 可以减少代码重复。可以再增加header、footer等来包含共同的头部和底部内容。

include 要么放在 head 中,可以减少重复引入css或js,要么放在body中当一个共同的导航条,或者底部内容。

然而如果要在多处都 include 就不如直接用 extends 了。

 

二、include使用方法

这里我们在 hello 下新建 hello2.html 和 hello3.html。

hello2.html 内容如下

hello2

{% include 'hello/hello3.html' %}

 

hello3.html 内容如下

this is a p in hello3.html

 

可以看出,hello2.html 包含了 hello3.html 中的内容。这样也达到了减少重复代码的作用。再添加 views 中 hello2 方法

views.py

def hello2(request):

return render(request,'hello/hello2.html')

 

以及 urls 中添加 hello2

urls.py

from django.contrib import admin

from django.urls import path

from hello import views

urlpatterns = [

path('hello2/',views.hello2),

path('hello///',views.hello),

path('admin/', admin.site.urls),

]

 

 

实现效果如下

 

load

某些应用提供自定义标签和过滤器库。要在一个模板中访问它们, 使用 {% load %} 标签:{% load comments %} {% load %} 标签可接受空隔分隔的多个库的名字作为参数。{% load comments i18n %}当载入一个自定义标签或过滤器库, 只有当前模板可以使用这些标签/过滤器 — 继承链中不论是父模板还是子模板都不能使用使用这些标签和过滤器。

 {%load staticfiles%} {%load static%}

在使用django的时候,加载静态文件时候,在index.html中输入{% load staticfiles %}进行static在html中的声明在加载static静态目录之时,则可以使用href='{% static 'css/reset.css' %}'

{% load staticfiles %} ### STATICFILES_DIRS{% static 'css/style.css' %}在一个网页中,不仅仅只有一个html骨架,还需要css样式文件,js执行文件以及一些图片等。因此在DTL中加载静态文件是一个必须要解决的问题。在DTL中,使用static标签来加载静态文件。要使用static标签,首先需要{% load static %}。 加载静态文件的步骤如下: 首先确保django.contrib.staticfiles已经添加到settings.INSTALLED_APPS中。 确保在settings.py中设置了STATIC_URL。

 

For the moment (Django 1.9 and earlier), {% load staticfiles %} loads the static templatetag from the contrib app that has more features than the built-in django.core.static.

The most important difference is staticfiles can manage files stored on CDN, since its resolver can manage hashes for example. core.static only append STATIC_URL to the static filename, which is not enough if you're processing your files (e.g. adding md5 hash to clear cache between releases)

This difference is due to the fact that managing non-local storage files was not dedicated to be included in the core package of Django, but was still useful to many developers to be implemented as a official contrib package. So if you started to use staticfiles, you had to remember to use it every in your templates. BUT, some problems could appear, for example when using Media classes so the decision has been to merge those two templatetags into one and use a different behaviour whether the developer has django.contrib.staticfiles in its INSTALLED_APPS or not.

From Django 1.10 and onwards (also see ticket in Django tracker), the {% load static %} is going to use staticfiles internally if activated (oherwise keep default behaviour), and the templatetag in the contrib package will be deprecated to avoid confusion.

 

在一个网页中,不仅仅只有一个html骨架,还需要css样式文件,js执行文件以及一些图片等。因此在DTL中加载静态文件是一个必须要解决的问题。在DTL中,使用static标签来加载静态文件。要使用static标签,首先需要{% load static %}。加载静态文件的步骤如下:    首先确保 django.contrib.staticfiles 已经添加到 settings.INSTALLED_APPS 中。    确保在 settings.py 中设置了 STATIC_URL。    注意: 上面两条都是在创建Django 项目的时候就自动给我们弄好了,只要我们没有去改动它,就不用管。    在已经安装了的app下创建一个文件夹叫做static,然后再在这个static文件夹下创建一个当前app的名字的文件夹,再把静态文件放到这个文件夹下。例如你的app叫做book,有一个静态文件叫做book.jpg,那么路径为book/static/book/book.jpg。(为什么在app下创建一个static文件夹,还需要在这个static下创建一个同app名字的文件夹呢?原因是如果直接把静态文件放在static文件夹下,那么在模版加载静态文件的时候就是使用book.jpg,如果在多个app之间有同名的静态文件,这时候可能就会产生混淆。而在static文件夹下加了一个同名app文件夹,在模版中加载的时候就是使用app名/book.jpg,这样就可以避免产生混淆。)    注意: 文件夹的名字必须为static 。    如果有一些静态文件是不和任何app挂钩的。即不再任何一个app的目录下。那么可以在settings.py中添加STATICFILES_DIRS,以后DTL就会在这个列表的路径中查找静态文件。例如我们在manage.py的同级目录下新建一个static的文件夹。然后在settings.py:中添加STATICFILES_DIRSSTATICFILES_DIRS = [    os.path.join(BASE_DIR,"static")]注: 第三种和第四种方法都可以加载静态文件,我的个人习惯是在manage.py的同级目录下新建一个static文件夹,然后将所有的静态文件进行分类的在里面存储。而不去app中新建一个static的文件夹。但这只是我的个人习惯。毕竟不管是黑猫白猫,能抓到老鼠的就是好猫,所以只要我们能把项目做出来能运行,并且代码结构有逻辑性、层次感就行了。    在模版中使用load标签加载static标签。比如要加载在项目的static文件夹下的style.css的文件。那么示例代码如下:{% load static %}注意: {% load static %}需要放在html的头部位置(至少在使用static标签的上面),一般都是放在html的最上面。如果{% extend %}标签和{% load static %}同时存在,{% extend %}需要放在最上面,然后再放{% load static %}等标签。    如果不想每次在模版中加载静态文件都使用load加载static标签,那么可以在settings.py中的TEMPLATES/OPTIONS添加'builtins':['django.templatetags.static'],这样以后在模版中就可以直接使用static标签,而不用手动的load了。    注意: 位置不要添加错误了TEMPLATES = [    {        'BACKEND': 'django.template.backends.django.DjangoTemplates',        'DIRS': [os.path.join(BASE_DIR, 'templates')]        ,        'APP_DIRS': True,        'OPTIONS': {            'context_processors': [                'django.template.context_processors.debug',                'django.template.context_processors.request',                'django.contrib.auth.context_processors.auth',                'django.contrib.messages.context_processors.messages',            ],            #添加在这个位置            'builtins' : [                'django.templatetags.static'            ],        },    },]————————————————链接:https://blog.csdn.net/xujin0/article/details/83421626

 

 

REF

https://www.cnblogs.com/roadwide/p/11318048.html

https://www.cnblogs.com/pinganzi/p/4825149.html

https://stackoverflow.com/questions/34422971/load-static-and-load-staticfiles-which-is-preferred

https://blog.csdn.net/J_wb49/article/details/103055817

https://blog.csdn.net/xujin0/article/details/83421626

查看原文