• [django]Django输出页面方式的补充


    1、直接输出 -- HTTPResponse

    helloworld.py

    from django.utils.httpwrappers import HttpResponse
    def index(request):
     
    return HTTPResponse('''<html><head></head><body>Hello world</body></html>''');
    def out(request):
     response 
    = HttpResponse(mimetype='text/csv')
     response[
    'Content-Disposition'= 'attachment; filename=1.txt'
     response.write(
    'abcdef')
     
    return response

    urls.py

    urlpatterns 
    = patterns('',
        (r
    '^$''test.helloworld.index'),
    )

    2. MTC -- render_to_response

    helloworld.py (view)

    #coding=utf-8
    from django.core.extensions import render_to_response
    def index(request):
            
    return render_to_response('helloworld', {'params': {'a':1'b':2} } )

    helloworld.html (template)

    <html>
    <body>
      
    <table>
      {% for key in params%}
      
    <tr><td> {{key}} </td><td> {{params.key}} </td></tr>
      {% endfor %}
      
    </table>
    </body>
    </html>

    以上是One Piece总结的,下面我再补充一个,其实这个是第一种的变样

    3、从template加载

    from django.template import loader, Context
    = loader.get_template('common/post_note.htx')
    = Context({'action': request.path , 'title': consts.ADD_TOPIC})
    return HttpResponse(t.render(c))


     

  • 相关阅读:
    win7下设置smtp的方法
    win7下怎么安装IIS
    python语法笔记(二)
    python语法笔记(一)
    python 的类变量和对象变量
    mysql使用笔记(四)
    mysql使用笔记(三)
    mysql使用笔记(二)
    windows下重新安装TCP/IP协议栈
    c++程序编码
  • 原文地址:https://www.cnblogs.com/maplye/p/496239.html
Copyright © 2020-2023  润新知