django: template using & debug
模板的作用方法有如下三种:
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的更多相关文章
- django.template.exceptions.TemplateDoesNotExist: login.html 错误处理
登陆Login界面时候报错 Internal Server Error: /login/ Traceback (most recent call last): File , in inner resp ...
- Django Template 进阶
回顾: Variables {{ var }} {{ dict.key }} {{ var.attr }} {{ var.method }} {{ varindex }} Filter {{ list ...
- Django 找不到模版报错" django.template.exceptions.TemplateDoesNotExist: index.html"
解决办法:在setting.py的TEMPLATES‘DIRS'[]加入模版路径 os.path.join(BASE_DIR, 'templates') TEMPLATES = [ { 'BACKEN ...
- django template 模板
九.Template模板 Template 模板是根据view传过来数据在html展示的功能,典型python 模板jinjia2库提供丰富的上下文展示func 创建template位置在项目下与ap ...
- Django template配置
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...
- 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 ...
- Django.template框架 template context (非常详细)
前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1,显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...
- django入门7之django template和xadmin常用技巧
django入门7之django template和xadmin常用技巧 <li {% ' == '/course' %}class="active"{% endif %}& ...
- django template
一.模板基本元素 1.例子程序 1)urls.py中新增部分 from django.conf.urls import patterns, url, include urlpatterns = pat ...
随机推荐
- BIOS+MBR模式 VS UEFI+GPT模式
EFI与MBR启动的区别 大硬盘和WIN8系统,让我们从传统的BIOS+MBR模式升级到UEFI+GPT模式,现在购买的主流电脑,都是预装WIN8系统,为了更好的支持2TB硬盘 ,更快速的启动win ...
- react-native-router-flux 下部导航
github url:https://github.com/aksonov/react-native-router-flux API: https://github.com/aksonov/react ...
- oracle中存储过程详解
oracle中存储过程的使用 过程是指用于执行特定操作的PL/SQL块.如果客户应用经常需要执行特定操作,那么可以考虑基于这些操作建立过程.通过使用过程,不仅可以简化客户应用的开发和维护,而且可以提高 ...
- java第一天的疑问
1字节 的 byte 2字节 的 char 精度 byte<short<char<int<long<float<double 随便打个整数默认为int 随便打个小数 ...
- nodejs学习记录
一.环境搭建 1.安装express 局部安装 npm i express 全局安装 npm i -g express (命令行express无法使用) 环境变量 npm i -g express-g ...
- 细说PHP优化那些事
我们在用PHP编程的时候,总是想要使自己的程序占用资源最小,运行速度更快,代码量更少.往往我们在追求这些的同时却失去了很多东西.下面我想讲讲我对PHP优化的理解.优化的目的是花最少的代价换来最快的运行 ...
- Nginx源码研究二:NGINX的事件处理概论
NGINX作为服务端的应用程序,在客户端发出数据后,服务端在做着这样一些处理,数据先会经过网卡,网卡会和操作系统做交互,经过操作系统的协议栈处理,再和不同的应用程序交互. 在这里面涉及两个概念,一个是 ...
- php explode 用法详解
定义和用法explode() 函数把字符串分割为数组. 语法explode(separator,string,limit)参数 描述 separator 必需.规定在哪里分割字符串.string 必需 ...
- Python中else语句块(和if、while、for、try搭配使用)
学过C/C++的都知道,else语句是和if语句搭配使用的, 但是在Python中,else语句更像是作为一个模块,不仅仅可以和if语句搭配,还可以和循环语句,异常处理语句搭配使用.下面逐个进行介绍: ...
- 文件磁盘读写类CArchive类
CArchive类的成员 数据成员 m_pDocument 指向被串行化的CDocument对象 构造函数 Carchive 创建一个Carhcive对象 Abort在不异常的情况下,关闭归档文件 C ...