django中如何產生非HTML格式的內容。,django
某些時候可能有這樣的需求,在網頁中點擊一個連結或者一個按鈕希望返回一張圖片、一個pdf文檔、一個csv文檔等而非HTML。在diango中很容易做到這些。django中的view用來接收http request並返回web response。通常情況下,返回的內容為HTML,但其能夠返回的不僅僅如此,還可以是上述圖片、pdf檔案等等。返回非HTML形式的內容的關鍵在於HttpResponse這個類,尤其是mimetype這個參數,通過將此參數設定為不同的值可以提示瀏覽器view返回了不同格式的內容。比如,想要返回圖片內容,只需讀如一張圖片,然後在HttpResponse中指明圖片的mimetype並將圖片內容作為另一參數response給瀏覽器,瀏覽器能夠自動正確的顯示圖片內容。
from django.http import HttpResponsedef my_image(request): image_data = open("/path/to/my/image.png", "rb").read() return HttpResponse(image_data, mimetype="image/png")另外一個需要特別注意的的是HttpResponse對象實現了Python的標準“file-like-object”API,也即可以將HttpResponse當做檔案使用。
例子:
產生CSV格式的內容
import csvfrom 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
需要注意的幾點:
1.HttpResponse中mimetype指定為了'text/csv'告知瀏覽器返回的文檔是CSV檔案。
2.HttpResponse設定了另外一個參數Content-Disposition其中attachment告知瀏覽器儲存返回的文檔而非顯示其內容,filename指明了返迴文檔的名字,改名字可任意指定。
3.因為csv的writer方法期望一個檔案類型的對象作為參數,而HttpResponse執行個體可以當做檔案使用,所以可以直接在csv模組的writer方法中將HttpResponse作為參數。
4.writer.writerow方法負責往檔案中寫入一行內容。
上述方法是返回非HTML格式內容的通用模式,也即:建立一個特定MIME Type的HttpResponse,將其傳遞給以檔案為參數產生特定格式的文檔的方法,之後返回該response。
產生PDF格式的內容
from reportlab.pdfgen import canvasfrom django.http import HttpResponsedef 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. # See the ReportLab documentation for the full list of functionality. p.drawString(100, 100, "Hello world.") # Close the PDF object cleanly, and we're done. p.showPage() p.save() return response
流程基本同上,需要注意的幾點:
1.此處使用了 application/pdf MIME type告知瀏覽器返回的是PDF檔案,而非HTML,否則瀏覽器會將其作為普通HTML內容處理。
2.canvas.Canvas方法期望一個file-like的對象作為參數,將HttpResponse傳遞給該方法。
3.使用Canvas執行個體的方法繪製PDF文檔,調用showPage()方法和save()方法(否則會產生損壞的pdf文檔)。
4.最後返回該HttpResponse執行個體
產生更為複雜的PDF文檔,這裡使用了cStringIO庫來臨時存放PDF檔案
from cStringIO import StringIOfrom reportlab.pdfgen import canvasfrom django.http import HttpResponsedef hello_pdf(request): # Create the HttpResponse object with the appropriate PDF headers. response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=hello.pdf' temp = StringIO() # Create the PDF object, using the StringIO object as its "file." p = canvas.Canvas(temp) # Draw things on the PDF. Here's where the PDF generation happens. # See the ReportLab documentation for the full list of functionality. p.drawString(100, 100, "Hello world.") # Close the PDF object cleanly. p.showPage() p.save() # Get the value of the StringIO buffer and write it to the response. response.write(temp.getvalue()) return response
其他可能的格式
實質上,任何可以寫檔案的Python庫都可與Django的HttpResponse結合用以返回特定格式的內容,如ZIP檔案、動態圖片、圖表、XLS檔案等等。
最後在看一個返回xls檔案的例子
from django.http import HttpResponseimport xlwtdef viewXls(request): response = HttpResponse(mimetype='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=request.xls' book = xlwt.Workbook(encoding='utf8') sheet = book.add_sheet('untitled') for row, column, value in ((0,0,1),(0,1,2),(1,0,3),(1,1,4)) sheet.write(int(row),int(column),value) book.save(response) return response流程同上,不在注釋。
另外,需要特別注意的是,這裡的request必須是通過表單提交才能正確返回特定格式的內容,若要是通過ajax方式發起的request則返回的內容會被當做文本串處理,而不能被瀏覽器解釋為特定內容。
比如:
$.ajax({ url:"{% url 'mycitsm.views.viewXls' %}", data:postData, type:"POST", success:function(result){ }, });//是不可以的,而要使用如下的表單提交才可以:var form = $("#xlsForm");form.attr({action:"{% url 'mycitsm.views.returnXls' %}",method:"POST" });form.submit();說到這裡有必要記錄一下開發過程中遇到的一個問題,也即將表單內容序列化為字串的問題。
有時需將表單中的所有內容序列化為鍵值對構成的串做為一個整體進行URL參數傳遞,而且需要對值中包含的特殊字元進行編碼。比如有如下表單:
<form> <div><input type="text" name="a" value="1" id="a" /></div> <div><input type="text" value="2" id="b" /></div> <div><input type="hidden" name="c" value="3" id="c" /></div> <div> <textarea name="d" rows="8" cols="40">4</textarea> </div> <div><select name="e"> <option value="5" selected="selected">5</option> <option value="6">6</option> <option value="7">7</option> </select></div> <div> <input type="checkbox" name="f" value="8" id="f" /> </div> <div> <input type="submit" name="g" value="Submit" id="g" /> </div></form>$('form').submit(function() { alert($(this).serialize()); return false;});#可以輸出a=1&c=3&d=4&e=5為什麼第二個text類型的input的值還有checkbox類型的input的值以及submit類型的input沒有被序列化呢?這是因為如果要表單元素的值包含到序列字串中,元素必須使用 name 屬性。而第二個text類型的input無name屬性。checkbox類型的input有一個並沒有被checked所以……。serialize()只會將”成功的控制項“序列化為字串。如果不使用按鈕來提交表單,則不對提交按鈕的值序列化,所以submit類型的input沒有被序列化。
當然除了直接對整個form序列化外還可對已選取的個別表單元素的jQuery對象序列化,如<input>,<textarea>等等。