Huma Qureshi
Huma Qureshi

Reputation: 201

How to get the data from views to template Django

From my model I am uploading a file , In my views I perform some operations on the file .I am using DetailView of CBV of Django , In which I get one object and perform some operations on it . Now I want the data (after performing the operations) in my template . I know how to show the file in my template because the file is in models . But the functions are not in models and I want to print those in my template. My models.py is :

class FileUpload(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True , null=True)
    file = models.FileField(upload_to='files')




    def get_absolute_url(self):
        return reverse('home')

my views.py is :

class PostDetailView(DetailView):
    context_object_name = "object_list"
    template_name = "post_detail.html"


    def get_object(self, *args,**kwargs):
        request = self.request
        pk = self.kwargs.get('pk')
        instance = FileUpload.objects.get(id = pk) #I want to print this  instance

        if instance is None:
            raise Http404("File Does not EXIST")
        else:
            pdfFileObj = open(instance.file.path, 'rb')
            pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
            print('number of pages = ',pdfReader.numPages)
            pageObj = pdfReader.getPage(0)
            print(pageObj.extractText())     #I want to print this
            query = pageObj.extractText()

            for j in search(query, tld="co.in", num=10, stop=10, pause=2):
                print(j)       #I want to print this

            pdfFileObj.close()
        return instance

Upvotes: 1

Views: 1338

Answers (1)

Brian Destura
Brian Destura

Reputation: 12068

Try to first add the queryset as FileUpload.objects.all(), in order for your detail view to know where to find your object automatically. That way, you don't have to write your own get_object.

You can then move all those processing to get_context_data, and then pass those to the template if you want to display them, like this:

class PostDetailView(DetailView):
    context_object_name = "object_list"
    template_name = "post_detail.html"
    queryset = FileUpload.objects.all()

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)

        pdfFileObj = open(self.object.file.path, 'rb')
        pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
        print('number of pages = ',pdfReader.numPages)
        pageObj = pdfReader.getPage(0)
        extracted_text = pageObj.extractText())
        query = pageObj.extractText()

        search_results = []
        for j in search(query, tld="co.in", num=10, stop=10, pause=2):
            search_results.append(j)

        pdfFileObj.close()

        context['extracted_text'] = extracted_text
        context['search_results'] = search_results

        return context

You should then be able to use them in your template:

{{ extracted_text }}

{{ search_results }}

And to print the instance, just use:

{{ object }}

Upvotes: 1

Related Questions