zoukankan      html  css  js  c++  java
  • 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">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>Csvt02</title>
    	</head>
    	<body>
    		<h1>Hi eli welcome to Django !</h1>
    	</body>
    </html>
    '
    >>>
  • 相关阅读:
    Leetcode NO.110 Balanced Binary Tree 平衡二叉树
    Leetcode NO.226 Invert Binary Tree 翻转二叉树
    Leetcode NO.215 Kth Largest Element In An Array 数组中的第K个最大元素
    根据特征的浏览器判断
    Cygwin在打开在当前目录
    【转帖】科学对待 健康养猫 打造快乐孕妇
    解决chrome浏览器安装扩展、应用程序一直处在“检查中”的问题
    对【SQL SERVER 分布式事务解决方案】的心得补充
    关于“点击这里继续访问您选择的百度XXX”
    VBA一例:如何保持文本框焦点
  • 原文地址:https://www.cnblogs.com/exclm/p/3350703.html
Copyright © 2011-2022 走看看