模板的作用方法有如下三种:

blog/views.py:

from django.template import loader, Context, Template
from django.http import HttpResponse
from django.shortcuts import render_to_response as r2r def index(req):
t = loader.get_template('index.html')
c = Context({'uname':'eli'})
html = t.render(c)
return HttpResponse(html) def index1(req):
t = Template('<h1>hey {{uname}} welcome to Django !</h1>')
c = Context({'uname':'eli'})
return HttpResponse(t.render(c)) def index2(req):
return r2r('index.html', {'uname':'man'})

对应的 urls.py:

X
from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover() urlpatterns = patterns('',
# Examples:
# url(r'^$', 'csvt02.views.home', name='home'),
# url(r'^csvt02/', include('csvt02.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^index/$', 'blog.views.index'),
url(r'^index1/$', 'blog.views.index1'),
url(r'^$', 'blog.views.index2'),
)

另,可以利用 manage.py 中的 shell 调试应用:

[root@bogon csvt02]#  python manage.py shell
Python 2.7.5 (default, Sep 20 2013, 07:02:05)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.shortcuts import render_to_response as r2r
>>> r2r('index.html',{'uname':'eli'})
<django.http.response.HttpResponse object at 0xa05ce6c>
>>> from django.template import loader
>>> t = loader.get_template('index.html')
>>> t
<django.template.base.Template object at 0xa0628ec>
>>> from django.template import Context
>>> c = Context({'uname':'eli'})
>>> c
[{'False': False, 'None': None, 'True': True}, {'uname': 'eli'}]
>>> t.render(c)
u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml">\r\n<head>\r\n\t<title>Csvt02</title>\r\n\t</head>\r\n\t<body>\r\n\t\t<h1>Hi eli welcome to Django !</h1>\r\n\t</body>\r\n</html>\r\n'
>>>

django: template using & debug的更多相关文章

  1. django.template.exceptions.TemplateDoesNotExist: login.html 错误处理

    登陆Login界面时候报错 Internal Server Error: /login/ Traceback (most recent call last): File , in inner resp ...

  2. Django Template 进阶

    回顾: Variables {{ var }} {{ dict.key }} {{ var.attr }} {{ var.method }} {{ varindex }} Filter {{ list ...

  3. Django 找不到模版报错" django.template.exceptions.TemplateDoesNotExist: index.html"

    解决办法:在setting.py的TEMPLATES‘DIRS'[]加入模版路径 os.path.join(BASE_DIR, 'templates') TEMPLATES = [ { 'BACKEN ...

  4. django template 模板

    九.Template模板 Template 模板是根据view传过来数据在html展示的功能,典型python 模板jinjia2库提供丰富的上下文展示func 创建template位置在项目下与ap ...

  5. Django template配置

    TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...

  6. django.template.exceptions.TemplateSyntaxError: 'static' is not a registered tag library. Must be one of:

    在访问web页面时报错,详细日志如下: django.template.exceptions.TemplateSyntaxError: 'staticfiles' is not a registere ...

  7. Django.template框架 template context (非常详细)

    前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1,显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...

  8. django入门7之django template和xadmin常用技巧

    django入门7之django template和xadmin常用技巧 <li {% ' == '/course' %}class="active"{% endif %}& ...

  9. django template

    一.模板基本元素 1.例子程序 1)urls.py中新增部分 from django.conf.urls import patterns, url, include urlpatterns = pat ...

随机推荐

  1. 去掉eclipse上编辑时的提示

    用eclipse时,鼠标移到类上时会给出提示,如下图:

  2. JQUERY1.9学习笔记 之基本过滤器(一) 动态选择器

    动态选择器:animated Selector 描述:当选择器运行时,选择动态进程中的所有元素.(对动态进程起作用) jQuery( ":animated" ) 注释::anima ...

  3. CentOS6.4 GOOGLE chrome install

    yum install http://people.centos.org/hughesjr/chromium/6/i386/RPMS/chromium-28.0.1500.45-205727.i686 ...

  4. dede 删除栏目文章后, 让ID从1开始

    1)删除所有栏目,新建ID从1开始: ALTER TABLE `dede_arctype` AUTO_INCREMENT =1; 2)删除所有文章,新发布文章ID从1开始: ALTER TABLE ` ...

  5. Python 改变当前工作目录

    在shell里编辑: import os os.getcwd()   #取得当前工作目录 os.chdir(r'目标目录')   # os.chdir(r'd:\learnpy')

  6. 使用Statement执行sql语句

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java ...

  7. 在同一上机器上建立两个SVN服务

    最快的三步: 1,SVNADMIN CREATE NAME 2, 改写CONF目录下的相关三个文件. 3,重写一个SVN的启动脚本,指定这个SVN不同的目录及端口号. [general] ### Th ...

  8. FE: CSS固定图片显示大小及GitHub Pages在线演示

    CSS固定图片显示大小 分析 假设图片区域的大小固定为250×300px,那么我们可以写出如下的样式 .picture-area { width: 250px; height: 300px; marg ...

  9. tigerVNC远程桌面,跨内网

    tigerVNC的简单使用教程(CentOS的远程桌面连接) 1.环境和软件准备 (1) CentOS 6.5下 [root@localhost ~]$ yum install tigervnc (2 ...

  10. COJ 0981 WZJ的数据结构(负十九)树综合

    WZJ的数据结构(负十九) 难度级别:E: 运行时间限制:3500ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ的数据结构中有很多都是关于树的.这让很多练习 ...