英文:
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" >
<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
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:
<a href="{{ mymodel.myfilefield.url }}" target="_blank">
答案2
得分: 0
正确的HTML代码:
<a href="/media/{{ opinion.tests }} " target="_blank">报告</a>
通过提及"/media/"文件夹路径,它起作用了。
英文:
correct html code:
<a href="/media/{{ opinion.tests }} " target="_blank">reports</a>
by mentioning "/media/" folder path, it worked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论