Hello guys am stuck trying all i can to query file in a foreignkey but it doesn't work django throws up an error i have been trying to do this for long now I just gave and now i am back to it need help!
from __future__ import unicode_literals
from django.db import models
class Examination(models.Model):
examination_name = models.CharField(max_length=50, unique=True)
class Meta:
verbose_name='examination'
verbose_name_plural='examinations'
def __unicode__(self):
return self.examination_name
class YearAndExaminationName(models.Model):
exam_name = models.ForeignKey(Examination)
examination_year = models.CharField(max_length=4)
class Meta:
verbose_name='year and examination name'
verbose_name_plural='year and examination names'
def __unicode__(self):
return self.examination_year
class PastQuestion(models.Model):
year_and_exam_name = models.ForeignKey(YearAndExaminationName)
file_name = models.CharField(max_length=40, default='self')
file = models.FileField(upload_to='uploads/', max_length=100,)
posted_by = models.CharField(max_length=40)
def __unicode__(self):
return self.file_name
What i intended doing is to get pdf to display files by referencing from view via id or pk but when i search stackoverflow or google what I get is how to convert html to pdf.
my views
def jamb_detail(request):
instance = get_object_or_404()
context = {
"title":"Jamb Past Question",
"instance":instance.year_and_exam_name,
}
return render(request, 'past_question/jamb_detail.html', context)
In views.py
def pdf_view(request):
# get file path by querying model and then
with open('/path/to/my/file.pdf', 'rb') as pdf:
response = HttpResponse(pdf.read(), mimetype='application/pdf')
response['Content-Disposition'] = 'inline;filename=some_file.pdf'
return response
pdf.closed