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 %}