Django模块变量(1)

1.修改index.html文件,添加两个变量title和user,代码如下:

html>
	<head>
		<title>{{title}}</title>
	</head>
	<body>
		<h1>{{user}}</h1>
	</body>
</html>

2.修改模块blog-->views.py,给变量赋值,设置Web页面标题为Django,以第一标题格式显示HelloWorld

代码如下:

1 from django.shortcuts import render_to_response
2 
3 # Create your views here.
4 
5 # from django.http import HttpResponse
6 def index(req):
7     # return HttpResponse('<h1>Hello Django</h1>')
8     return render_to_response('templates/index.html',{'title':'Django','user':'HelloWorld'})

3.urls.py和settings.py与上节模板文件导入一样,不做任何变化

4.运行Django服务器

5.在浏览器上访问http://localhost:8000/blog,页面效果如下:

猜你喜欢

转载自www.cnblogs.com/KSYoon/p/9689997.html