1

I have a modal that stores the file_name, file_link, is_active and an uploaded on field. I can add the PDF via the admin site but I am trying to allow the users the ability to download the PDF or view the PDF on a new tab from the front end webpage that I created.

Currently, the view that I created is getting the information for each PDF so that I can display the name, link, etc. If I put the {{ pdf.file_link }} into an it doesn't do anything.

I would really appreciate getting some assistance on this so that I can stope spinning my wheels. Thank you in advance!+

EDIT: Models:

class HelpfulPDF(models.Model):
    file_name = models.CharField(max_length=256)
    file_link = models.FileField(upload_to='pdfs')

    uploaded_on = models.DateField(auto_now=True)

    is_active = models.BooleanField(default=True)

    def __str__(self):
        return "{} - {}".format(self.id, self.file_name)

View: As you can see in the view, I am only getting the date from the model. I am not doing anything special with the actual PDF yet. That is the question.

@login_required()
    def help_center_view(request):
    data = dict()
    data['userprofile'] = request.user.userProfile
    data['get_pdfs'] = HelpfulPDF.objects.filter(is_active=True)

    return render(request, 'help_center.html', data.items())

Template:

{% extends 'base.html' %}

{% load staticfiles %}

{% block content %}
    <hr>
        <div class="text-center">
            <b>Please look at the below PDF's.</b> <i>(You can view   in browser or download to your computer)</i>
        </div>
    <hr>
    <br><br>
    <div class="text-center">
    {% if get_pdfs %}
        {% for each_pdf in get_pdfs %}
            <div style="display: inline-block; width:275px; height: auto; border: thin grey solid; border-radius: 15px ">
                <br>
                {% if each_pdf.file_name %}
                    <h6>{{ each_pdf.file_name }}</h6>
                {% else %}
                    <h6>Helpful PDF</h6>
                {% endif %}
                <hr>
                <div style="margin-top: 13em">
                    <hr>
                    <button class="btn-primary btn-sm">Download PDF</button>
                    <a href="#"><button class="btn-primary btn-sm">View PDF Online</button></a>
                </div>
                <br>
            </div>
        {% endfor %}
    {% else %}
        <h4>There are no PDF's to display at this time.</h4>
    {% endif %}


    </div>
{% endblock %}

2 Answers2

0

If your link to the pdf file works (try going to it in your browser and see what happens), then it should be possible to do the equivalent of this in your template:

<a href={{ your_link }}></a>

Add the target="_blank" attribute to have it open in another tab.

You can add a download attribute to the anchor element if you want it to download on click.

The browser should handle opening or downloading the PDF automatically. If not, then either your link is not valid (the most likely cause) or (much less likely because you'd probably know it) your server is sending the wrong content-type header in the response (which you can inspect from the browser dev console in the Network tab on Chrome).

j3py
  • 1,119
  • 10
  • 22
0

You can create a view to proxy the PDF files and return specific headers, see the following answer: How to show a PDF file in a Django view

Community
  • 1
  • 1
James Lin
  • 25,028
  • 36
  • 133
  • 233