**app里的urls**
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
# path('',views.indexviews.as_view()),
# path('news/',views.news,name="news"),#我们这里是通过命名空间来调用
# path('sport/',views.sport,name="sport")
path('',views.index,name="index")
]
**views.py**
from django.shortcuts import render
from django.http import HttpResponse
from django.views import View
# Create your views here.
class indexviews(View):
def get(self,request):
return render(request, "index.html")
#DTL语言,模板变量的使用
def get(self,request):
requests= {
'scheme':request.scheme,
'method':request.method,
'title':"sport news",
'book_list':{
'book_name':"Django",
'book_context':"HTML",
}
}
return render(request,"index.html",context=requests)
##继承extends
def get(self,request):
return render(request,"extend.html")
#自动转义
def get(self,request):
context={
"qq":'<a href="http://www.qq.com">QQ</a>'
}
return render(request,"extend.html",context=context)
#条件判断 if
def get(self, request):
context = {
"age":32,
}
return render(request, "extend.html", context=context)
#For 循环以及空循环For Empty
def get(self, request):
context = {
"books":[
"python","java","c++"
],
"menu":{
"title":"python",
"price":15,
"author":"wei"
},
"empty":[],
"error":"内容为空"
}
return render(request, "extend.html", context=context)
#with标签
def get(self, request):
context = {
"books":{
"python":{
"django":"with"
}
}
}
return render(request, "extend.html", context=context)
以下是模板中的文件
**base.html**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>这是基类</h1>
{% block content%}
{% endblock %}
{% now "Y M d H:i:s"%}
<!--需要设置时区,不然显示时间不对-->
</body>
</html>
**extend.html**
{% extends 'base.html'%}
使用extends进行继承,就可以在当前文件里进行续写
需要注意的是,如果继承的对象不在同一个目录下,要写出地址
续写
--{% block ....%}
<div>我是续写的内容</div>
{% endblock %}
{% comment%}
{% block ... %}
<a href={% url "news" %}>
<h1>news</h1>
</a>
<a href={% url "sport" %}>
<h1>sport</h1>
</a>
{% endblock%}
{%endcomment%}
自动转义
为了防止恶意网站,采用on,不使用off
如果使用off我们就发现变成了一个链接
{% block "content"%}
{% autoescape on%}
<h1>{{ qq }}</h1>
{% endautoescape%}
{% endblock%}
这里的判断语句有格式要求 if age > 18,一个空格都不能少
block content"%}
{% if age > 18 %}
<h1>你可以访问本网站的内容</h1>
{%else%}
<h1>你不到法定年龄</h1>
{%endif%}
{% endblock%}
#for empty
{% block content %}
{% for book in books%}
<h1>{{book}}</h1>
{% endfor %}
{% for k,v in menu.items%}
<h1>{{k}}:{{v}}</h1>
{% endfor %}
{% for f in empty%}
<h1>{{f}}</h1>
{% empty %}
<h1>Sorry</h1>
<h1>{{error}}</h1>
{% endfor %}
{% endblock %}
{{}}里面的内容表示这是一个变量
利用with,在多次调用长包含的语句时更方便,但如果超出with模块,就没用了
{% block content%}
<h1>{{books.python.django}}</h1>
{% with books.python.django as t%}
<h1>{{t}}</h1>
{% endwith %}
{%endblock%}
{% block content%}
<h1>{{value|add:plus}}</h1>
<h1>{{value|add:a}}</h1>
<h1>{{a|add:b}}</h1>
<h1>{{c|add:d}}</h1>
{%endblock%}
结果:
600
Djangopython
['a', 'b', 'c', 'd']
由结果可知,同类型的相加可以,如果不同类型,会返回一个空字符串,即不显示
**index.html**
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
<link rel="stylesheet" href={% static "css/index.css" %}>
</head>
<body>
<h1>
这是news里的
模板变量{{scheme}}
#这就是调用的方式
{{book_list.book_name}}
# 调用都是用‘.’
</h1>
<div class="nav">nav</div>
<div class="content">content</div>
<div class="footer">footer</div>
</body>
</html>
**news.py**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>这是新闻的页面</h1>
</body>
</html>
**sport.html**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>这是体育的页面</h1>
</body>
</html>