Python系列视频教程: Django【13讲】第六讲 模板基本使用
2014-02-12 22:04
1041 查看
Python系列视频教程: Django【13讲】第六讲 模板基本使用
一.
step1:我们创建一个项目
python %django_home%\bin\django-admin.py startproject csvt02
step2:再创建个应用
进入到项目目录
cd csvt02
创建一个app
D:\test\mysite\csvt02>python %django_home%\bin\django-admin.py startapp blog
step3:
修改settings.py
加入blog应用
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-cn'
#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
step4:使用templates
在blog目录里面创建子目录templates
创建index.html
step5:修改urls.py
url(r'^index/$', 'blog.views.index'),
step6:修改views.py-使用loader.get_template
加入相应的view方法
#from django.shortcuts import render_to_response
from django.template import loader,Context
from django.http import HttpResonse
# Create your views here.
def index(req):
t=loader.get_template('index.html')
c=Context({'uname':'alen'})
html=t.render(c)
return Httpresponse(html)
step7:运行开发服务器
D:\test\mysite\csvt02>python .\manage.py runserver
http://127.0.0.1:8000/index/
测试通过
ipython manage.py shell
二 使用模板的几种方式
使用模板的第一种方式
加载模板
获得数据
模板渲染数据
输出
第二种方式
url(r'^index1/$', 'blog.views.index1'),
def index1(req):
t=Template('<h1>hello {{uname}}</h1>')
c=Context({'uname':'csvt'})
return HttpResponse(t.render(c))
http://127.0.0.1:8000/index1/
url(r'^index2/$', 'blog.views.index2'),
from django.shortcuts import render_to_response
def index2(req):
return render_to_response('index.html',{'uname':'csvt quickly'})
http://127.0.0.1:8000/index2/
一.
step1:我们创建一个项目
python %django_home%\bin\django-admin.py startproject csvt02
step2:再创建个应用
进入到项目目录
cd csvt02
创建一个app
D:\test\mysite\csvt02>python %django_home%\bin\django-admin.py startapp blog
step3:
修改settings.py
加入blog应用
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-cn'
#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
step4:使用templates
在blog目录里面创建子目录templates
创建index.html
step5:修改urls.py
url(r'^index/$', 'blog.views.index'),
step6:修改views.py-使用loader.get_template
加入相应的view方法
#from django.shortcuts import render_to_response
from django.template import loader,Context
from django.http import HttpResonse
# Create your views here.
def index(req):
t=loader.get_template('index.html')
c=Context({'uname':'alen'})
html=t.render(c)
return Httpresponse(html)
step7:运行开发服务器
D:\test\mysite\csvt02>python .\manage.py runserver
http://127.0.0.1:8000/index/
测试通过
hello alen
老师这个时候使用了ipythonipython manage.py shell
二 使用模板的几种方式
使用模板的第一种方式
加载模板
获得数据
模板渲染数据
输出
第二种方式
url(r'^index1/$', 'blog.views.index1'),
def index1(req):
t=Template('<h1>hello {{uname}}</h1>')
c=Context({'uname':'csvt'})
return HttpResponse(t.render(c))
http://127.0.0.1:8000/index1/
hello csvt
第三种快速方法:url(r'^index2/$', 'blog.views.index2'),
from django.shortcuts import render_to_response
def index2(req):
return render_to_response('index.html',{'uname':'csvt quickly'})
http://127.0.0.1:8000/index2/
hello csvt quickly
相关文章推荐
- Python系列视频教程: Django【13讲】第七讲 数据库使用1
- Python系列视频教程: Django【13讲】第八讲 数据库使用2
- Python系列视频教程: Django【13讲】第九讲 数据库使用多对一映射
- Python系列视频教程: Django【13讲】第13讲 表单使用
- Python系列视频教程: Django【13讲】第二讲 模板文件的导入
- Python系列视频教程: Django【13讲】第三讲 模板变量的使用
- Python系列视频教程: Django【13讲】第一讲 Django安装
- Python系列视频教程: Django【13讲】第五讲 URL配置
- Python系列视频教程: Django【13讲】第十讲 数据库管理
- Python系列视频教程: Django【13讲】第11讲 数据库多对多映射
- Python系列视频教程: Django【13讲】第12讲 数据展示
- Python系列视频教程: Django【13讲】第四讲 运算符-表达式
- Python+Django+SAE系列教程5-----使用存储器cpickle
- python框架之django使用系列教程(1),原创,转载请注明出处
- Python Twisted系列教程13:使用Deferred新功能实现新客户端
- Django系列教程:五、模板的使用
- Python+Django+SAE系列教程10-----Django的模板
- python django的模板的基本使用方式
- Python+Django+SAE系列教程1-----Python环境和基本语法
- Python+Django+SAE系列教程10-----Django模板