2

I'm currently working on a question bank website using Django 1.87 that allow users o login and view saved past questions. so far i have been able to display links to the past questions in the database base on user selection. the past questions are uploaded as docs or PDF format and stored in the media directory.

I want logged in users to be able to open and read the content of the past question before deciding whether to print or download it. so far i have been able to display a list of links to the documents and when i click on a link it downloads the document to my computer. what i really want to achieve is for users to be able to click on a link and open the document from within the browser before clicking a download buttenter code hereon or print.

here are my codes:

models.py

class QuestionBank(models.Model):

date = models.DateField(_("Date"),default=date.today)
class_level = models.ForeignKey(ClassLevel)
course_name = models.CharField(max_length=350)
course_code = models.CharField(max_length=20)
question_papers = models.FileField(upload_to = 'Question_Papers/     %y/%m/%d')   

views.py def Questions(request, sel_course):

context = RequestContext(request)
SelCourse = sel_course.replace('_',' ')
context_dict = {'SelCourse':SelCourse}

try:
    Quest_For_course = QuestionBank.objects.filter(course_code = SelCourse)
    context_dict['Quest_For_course'] = Quest_For_course

except course_code.DoesNotExist:
    pass

return render_to_response('Qbank/QuestionsPage.html', context_dict, context)

in my Template i have this

QuestionsPage.html

<title> Question Papers </title> 

<body> 
    <h2> LIST OF QUESTION PAPERS FOR {{selCourse}} </h2>

    <h3> Select question to view, download or print it</h3>

    {% if Quest_For_course %}

    <ul>{% for ques in Quest_For_course %}

    <li><a href="{{ques.question_papers.url}}"> 

    {{ques.question_papers}} </a></li>

    {%endfor%}
    </ul>

    {%else%}
    <strong> THERE ARE NO AVAILABLE QUESTION FOR THIS CLASS AT THE MOMENT </strong>
    {%endif%}
</body>

how do i make past question documents view-able? on a form that has download and print button. thanks

jids
  • 51
  • 1
  • 9
  • take a look at this - http://stackoverflow.com/questions/39919012/django-python-show-pdf-in-a-template – Compadre Apr 06 '17 at 14:58
  • @Compadre I'm grateful for the refereed link it has really helped in clarifying so many things i was confused about. but now i facing another challenge and its driving nuts as have tried so many methods to get the absolute path to file working I'm so confused and don't know what else to do about it. here is what i have done – jids Apr 08 '17 at 15:52
  • some one should please help me on this, Im stuck i dont know what else to try. this code here gives me the absolute path to the files i want to output file_path = os.path.join(settings.MEDIA_ROOT, 'QuestionPapers',File_Name) but i still get FileNotFoundError – jids Apr 08 '17 at 18:45
  • you need to update your settings according to this documentation page https://docs.djangoproject.com/en/1.10/howto/static-files/ – Compadre Apr 09 '17 at 17:07
  • I think you will agree, that if django could serve ANY file from your file system, it would not be great idea – Compadre Apr 09 '17 at 17:08

0 Answers0