目录
1.创建模板目录
2.进行模板目录路径配置
3.加载使用模板
from django.template.loader import get_template
超链接技巧:
/ 当html标签的路径当中出现 / 代表路径分割,到那时如果开头出现代表根,否则从当前开始链接
4.完整操作过程
视图文件 views.py
有三种写法:
from django.http import HttpResponse
from django.template.loader import get_template
def index(request):
html=get_template("index.html")
context={"name":"王麻子"}
result=html.render(context)
return HttpResponse(result)
from django.shortcuts import render_to_response
def index(request):
name = "老张"
return render_to_response("index.html", locals())
from django.shortcuts import render_to_response
def index(request):
return render_to_response("index.html",{"name":"老刘"})
路由文件 urls.py
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is our index I am {{ name }}
<a href="/variable">按我呀</a>
</body>
</html>
启动服务器运行:
我大概说一下运行的整个过程:
1、通过浏览器向我们的服务器发起请求,请求内容http://127.0.0.1:8000/index/
2、请求传递给服务器,url开始匹配ip和端口之后的内容index/
3、匹配到index之后,调用了index函数
4、Index函数来自开发者在视图文件当做的定义
5.index函数把响应交给了网页index.html,此时浏览器会显示index.html的内容
6.当点击a标签,会匹配地址/variable
7.找到variable函数,执行,结束