generating pdf report with django-easy-pdf

780 views
Skip to first unread message

sum abiut

unread,
Apr 22, 2015, 9:16:46 PM4/22/15
to django...@googlegroups.com
Hi,
I am trying to generate pdf reports using django-easy-pdf but some how i am not geting the data. 


here is what i get. i only get the header but not the data.  i think i am missing something from class viewpdfreport that i can't seem to figure it ouInline image 1

Here is my view.py:

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.conf import settings
from django.views.generic import TemplateView
from eLeave.views import govoffice

from eLeave.models import newleave


from easy_pdf.views import PDFTemplateView

class viewpdfreport(PDFTemplateView):
    template_name = "viewpdf.html"

    def get_context_data(self, **kwargs):
        return super(viewpdfreport, self).get_context_data(
            pagesize="A4",
            title="Test!",
            **kwargs
        )




here is my viewpdf.html

<table id="t01" >
<tr>
<!--<th><input type="checkbox" id="test"></th>-->
  <th>First Name</th>
  <th>Last Name</th>
  <th>Position</th>
  <th>Department</th>
  <th>Month </th>
  <th>Year</th>
  <th>Total leave consume</th>
  <th> Current Leave Entitlement</th>
   <th>Holidays</th>
   <th>Weekend</th>
   <th> Leave outstanding Balance</th>
   <th>Leave current Balance</th>
 
 
</tr>
{%for a in govstaff%}
<tr>
<!--<td><a href ="#"><input type="checkbox" onClick="parent.location='#'" > </a> </td>-->
<td>{{a.first_name}}</td>
  <td>{{a.last_name}}</td>
  <td>{{a.position}}</td>
  <td>{{a.department}}</td>
  <td>{{a.month}}</td>
  <td>{{a.year}}</td>
  <td>{{a.total_working_days}}</td>
  <td>{{a.leave_entitlement}}</td>
  <td>{{a.holiday}}</td>
  <td>{{a.weekend}}</td>
  <td>{{a.leave_outstanding_balance}}</td>
  <td>{{a.leave_current_balance}}</td>
 


  </tr>
{%endfor%}
</table>


this function is a view from a different app(view.py)
def govoffice(request):  govstaff=newleave.objects.filter(department="GOV",department_head_authorization="Approved")
    return render(request,'viewpdf.html', locals())


Cheers,

James Schneider

unread,
Apr 22, 2015, 9:35:08 PM4/22/15
to django...@googlegroups.com

Have you validated that there is actual data in the govstaff variable within your template? Nothing is going to show up if it is an empty variable or doesn't exist in your template context.

While I'm not familiar with the package you're using, I suspect you need to add the govstaff variable to your context within the get_context_data() method in your view class. Otherwise your template has no idea what data you are trying to display.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y4TC8oXGeE0q9vPcj%3Dnx9qXJ37Ac4PcZiW6cGgGKb-LJw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

sum abiut

unread,
Apr 23, 2015, 1:25:38 AM4/23/15
to django...@googlegroups.com
Hi James,
Yes i have validated that there is  data in the govstaff variable. when i add govstaff to context within the get_context_data() i am getting the error non-keyword arg after keyword arg. Is there are other ways to generated pdf reports on Django?

Cheers,


For more options, visit https://groups.google.com/d/optout.



--

James Schneider

unread,
Apr 23, 2015, 2:25:48 AM4/23/15
to django...@googlegroups.com

How are you adding the govstaff variable in the get_contexr_data method? Can you post what you tried? You shouldn't be getting an error like that. Are you trying to do it as part of the super() call?

As far as alternatives go, ReportLab is pretty popular with Django, to the point where it is on the official docs:

https://docs.djangoproject.com/en/1.8/howto/outputting-pdf/

-James

Jose Aguirre

unread,
May 31, 2015, 6:48:21 PM5/31/15
to django...@googlegroups.com

You need to add the get method this:

 def get(self, request, *args, **kwargs):
    """
Handles GET request and returns HTTP response.
"""
context = self.get_context_data(**kwargs)
#data: dic, array, etc.
values
= [1,2,3,4,5]
# add the data in the context
context["values"] = values

return self.render_to_response(context)


sum abiut

unread,
Jun 9, 2015, 5:16:46 PM6/9/15
to django...@googlegroups.com
Thanks Jose,
i will try that out and see how it goes.

Cheers,
Sum

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



-

roshan.pannase

unread,
Aug 22, 2015, 7:34:21 AM8/22/15
to Django users
class HelloPDFView(PDFTemplateView):
query_results = Contact.objects.all()
print "DEBUG : ", query_results
template_name = 'hello.html'

def get(self,request, *args, **kwargs):
context = self.get_context_data(**kwargs)
print "DEBUG : ", self.query_results
template_name = 'hello.html'
context["query_results"] = self.query_results

return self.render_to_response(context)


Try this one it working successfully
Reply all
Reply to author
Forward
0 new messages