`
maplye
  • 浏览: 116688 次
  • 来自: ...
社区版块
存档分类
最新评论

[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))


 

分享到:
评论

相关推荐

    编写第一个 Django 应用1.pdf

    23. Django社区和资源:Django拥有一个活跃的开发者社区,提供了许多有用的资源,包括教程、论坛讨论和第三方库,这些都是学习和提高Django开发技能的重要补充。 24. 项目开发流程:从创建项目、定义模型、编写视图...

    archive_ Django博客系统 v3.2 正式版 [江西新余电信].zip.zip

    【描述】"archive_ Django博客系统 v3.2 正式版 [江西新余电信].zip" 是对标题的补充,进一步确认了压缩文件的内容——一个基于Django框架构建的博客系统,版本号为3.2,被标识为正式发行版,且与江西新余电信有关,...

    Django模板标签{% for %}循环,获取制定条数据实例

    补充一点,如果你需要在前端页面上生成连续的序号,可以使用`forloop.counter`。在for循环中,你可以直接输出`forloop.counter`来得到当前循环的序号。例如: ```html {% for inrow in insocket_list %} &lt;td&gt;{...

    基于jsp的超市管理系统源码数据库.doc

    - **E-R图**:通过图形方式直观地展示了实体及其联系,为数据库设计提供了参考依据。 #### 五、系统详细设计 **5.1 数据库的逻辑设计** - 详细介绍数据库表结构、字段定义以及约束条件等内容。 - 示例:商品信息...

Global site tag (gtag.js) - Google Analytics