Django不显示来自数据库的pdf

kdfy810k  于 2023-03-20  发布在  Go
关注(0)|答案(1)|浏览(109)

学习Django并希望在HTML页面上显示来自数据库的PDF文档。但我有错误:无法连接到站点,ERR_NAME_NOT_RESOLVED。
models.py

class Certificate(models.Model):
    title = models.CharField(max_length=30, null=True)
    pdf = models.FileField(upload_to='pdf')
    status = models.BooleanField(default=True)

    class Meta:
        verbose_name_plural='09. Certificates'

views.py

def certificates(request):
    certificates = Certificate.objects.filter(status=True).order_by('-id')
    return render(request, 'certificates.html', {'certificates': certificates})

certificates.html

{% block content %}
<main class="container my-4">
    <h3 class="my-4 border-bottom pb-1">Сертификаты</h3>
    <div class="row">
        {% for certificate in certificates %}
        <html>
          <div class="card shadow">
            <h1>{{certificate.title}}</h1>
            <iframe src="//media/{{certificate.pdf}}" width="100%" height="100%">
            </iframe>
          </div>
        </html>
        {% endfor %}
    </div>
</main>
{% endblock %}

我通过谷歌采取了部分信息,也许我错过了重要的设置,但我找不到解决方案?网站在我的计算机上本地运行。

dwbf0jvd

dwbf0jvd1#

在这里,我试图解决问题,请检查与下面的代码您的身边
models.py

class Certificate(models.Model):
    title = models.CharField(max_length=30, null=True)
    pdf = models.FileField(upload_to='pdf')
    status = models.BooleanField(default=True)

views.py

def View1(request):
    all_data= Certificate.objects.filter(status=True).order_by('-id')
    context = {'data':all_data}
    return render(request,'index.html',context)

settings.py(最后添加此行)

X_FRAME_OPTIONS = 'SAMEORIGIN'

Html代码

<div class="container">
    <div class="row">
        {% for i in data %}
        <div class="col p-3">
            <h5 class="card-title">{{i.title}}</h5>
            <iframe src="{{i.pdf.url}}" style=" width:600px; height:400px;" frameborder="0"></iframe>

        </div>
        {% endfor %}

    </div>
</div>

浏览器输出

相关问题