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

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. 关于List、Set集合以及Map的使用

    package tingjizifu; import java.util.*; public class TongJi { /* * 使用Scanner从控制台读取一个字符串,统计字符串中每个字符出现 ...

  2. flowplayer+flashhls使用过程中发现的一些小问题

    flashls里边有好几套代码,主要看生成路径,其中flowplayer用了flashls.swc,flashls.swc使用的代码在这里:/src/org/mangui/hls,所以要注意,当搜索代 ...

  3. 使用Jstl异常:The absolute uri: http://java.sun.com/jsp/jstl/core cannot&nbs

    错误提示是:        org.apache.jasper.JasperException: This absolute uri http://java.sun.com/jsp/jstl/core ...

  4. 大小写转换,split分割

    一.大小写转换 1.定义和用法 toUpperCase() 方法用于把字符串转换为大写. toLowerCase() 方法用于把字符串转换为小写.    用法: stringObject.toUppe ...

  5. oldboy第十三天学习

    1.现在给我的感觉是,python终于入门了开始越学越简单了.变得更好理解了. 一.memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它 ...

  6. Sicily 1034. Forest

    题目地址:1034. Forest 思路: 网上很多说用深搜,很任性.......发现广搜也挺好用的,实验课打的(⊙o⊙)…orz........囧. 先找根结点,根据根结点广搜深度,广搜宽度,不过要 ...

  7. PHP 类中的常量

    类中的常量与静态成员类似他们只属于类而不属于类的任何实例,访问形式与访问静态成员一样. 例如: <?php class MyConst{ const RED="Red"; c ...

  8. uva 10007 Count the Trees

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. layer iframe层的使用,传参

    父层 <div class="col-xs-4 text-left" style="padding-left: 50px;"><button ...

  10. vijos国庆节模拟赛之繁星春水

    A.闪烁的繁星 题目:https://vijos.org/p/1881 题解:貌似做过小白逛公园或者序列操作都可以秒出吧,就是pushup函数比较麻烦,不过仔细想一想就知道了. 代码: #includ ...