python 在Django中显示PDF

vnjpjtjt  于 2022-11-28  发布在  Python
关注(0)|答案(1)|浏览(252)

我需要在浏览器中显示PDF文件,但我找不到解决方案,以采取文件夹媒体的PDF,PDF文件保存在我的数据库中,但我无法显示。
我的urls.py:

urlpatterns = [
    path('uploadfile/', views.uploadFile, name="uploadFile"),
    path('verPDF/<idtermsCondition>', views.verPDF, name='verPDF'),
]

我的models.py:

class termsCondition(models.Model):

    title = models.CharField(max_length=20, verbose_name="title")
    uploadPDF = models.FileField(
        upload_to="PDF/", null=True, blank=True)
    dateTimeUploaded = models.DateTimeField(auto_now_add=True)

    deleted_at = models.DateTimeField(
        auto_now=False, verbose_name="Fecha eliminacion", blank=True, null=True)

class Meta:
    verbose_name = "termsCondition"
    verbose_name_plural = "termsConditions"

我的views.py:

def uploadFile(request):
    user = request.user
    if user.is_authenticated:
        if user.is_admin:
            if request.method == "POST":
                # Fetching the form data
                fileTitle = request.POST["fileTitle"]
                loadPDF = request.FILES["uploadPDF"]

                # Saving the information in the database
                termscondition = termsCondition.objects.create(
                    title=fileTitle,
                    uploadPDF=loadPDF
                )
                termscondition.save()

            else:
                listfiles = termsCondition.objects.all()[:1].get()
                return render(request, 'subirTerminos.html', context={
                    "files": listfiles
                })
        else:
            messages.add_message(request=request, level=messages.SUCCESS,
                                 message="No tiene suficientes permisos para ingresar a esta página")
        return redirect('customer')

    else:
        return redirect('login2')


def verPDF(request, idtermsCondition):
    user = request.user
    if user.is_authenticated():
        if user.is_admin:
            getPDF = termsCondition.objects.get(pk=idtermsCondition)
            seePDF = {'PDF': getPDF.uploadPDF}
            print(seePDF)
            return render(request, 'subirTerminos.html', {'termsCondition': getPDF, 'uploadPDF': getPDF.uploadPDF})
        else:
            messages.error(request, 'Do not have permission')
    else:
        return redirect('login2')

我的html:

<div>
    <iframe id="verPDF" src="media/PDF/{{ uploadPDF.url }}" 
    style="width:800px; height:800px;"></iframe>
  </div>

我想看看我的pdf,我不能做,我想知道如何做,我尝试了许多解决方案,我接受js,嵌入iframe什么都可以解决。

wztqucjr

wztqucjr1#

verPDF视图中,它应该是user.is_authenticated而不是user.is_authenticated(),而且我建议您将<idtermsCondition>更改为<int:idtermsCondition>,因为默认情况下(如果没有给定)它被视为字符串。
urls.py

urlpatterns = [
    path('uploadfile/', views.uploadFile, name="uploadFile"),
    path('verPDF/<int:idtermsCondition>/', views.verPDF, name='verPDF'),
]

并且{{uploadPDF.url}}已经具有url(媒体目录的完整路径),并尝试使用<embed>标记,因此:

<div>
    <embed id="verPDF" src="{{uploadPDF.url}}" width="500" height="375" type="application/pdf">
</div>

**注意:**始终在每条路线的末尾添加/

相关问题