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:
<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.
2
Answers
correct html code: reports
by mentioning "/media/" folder path, it worked.
Your FileField should give the pdf a ‘url’ attribute, and that’s what you’ll want to point the href to in your template: