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

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. intellij idea 热部署失效,需要手动编译类

    从网上看到的解决方案,做一下备忘: spring boot项目中遇到jrebel类需要手动编译才会触发热部署的问题(spring boot devtools一样的问题) 1.ctl + shift + ...

  2. Android自定义扁平化对话框

    平时我们开发的大多数的Android.iOS的APP,它们的风格都是拟物化设计.如今Android 4.X.iOS 7.WP8采用的是扁平化设计,可以看出扁平化设计是未来UI设计的趋势.其实扁平化设计 ...

  3. redis数据结构HyperLogLog

    如果我们要实现记录网站每天访问的独立IP数量这样的一个功能 集合实现: 使用集合来储存每个访客的 IP ,通过集合性质(集合中的每个元素都各不相同)来得到多个独立 IP ,然后通过调用 SCARD 命 ...

  4. 3.4 C与汇编程序的相互调用

    为了提高代码执行效率,内核源代码中有些地方直接使用了汇编语言编制.这就会涉及在两种语言编制的程序之间相互调用的问题. 函数调用包括从一块代码到另一块代码之间的双向数据传递和执行控制转移.数据传递通过函 ...

  5. input 类型为number型时,maxlength不生效?

    input 类型为number型时,maxlength不生效? 可以加oninput属性来控制最大长度:<input id="numInput" type="num ...

  6. 【PHP】新浪、淘宝的地区 API调用

    /推荐使用新浪的: $ch = curl_init(); $url = "http://ip.dpool.sina.com.cn/iplookup/iplookup.php?format=j ...

  7. C++通过OCCI操作Oracle数据库详解

    1.安装OCCI 如果oracle数据库默认没有安装OCCI,可以自己从官网上下载与自己数据库版本一致的API,其中包含四个软件包: oracle-instantclient-sqlplus-10.2 ...

  8. UIBezierPath

    UIBezierPath 笔者在写本篇文章之前,也没有系统学习过贝塞尔曲线,只是曾经某一次的需求需要使用到,才临时百度看了一看而且使用最基本的功能.现在总算有时间停下来好好研究研究这个神奇而伟大的贝塞 ...

  9. rsync学习

    echo "aabb" > pswd pswd rsync -Cvaz assert.awk stat@59.151.37.17::stat/read/aabb/assert ...

  10. PHP常用字符串函数

    1:访问子字符串:strpos()2:提取子字符串:substr()3:逐字节处理字符串:strstr()4:计算字符串长度:strlen()5:替换子字符串:substr_replace()6:按字 ...