Include a pdf file in urls in Django

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


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.



$(function() {
      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')


            with open(os.path.join(os.path.dirname(__file__),'../../../../gui','Report.pdf')) as pdf:
                response = HttpResponse(, 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?

    return render(request, 'generate_report/reports.html', ctx)


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

Need Your Help

Highlight text similar to grep, but don't filter out text

linux grep command-line-interface

When using grep, it will highlight any text in a line with a match to your regular expression.

Playing music through entire app (game) - when to release MediaPlayer?

android android-activity release android-mediaplayer

I am making a simple game and I want to loop few songs when app is in foreground using MediaPlayer. Playback should stop when UI is not visible, so I decided to play it from activities and not a se...