[Answer]-Include a pdf file in urls in Django

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

Leave a comment