上次提到了进行html的地址链接,即在views中增加一个add2,在urls中增加这个add2的对应目录结构
urls中的目录结构使用了正则表达,\d代表一个数字,+代表一个或多个前面的字符,写在一起\d+就是一个或多个数字
=========================================================================================
今天学习了模板的使用
在blog新建目录templates,render会去该app下的templates目录下查找对应的html
from django.shortcuts import render_to_response,render def index(req): return render(req,'index.html') 这样就可以直接在index.html文件修改网页显示了 也可以对在views,中增加变量如一个字符变量
views: def index(req): return render(req,'index.html',{'user':"Larson"})
在html增加变量增加{{ string }}
<html> <head> <title>MY BLOG</title> </head> <body> <h1>LarsonGo</h1> <p>########################################################</p> <p>Introduction:</p> </p>my name is {{user}}</p> </body> </html> 这样就可以显示姓名了,当然也可以显示字典的内容 如下:
views: def index(req): #user=["Larson"] user={'name':'Larson','sex':'male','age':'24'} return render(req,'index.html',{'user':user})
html 文件 <html> <head> <title>MY BLOG</title> </head> <body> <h1>LarsonGo</h1> <p>########################################################</p> <p>Introduction:</p> </p>my name is {{user.name}},I am {{user.age}} years old</p> </body> </html>