【django 学习笔记】12 输出非HTML内容
Django
拥有一些
便利的内建工具帮助你生成常见的非HTML
内
容:
RSS/Atom
聚合文件
站点地图 (
一个XML
格式文件,
最初由Google
开发,
用于给搜索引擎提示线索)
基础:
视图和MIME
类型
从一个视图返回一个非 HTML
内
容的关键是在构造一个 HttpResponse
类
时,
需要指定 mimetype
参数。 通
过改变 MIME
类型,
我们可以通知浏览器将要返回的数据是另一种类
型。
from django.http
import HttpResponse
def my_image(request):
image_data =
open(“/path/to/my/image.png”, “rb”).read()
return
HttpResponse(image_data, mimetype=”image/png”)
生成 CSV
文件
import csv
from django.http
import HttpResponse
# Number of unruly
passengers each year 1995 – 2005. In a real application
# this would likely
come from a database or some other back-end data store.
UNRULY_PASSENGERS =
[146,184,235,200,226,251,299,273,281,304,203]
def
unruly_passengers_csv(request):
# Create the
HttpResponse object with the appropriate CSV header.
response =
HttpResponse(mimetype=’text/csv’)
response['Content-Disposition']
= ‘attachment; filename=unruly.csv’
# Create the CSV
writer using the HttpResponse as the “file.”
writer =
csv.writer(response)
writer.writerow(['Year',
'Unruly Airline Passengers'])
for (year, num) in
zip(range(1995, 2006), UNRULY_PASSENGERS):
writer.writerow([year,
num])
return response
生成 PDF
文件
安装 ReportLab
from reportlab.pdfgen
import canvas
from django.http
import HttpResponse
def
hello_pdf(request):
# Create the
HttpResponse object with the appropriate PDF headers.
response =
HttpResponse(mimetype=’application/pdf’)
response['Content-Disposition']
= ‘attachment; filename=hello.pdf’
# Create the PDF
object, using the response object as its “file.”
p =
canvas.Canvas(response)
# Draw things on the
PDF. Here’s where the PDF generation happens.
# S
相关文档:
根据我最近的一些实践以及在和一些读者进行关于HTML表格的使用问题沟通之后,决定写这篇文章。总的来说,我注意到由于误导性信息,他们对于table的使用有种先入为主的厌恶。事实上很多人会说”我看到永远不应该使用表格”的说法,但是这绝对是错误的!这个建议只是针对使用HTML表格来定义网页 ......
众所周知,对于链接和图片,我们可以通过添加title
属
性以显示一些说明文字,一般情况下,这些文字都是显示成一行,那么有没有办法让它以多行的方式显示呢?解决的方法有两种:
1.将title
属
性分成几行来写,例如:
<a href=#" title
="说明一
说明二
说明
三">印象派</a> ......
框线制作常用代码
代码
含义
<table>...</table>
建立表格,所有的其他标记都需要在此标记中
<table width=* heigth=*></table>
设定表格宽度width和高度height,属性值可以使用点数,如:width=50,也可以使用百分比,如:width=50%。
<table bgcolor=*></ta ......
来自:http://9host.cn/html/20074221602392654.html
最近在搞网页编程,总结了frameset 的一些使用技巧,供大家参考哦,还是先剖析一下框架吧!
■ 框架标记
<FRAMESET> <FRAME>
<NOFRAMES>
<IFRAME>
欲明白本篇【HTML彻底剖析】之标记分类,请看 【标记一览】 ......