django 模板
2015-08-12 16:18
309 查看
django 模板
1. 一般步骤
django startproject test01django startapp blog
vim test01/setting.py 把blog那个app给安装上去。
vim test01/urls.py 添加一个url 映射到views的函数中。
mkdir blog/templates 添加模板目录
vim blog/templates/index.html 添加模板文件
vim blog/views.py 在里面添加函数,
2. views
[code]from django.template import loader,Context from django.http import HttpResponse def index(req): t = loader.get_template('index.html') c = Context({'user':'aca'}) html = t.render(c) return HttpResponse(html)
分了4步
用loader的get_template方法来取一个template对象。
生成一个Context对象,可以用dict初始化他。
将模板渲染出来,是一个字符串。
最好将字符串用HttpResponse函数来返回给client。
上面4步能简化成一步
[code]from django.shortcuts import render_to_response def index2(req): return render_to_response('index.html',{'user':'aca'})
或者是:
[code]from django.shortcuts import render def index2(req): return render(req,'index.html', {'user':'gqdw'} )
详细可以看官网:
https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/
3. template
就很简单了,类似于jinja2 ,也可以加if ,或者循环。详细用法还是参见官网:https://docs.djangoproject.com/en/1.8/ref/templates/language/
相关文章推荐
- 修改u-boot的开机logo及显示过程
- POJ 1179 Polygon
- Google Earth谷歌地球卫片下载器--可下载全球历史卫星,无偏移、高精度
- Codeforces 569 A. Music ( 模拟 )
- Google Gson 使用简介
- MonogoDB的GirdFS
- 使用Go语言简单模拟Python的生成器
- UVA 11383 Golden Tiger Claw 金虎爪(KM算法)
- Gonet2 游戏服务器框架解析之Agent(3)
- Migo收货增强
- Algorithms—129.Sum Root to Leaf Numbers
- 江苏电信欢go与省公安厅合作保无线上网安全
- CodeForces 568A. Primes or Palindromes?( 素数 + 回文数 )
- [Data Structure & Algorithm] Hash那点事儿
- GoogleGoogleGoogle!!!! 百度云资源
- golang 数组切片(一)
- Django-视图和URl配置
- django-模版
- Google Earth谷歌地球卫片下载器--可下载全球历史卫星,无偏移、高精度
- POJ 2007 Scrambled Polygon(凸包)