Pdf文件在前端未显示。

huangapple go评论67阅读模式
英文:

Pdf file is not displaying on frontend

问题

我已经在django管理面板中上传了pdf文件。然而,当我尝试在html文件中使用文件标签打开pdf文件时,它没有打开。

HTML代码:

<div class="container">

  <a href="{{ opinion.tests }}">Your tests</a>
</div>

views.py:

def report(request):
if request.method == 'POST':
    try:
        name = request.POST['name']
        phone1 = request.POST['phone']
        print("name:", name, "phone", type(phone1))

        analysis = Report.objects.filter(phone=phone1)[0]
        opinion = {'opinion': analysis}
        return render(request, "reports.html", opinion)
    except:
        return render(request,"not_found.html")

models.py:

class Report(models.Model):
current_date = str(datetime.date.today())

name= models.CharField(max_length=100)
email= models.EmailField(max_length=1000)
phone = models.IntegerField()
previous_history =models.TextField(max_length=5000)
current_diagnosis= models.TextField(max_length=5000)
doctor = models.CharField(max_length=100)
tests = models.FileField(upload_to='ptest/', default="")

def __str__(self):
    return self.name + " | " + "Doctor : "+ self.doctor

文件在管理面板中显示。它也在后端上传。html页面也在没有任何错误的情况下显示。然而,当我点击打开文件以查看时,会出现错误。

我正在上传一个pdf文件,我期望该文件在前端的html文件中显示。

英文:

I have uploaded the pdf file in django admin panel. However, when I try to open the pdf file in html file tag, it is not opening.

HTML code:
<div class="container" >

  &lt;a href=&quot;{{ opinion.tests }}&quot;&gt;Your tests&lt;/a&gt;
&lt;/div&gt;

views.py:
def report(request):
if request.method == 'POST':
try:
name = request.POST['name']
phone1 = request.POST['phone']
print("name:", name, "phone", type(phone1))

        analysis = Report.objects.filter(phone=phone1)[0]
        opinion = {&#39;opinion&#39;: analysis}
        return render(request, &quot;reports.html&quot;, opinion)
    except:
        return render(request,&quot;not_found.html&quot;)

models.py:

class Report(models.Model):
current_date = str(datetime.date.today())

name= models.CharField(max_length=100)
email= models.EmailField(max_length=1000)
phone = models.IntegerField()
previous_history =models.TextField(max_length=5000)
current_diagnosis= models.TextField(max_length=5000)
doctor = models.CharField(max_length=100)
tests = models.FileField(upload_to=&#39;ptest/&#39;, default=&quot;&quot;)

def __str__(self):
    return self.name + &quot; | &quot; + &quot;Doctor : &quot;+ self.doctor

File is displaying in admin panel. It is also uploading in the backend. The html page also displays without any error. However, when I click to open to see the file , it gives an error.

I am uploading a pdf file and i am expecting the file to be displayed in html file in the frontend.

答案1

得分: 1

Your FileField should give the pdf a 'url' attribute, and that's what you'll want to point the href to in your template:

<a href="{{ mymodel.myfilefield.url }}" target="_blank">
英文:

Your FileField should give the pdf a 'url' attribute, and that's what you'll want to point the href to in your template:

&lt;a href=&quot;{{ mymodel.myfilefield.url }}&quot; target=&quot;_blank&quot;&gt;

答案2

得分: 0

正确的HTML代码:
     &lt;a href=&quot;/media/{{ opinion.tests }} &quot; target=&quot;_blank&quot;&gt;报告&lt;/a&gt;

通过提及&quot;/media/&quot;文件夹路径,它起作用了。
英文:

correct html code:
<a href="/media/{{ opinion.tests }} " target="_blank">reports</a>

by mentioning "/media/" folder path, it worked.

huangapple
  • 本文由 发表于 2023年6月27日 19:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定