我有一個存儲file_name,file_link,is_active和上傳字段的模式。我可以通過管理網站添加PDF,但我試圖讓用戶能夠從我創建的前端網頁的新選項卡上下載PDF或查看PDF。如何允許用戶在我的靜態文件夾中在線下載或查看PDF?
目前,我創建的視圖正在獲取每個PDF的信息,以便我可以顯示名稱,鏈接等。如果我將{{pdf.file_link}}放入其中,它不會執行任何操作。
我真的很感激在這方面獲得一些幫助,這樣我就可以採集我的輪子。謝謝你在前進+
編輯:! 型號:
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)
查看: 正如你可以在視圖中看到的,我只得到從模型的日期。我對實際的PDF尚未做任何特殊的處理。就是那個問題。
@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())
模板:
{% 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 %}
你應該添加一些代碼到這個問題。看了之後,我們可以幫助你! –
顯示您的models.py,views.py和some_template.html – TitanFighter
對不起,不添加代碼哈哈。 模型: –