How to print page

Asked

Viewed 533 times

0

I want to create a template for printing data but I’m a little lost on how to do this, I have a print name button, the same is found in a table that contains the customer name, telephone address etc ..., when clicking on it it should take the id of this client and the data of the same as name, phone, address ..., and put inside the print_page.html template, which will contain some more things like logo, company name ..., can anyone tell me if you have how to do this with Django direct in views or if I have to use javascript or something else?

I was trying to do direct in the views, but I finally noticed that my logic is not at all right I believe.

class PrintView(TemplateView):
    template_name = 'core/print_page.html'

    def get_context_data(self, **kwargs):
        context = super(PrintView, self).get_context_data(**kwargs)
        context['print_page'] = Client.objects.get(pk=self.pk)
        return context

I did some research but I couldn’t find anything related to what I want.

Thanks in advance for any help.

1 answer

0


Well, as you will print only one line from your database, I suggest you use a DetailView instead of a TemplateView, so you don’t need to set the context:

class ClientPrintView(DetailView):
    model = Client
    template_name = 'core/print_page.html'

Make the template as you see fit. If you want the print window to appear right away, you can place a javascript script at the end of the template:

<script>
  (function(){
    window.print();
  })();
</script>
  • Show vlw @Pablo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.