• 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>
    '
    >>>
  • 相关阅读:
    一个主板上连接两个都有引导的盘
    pytorch查看模型weight与grad
    linux终端窗口字体缩放快捷键
    vim选中多行缩进(python多行缩进)与删除多行前面的空格
    python import 包的路径以及相对路径加载的问题
    pycharm中添加PATH变量
    Atom选中多行操作
    php扩展 swoole的安装与使用
    12121212
    linux系统下清理所有Redis缓存
  • 原文地址:https://www.cnblogs.com/exclm/p/3350703.html
Copyright © 2020-2023  润新知