zoukankan      html  css  js  c++  java
  • django 学习-5 模板使用流程

    首先在模板下建一个index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title></title>
    </head>
    <body>
    <h1> hello {{uname}}</h1>
    </body>
    </html>
    </body>
    </html>
    ~      

    1、from django.http import HttpResponse

         from django.template  import loader, Context     导入要用的东西

    def index(req):
            t = loader.get_template('index.html')
            c = Context({'uname':'ssj'})                生成一个context对象
            return HttpResponse(t.render(c))           返回相应的字符串

    2、、from django.template  import Template

    def index1(req):
            t = Template('<h1>hello {{uname}}</h1>')   直接通过template把里面的模板以字符串的形式提供出来
            c = Context({'uname':'ssj'})
            return HttpResponse(t.render(c))

    3、from django.shortcuts  import render_to_response

    def index2(req):
            return render_to_response('index.html',{'uname':'ssj1'})

    这是一个比较快捷的方式,后面花括号里的内容就相当于上面context的内容

  • 相关阅读:
    UVa 839 -- Not so Mobile(树的递归输入)
    UVa 548 -- Tree
    UVA 122 -- Trees on the level (二叉树 BFS)
    UVa679 小球下落(树)
    POJ 2255 -- Tree Recovery
    POJ 1451 -- T9
    POJ 2513 -- Colored Sticks
    STL -- heap结构及算法
    nginx利用try_files实现多个源
    nginx location的优先级
  • 原文地址:https://www.cnblogs.com/Icanflyssj/p/5128032.html
Copyright © 2011-2022 走看看