0

I've a pdf file in one of my main project directory. How should I add this file in the main urls.py file so as to link this file in <a href> tag.

EDIT

I get 2 dates, start and end date, via AJAX. I process the data b/w those 2 dates, generate a report and then returns an HttpResponse. The PDF report is now saved in my main project directory. Now I get a response back in AJAX. So, now how should I process the response in the success function, sent back from the sever and open a PDF file.

Thanks.

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 view code

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)
Community
  • 1
  • 1
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

1 Answers1

1

You can do this in two ways depending upon the requirements of your app

  1. Add the pdf file to the static directory and serve it from there. After all pdf file is really just a static resource.

  2. However, in case you need to do some checks before serving the file to the user eg. allowing only the authenticated users to access it, then write a view that will do the necessary checking and then serve the contents of the file with appropriate response headers. See example here

Edit after the OP updated their question

Is ajax absolutely necessary here? In case it is I can think of following:

Since you mention a report is being generated, I will assume that non authenticated and no authorized users shouldn't be able to access the report.

One way is to store the generated pdf file at some location (outside the static dir) and also save a record of the name of the generated file and the id of the user who can access it in a database table. The request can then simply respond with the name of the file.

In the success callback of ajax, open the url of a view along with the filename as get param in a separate tab. This view will check if the user can access the file and serve it.

Again, all this complexity can be avoided if ajax is not a requirement

Community
  • 1
  • 1
naiquevin
  • 7,588
  • 12
  • 53
  • 62
  • I tried this. 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 pdf.closed` – Praful Bagai Dec 09 '13 at 08:46
  • It still not gets the pdf file in the client side. – Praful Bagai Dec 09 '13 at 08:49