我在我的main project
目錄中有一個pdf文件。我應該如何在主urls.py文件中添加此文件,以便將此文件鏈接到<a href>
標記中。在Django的網址中包含pdf文件
編輯
我得到兩個日期,開始和結束日期,通過AJAX。我處理這兩個日期的數據,生成一個報告,然後返回一個HttpResponse。 PDF報告現在保存在我的主項目目錄中。現在我收到了AJAX的回覆。那麼,現在我該如何處理成功功能中的響應,從服務器發回並打開PDF文件。
謝謝。
jQuery的
$(function() {
$("#report_submit").click(function(){
$.ajax({
type : "POST",
url: "/reports/",
data : { 'start_date' : $("#startDate").val() , 'end_date' : $("#endDate").val() },
success : function(result){
},
error : function(result){
}
});
});
});
的Django視圖代碼
def generate_report(request):
ctx = {}
if request.is_ajax():
if request.POST.has_key('start_date'):
start_date = datetime.strptime(request.POST[ 'start_date'] , '%m/%d/%Y')
end_date = datetime.strptime(request.POST[ 'end_date'] , '%m/%d/%Y')
......
# PDF GENERATED in MAIN PROJECT DIRECTORY
with open(os.path.join(os.path.dirname(__file__),'../../../../gui','Report.pdf')) as pdf:
response = HttpResponse(pdf.read(), content_type='application/pdf')
response['Content-Disposition'] = 'inline;filename=Report.pdf'
return response # so, now when I send a response back, how should I process it in AJAX success function?
pdf.closed
return render(request, 'generate_report/reports.html', ctx)
我試過了。 (os.path.join(os.path.dirname(__ file __),'../../../gui','Report.pdf'))as pdf: \t \t \t \t \t響應= HttpResponse(pdf.read(),content_type ='application/pdf') \t \t \t \t response ['Content-Disposition'] ='inline; filename = Report。PDF」 \t \t \t \t迴響應 \t \t \t pdf.closed' – PythonEnthusiast
它仍然無法獲取客戶端的PDF文件。 – PythonEnthusiast
看到我編輯的問題。謝謝。 – PythonEnthusiast