from django.http import HttpResponse
def download_file(request):
# Text file
#response = HttpResponse(mimetype='text/plain') #定义输出格式为txt
#response['Content-Disposition'] = 'attachment; filename=my.txt' #规定文件名字
#给txt写入内容
#response.write("aa/n")
#response.write("bb")
# PDF file
#在生成 PDF 文件之前,需要安装 ReportLab 库
#http://code.djangoproject.com/svn/django/branches/0.95-bugfixes/docs/outputting_pdf.txt
from reportlab.pdfgen import canvas
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
p = canvas.Canvas(response)
p.drawString(100, 100, "Hello world.")
p.showPage()
p.save()
return response