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 ...
随机推荐
- PHP防注入转义功能
PHP addslashes() 函数 $str = addslashes('Shanghai is the "biggest" city in China.'); echo($s ...
- 思科27亿美元收购网络安全公司Sourcefire
据国外媒体报道,思科于今日宣布将以27亿美元的总价收购网络安全公司Sourcefire,以加强自身在网络安全业务领域的优势,该交易将于今年下半年完成. [IT商业新闻网讯](记者 张良)7月23日消息 ...
- jquery 20行代码实现简单轮播效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- css background-position:x% y%
规则1: background-position:xpx ypx; 为正值时:背景图片的左上顶点朝右下移动直至距离div的左上顶点为xpx ypx: 规则2: background-position: ...
- 【PHP】新浪、淘宝的地区 API调用
/推荐使用新浪的: $ch = curl_init(); $url = "http://ip.dpool.sina.com.cn/iplookup/iplookup.php?format=j ...
- HTML5+Css3-webkit-filter
-webkit-filter 现在规范中支持的效果有: grayscale 灰度 sepia 褐色 saturate 饱和度 hue-rotate 色相旋转 invert 反色 opacity 透明度 ...
- 使用C#版本GDAL读取复数图像
GDAL的C#版本虽然在很多算法接口没有导出,但是在读写数据中的接口基本上都是完全导出了.使用ReadRaster和WriteRaster方法来进行读写,同时对这两个方法进行了重载,对于常用的数据类型 ...
- C# AES加密解密算法
/// <summary> /// AES加密 /// </summary> /// <param name="encryptStr">明文&l ...
- Local System/Network Service/Local Service
// The name of the account under which the service should run// 1 NT AUTHORITY\\SYSTEM 2 NT AUTHORIT ...
- 8.2.1.4 Index Merge Optimization 索引合并优化:
8.2.1.4 Index Merge Optimization 索引合并优化: 索引合并方法是用于检索记录 使用多个 范围扫描和合并它们的结果集到一起 mysql> show index fr ...