Printing page numbers using Pisa - python

I am using pisa in my django project, to make my html generate on a PDF for report functionality. Some of my reports are getting rather large and I have noticed it doesn't have any page numbering on it. Does anyone know how I can get page numbers to show up on the printed PDF report?
edit
here is how I generate the PDF
if request.POST.has_key('print_report_submit'):
context['show_report'] = True
context['mb_logo'] = os.path.join(os.path.dirname(__file__), "../../../media/images/mb_logo.jpg")
html = render_to_string('reports/fam_maturity_print.html', RequestContext(request,context))
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=family_maturity-statement.pdf'
return response
HTML
{% if show_report %}
<table width="100%">
<tr>
<td colspan="2" align="center" width="400px">
<h1 class="title">Family Maturities by Year</h1>
<br/><br/>
</td>
</tr>
<tr>
<td width="500px">
<img src="{{ mb_logo }}"/>
<td> <p>
<img style="max-height: 60px; max-width:60px;" src="{{ check_image }}"/>
</p>
</tr>
</table>
<br/>
<br/>
<hr class="report_hr1"/>
<h2 class="state_head" style="font-size: 16px;">Statement Information As of {{ statement_date|date:"F d, Y" }}</h2>
<hr class="report_hr1"/>
<table>
{% for plan_dict in plan_list %}
<hr class="report_hr2"/>
<table>
<tr>
<td><strong>Plan Holder: </strong></td><td>{{ plan_dict.plan.get_primary_owner.member.client}}</td>
<td><strong>Joint Owner(s): </strong></td>
<td>
{% for member in plan_dict.plan.planmember_set.all %}
{% ifnotequal member.ownership_type.code "primary" %}
{{ member }}
{% endifnotequal %}
{% endfor %}
</td>
</tr>
<tr>
<td><strong>Plan ID: </strong></td><td>{{ plan_dict.plan.id }}</td>
</tr>
<tr>
<td><strong>Plan type: </strong></td><td>{{ plan_dict.plan.plan_type }}</td>
</tr>
</table>
<hr class="report_hr2"/>
{# This is the table for the current year's maturities #}
{% if plan_dict.investment %}
<table>
<td colspan="2"><h3>{{ cur_year }}</h3></td>
<tr>
<th>Financial Institution</th>
<th>Type</th>
<th>Principal</th>
<th>Maturity</th>
</tr>
{% for i in plan_dict.investment %}
<tr>
<td>{{ i.financial_institution.abbr }}</td>
<td>{{ i.product.code }}</td>
<td>${{ i.amount|intcomma }}</td>
<td>${{ i.maturity_amount|intcomma }}</td>
</tr>
{% endfor %}
<tr>
<td><strong>Total for {{ cur_year }}</strong></td>
<td></td>
<td><strong>${{ plan_dict.total|intcomma }}</strong></td>
<td><strong>${{ plan_dict.total_m|intcomma }}</strong></td>
</tr>
</table>
{% endif %}
<div id="holding_table" align="center">
<h3>Holding Totals with Each Company</h3>
<table>
<tr>
<td style="font-weight: bold;">Financial Institution</td>
<td style="font-weight: bold;">Total</td>
</tr>
{% for l in plan_dict.total_list %}
{% if l.maturity_amount__sum != None %}
<tr>
<td>{{ l.financial_institution__abbr }}</td>
<td>${{ l.maturity_amount__sum|intcomma }}</td>
</tr>
{% endif %}
{% endfor %}
<tr>
<td style="font-weight: bold;">Total Non-Fund Holdings for this plan</td>
<td style="font-weight: bold;">${{ plan_dict.grand|intcomma }}</td>
</tr>
</table>
<span class="disclaimer_span">This report has been prepared from data believed to be reliable but no
representation is made as to accuracy or completeness.</span>
</div>
<br/>
<br/>
{% endfor %}
</table>
{% endif %}
Any help would be much appreciated.

How says in this link:
http://www.arnebrodowski.de/blog/501-Pisa-and-Reportlab-pitfalls.html
One problem I ran into was adding a static footer to my pages, which contains a <pdf:pagenumber /> tag and should show the current pagenumber at the bottom of every page of the resulting PDF. The problem was, that every page just showed the number 0. The solution was very simple, but I was only able to figure it out after studiying the pisa source-code: You can only use the tag inside a paragraph, not (as I did) inside a table for example. Even parapgraphs inside tables don't work. It has to be a top-level paragraph.
Edit
Try this:
<div>
<pdf:pagenumber />
</div>

This is working for me you need to use this its for A3 size
#page{
size: A3;
margin: 1cm;
#frame footer_frame { /* Another static Frame */
-pdf-frame-content: footer_content;
left: 400pt; top: 1170pt; height: 20pt;
}
}
you have to put on above table
<div id="footer_content"> Page <pdf:pagenumber>
of <pdf:pagecount> </div>

Related

Django template for loop is empty in another block

I have a function in my view.py:
#login_required(login_url='login')
def gpd(request,pk):
# get gpd by id
current_gpd = get_gpd(pk)
# get current campaign year #TODO check types
current_campaign = get_campaign(current_gpd.gpd_year)
# get all corporate goals for current campaign
corp_goals = CorporateGoal.objects.filter(gpd_year=current_campaign.year)
compl_weight = []
for goal in corp_goals:
compl_weight.append(goal.corp_factor_weight*current_gpd.bonus.corporate_component//100)
corporate_goals = zip(corp_goals, compl_weight)
if is_manager(request)!=None:
team = get_team(request)
context = {'gpd':current_gpd,
'corporate_goals':corporate_goals,
}
return render(request, 'app/gpd_forms/form_gpd.html', context)
else:
context = {'gpd':current_gpd,
'corporate_goals':corporate_goals,
}
return render(request, 'app/gpd_forms/form_gpd.html', context)
As you can see, in context I have corporate_goal.
My form_gpd.html:
{% extends 'app/navbar/main.html' %}
{% load static %}
{% block content %}
{% include 'app/slidebar/gpd_form_slidebar.html' %}
<div class="container" style="background-color:white">
<div class="row">
<div id="section2" class="container-fluid">
{% include 'app/gpd_blocks/corporate_goal.html' %}
</div>
</div>
<hr />
</div>
<div class="container" style="background-color:white">
<div class="row">
<div id="section5" class="container-fluid">
{% include 'app/gpd_blocks/summary.html' %}
</div>
</div>
<hr />
</div>
</div>
{% endblock %}
for example in corporate block I am executing next:
{% load static %}
{% block content %}
<div class="row" id="super">
<p>&nbsp</p>
</div>
<div class="row" id="super">
<div class="col-11" style="color: ivory; font-weight: bold; font-size: 1.3em;">
CORPORATE GOALS BLOCK
</div>
</div>
<div class="row" id="super">
<p>&nbsp</p>
</div>
{% for goal, compl_weight in corporate_goals %}
<hr style="height:2px;border:none;color:rgb(87, 124, 161);background-color:rgb(87, 124, 161);" />
<!-- Corporate goal section-->
<div class="row">
<div class="col">
Corp. Goal: {{ goal.corp_goal_title }}
</div>
</div>
<div class="row">
<div class="col-8">
<div>
{% if goal.corp_factor_rate %}
<p style="color:mediumspringgreen">rated</p>
{% else %}
<p style="color:indianred">unrated</p>
{% endif %}
</div>
</div>
<div class="col-3">
<div style="margin-inline-start:auto">
{{compl_weight}} % of total score
</div>
</div>
</div>
<!-- Tabs for details-->
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#det1{{ goal.id }}">Goal Details</a></li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#det2{{ goal.id }}">Other Details</a></li>
</ul>
<!-- Tabs content for details-->
<div class="tab-content" >
<!--First tab-->
<div id="det1{{ goal.id }}" class="tab-pane fade show active">
<div class="row">
<!--Column 1-->
<div class="col">
<table class="table-bordless" style="margin-top:20px;">
<tbody>
<tr>
<th>Start Date</th>
<td width="50"></td>
<td>{{ goal.start_date }}</td>
</tr>
<tr>
<th>Weight</th>
<td width="20"></td>
<td>{{ goal.corp_factor_weight }} %</td>
</tr>
<tr>
<th><p>&nbsp</p></th>
</tr>
<tr>
<th style="color:dimgray">Goal description</th>
<td width="18"></td>
<td></td>
</tr>
</tbody>
</table>
<div class="row">
<textarea class="form-control" readonly title="Description" style="background-color:aliceblue !important;">{{goal.corp_goal_description}}</textarea>
</div>
</div>
<!--Column 2-->
<div class="col">
<table class="table-bordless" style="margin-top:20px;">
<tbody>
<tr>
<th>Due Date</th>
<td width="50"></td>
<td>{{ goal.end_date }}</td>
</tr>
<tr>
<th>Factor Rate</th>
<td width="50"></td>
<td>
{% if goal.corp_factor_rate %}
{{ goal.corp_factor_rate }}
{% else %}
<div style="color:mediumspringgreen; font-weight: bold;">ongoing...</div>
{% endif %}
</td>
</tr>
<tr>
<th><p>&nbsp</p></th>
</tr>
<tr>
<th style="color:dimgray">Goal comment</th>
<td width="18"></td>
<td></td>
</tr>
</tbody>
</table>
<div class="row">
<textarea class="form-control" readonly title="Comment" style="background-color:aliceblue !important;">{{goal.corp_goal_comment}}</textarea>
</div>
</div>
</div>
</div>
<!--Second tab-->
<div id="det2{{ goal.id }}" class="tab-pane fade" style="margin-top:20px;">
<p>Factor for Goal Achievement:</p>
<table class="table">
<tbody>
<tr>
<th>Factor</th>
<td>0</td>
<th>Description</th>
<td>{{ goal.corp_factor_0 }}</td>
</tr>
<tr>
<th>Factor</th>
<td>1</td>
<th>Description</th>
<td>{{ goal.corp_factor_1 }}</td>
</tr>
<tr>
<th>Factor</th>
<td>2</td>
<th>Description</th>
<td>{{ goal.corp_factor_2 }}</td>
</tr>
<tr>
<th>Factor</th>
<td>3</td>
<th>Description</th>
<td>{{ goal.corp_factor_3 }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<br />
<br />
<br />
{% endfor %}
{% endblock %}
And it works perfectly. But when in last block summary I want to use corporate_goals one more time - I have nothing on my page, looks like corporate_goals is not exist.
my summary.html
{% load static %}
{% block content %}
<div class="row">
<p>123</p>
</div>
<div class="row">
{% for goal, compl_weight in corporate_goals %}
{{ goal.corp_goal_title }}
{% endfor %}
</div>
<div class="row">
<p>123</p>
</div>
{% endblock %}
Even If I copy all my code from corporate_goal.html into summary - I will have nothing. Why ?
I think that the problem is that your html structure has no sense. You have three {% block content %}. Please, delete the content blocks of your include files (corporate_goal.html and summary.html) and check if this is the problem. I think that one of your content block is overriding the other one.
Just to clarify, when you use the "include tag" is like you were pasting the code from other file. So imagine the result. You have a block content that contains two other block contents inside it.
My problem was in zip objects. When we are iterating through a zip object it is exhausted and you cannot iterate through it again. So, solution is corporate_goals = list(zip(corp_goals, compl_weight)). Anyway, thank you, #LaCharcaSoftware, for the advice, I've changed my structure to avoid duplication with block content.

'post' object is not iterable in django and it is showing error but i'm not getting what's going wrong on code

TypeError at /dashboard
'post' object is not iterable i am just try to get data from db table and show on dashboard in dabel form
my dashboard.html page
{% if posts %}
<table class="table table-dark table-hover">
<thead>
<tr>
<th scope="col" style="width:3%">ID</th>
<th scope="col" style="width:30%">Title</th>
<th scope="col" style="width:55%">Description</th>
<th scope="col" style="width:15%">Action</th>
</tr>
</thead>
<tbody>
{% for post in posts %}
<tr>
<td>{{posts.id}} </td>
<td>{{posts.title}}</td>
<td>{{posts.desc}}</td>
<td><a class="btn btn-primary" type="submit" value="Submit">Edit</a>
<form action ='' , method ='POST' class='d-inline'>
{% csrf_token %}
<input type='submit' class='btn btn-danger btn-sm' value ='Delete' >
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<h4 class =' text-center alert alert-warning'>No Records </h4>
{% endif %}
</div>
</div>
{% endblock content %}
my views.py
def dashboard(request):
if request.user.is_authenticated:
posts= post.objects.all()
return render(request,'dashboard.html',{'posts':post})
else:
return HttpResponseRedirect('login')
urls.py
path('dashboard',views.dashboard,name='dashboard'),
You have a typo in your views.py file. Try change this:
return render(request,'dashboard.html',{'posts':post})
to this:
return render(request,'dashboard.html',{'posts':posts})
I assume in template dashboard.html you have to change this :
{% for post in posts %}
<tr>
<td>{{posts.id}} </td>
<td>{{posts.title}}</td>
<td>{{posts.desc}}</td>
to this :
{% for post in posts %}
<tr>
<td>{{post.id}} </td>
<td>{{post.title}}</td>
<td>{{post.desc}}</td>
It should be like this:
{% for post in posts %}
<tr>
<td>{{post.id}} </td>
<td>{{post.title}}</td>
<td>{{post.desc}}</td>

Too Many Values To Unpack While Rendering Data On A Template (Django)

VIEWS
def Aeging(request):
current_date_thirty = date.today().isoformat()
days_before_thirty = (date.today() - timedelta(days=30)).isoformat()
thirty = journalVoucher.objects.values('account_name_one').order_by('account_name_one').filter(voucher_date_one__range=[days_before_thirty,current_date_thirty]).annotate(outstanding=(Sum('credit_amount_one')-Sum('debit_amount_one')))
for item in thirty:
item['account_name_one'] = CreateLedger.objects.get(id=item['account_name_one'])
return thirty
context ={'thirty':thirty}
return render(request,'report/aeging/aeging_analysis.html',context)
<div class="row">
<div class="table-responsive col-md-6">
<table id="example" class="table table-bordered">
<caption style="caption-side:top; text-align: center;"><h3>0-30 Days</h3></caption>
<thead class="thead-dark">
<tr>
{# <th>S No.</th>#}
<th>Party Name</th>
<th>Total Amount</th>
</tr>
</thead>
<tbody>
{% for item in thirty %}
<tr class="table-success">
{# <td>{{ }} </td>#}
<td>{{item.account_name_one}}</td>
<td>{{ item.outstanding }} </td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr class="totalColumn">
<td style="visibility:hidden;"></td>
<td>Total:</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
Hi The Query Is perfectly Run on python shell but when i render the data on a template it shows too many values to unpack (expected 2).
I Provide You My Views and Template Code.
Any Solution Of This??
Images Link..
https://drive.google.com/drive/folders/1V7uI4YS4Qg8ng4bmN0aCFvp85Dzi8n5S?usp=sharing

Is there a way to concatenate a django object query from a template?

I have the following code:
<table id="MyTable" class="highlight responsive-table" >
<thead>
<tr>
{% with columns as columns %}
{% for columns in columns %}
<th class="th-sm">{{ columns }}</th>
{% endfor %}
{% endwith %}
</tr>
</thead>
<tbody>
{% for trans in transactions %}
<tr>
<td> {{ trans.transactionsequencenumber }}</td>
<td> {{ trans.posid }}</td>
<td> {{ trans.transactionnumber }}</td>
</tr>
{% endfor %}
</table>
which works for displaying static table data, but the idea is that the columns being iterated are being set via a user preferences model, so the table columns can change depending on the user, theres a possible 90+ columns to choose from, and here is where my issue lays.
pseudo code below to help illustrate my goal:
<table id="MyTable" class="highlight responsive-table" >
<thead>
<tr>
{% with columns as columns %}
{% for columns in columns %}
<th class="th-sm">{{ columns }}</th>
{% endfor %}
{% endwith %}
</tr>
</thead>
<tbody>
{% for trans in transactions %}
<tr>
<td> {{ trans.{{columns}} }}</td>
</tr>
{% endfor %}
I've tried simple string concatenation so far, but that just prints the string to the table and not the object data,
any help and/or guidance would be greatly apprecieted.
be kind, this is my first question.
For every object that represent a line in the database attribute, you put it into a column. For example:
<tr>
<th> title </th>
<th> body </th>
</tr>
{% for post in posts %}
<tr>
<td> {{post.title}}</td>
<td> {{post.body}} </td>
</tr>
{% endfor %}

how to disable a button which acts as a link django

What is the best way to disable the "deny" button when "approve" button is clicked ? I have {{some}} that stores the value of approve or deny value.
<button>Approve</button>
<button>Deny</button>
html file
{% if some %}
<table id="example" class="display" cellspacing="0" width="100%" border="1.5px">
<tr align="center">
<th> Student ID </th>
<th> Student Name </th>
<th> Start Date </th>
<th> End Date </th>
<th> Action </th>
<th> Status </th>
</tr>
{% for item in query_results %}
<tr align="center">
<td> {{item.studentID}} </td>
<td> {{item.studentName}} </td>
<td> {{item.startDate|date:'d-m-Y'}} </td>
<td> {{item.endDate|date:'d-m-Y'}} </td>
<td><button>Approve</button> <button {% if some == 'approve' %} disabled{% endif %}>Deny</button></td>
<td>
{% if item.status %}
{{item.status}}
{% else %}
Pending
{% endif %}
</td>
</tr>
{% endfor %}
</table>
{% else %}
the {{some}} gets from here
views.py
def superltimesheet(request):
query_results = Timesheet.objects.all()
data={'query_results':query_results, 'some':'some'}
return render(request, 'hrfinance/supervisor_list_timesheet.html', data)
Use an if tag:
<button{% if some == 'deny' %} disabled{% endif %}>Approve</button>
I am not sure about what are you asking, but you can do an if statement like
{% if some == 'approve' %}
<button>Deny</button>
{% else %}
<button>Approve</button>
{% endif %}
or:
{% if some == 'approve' %}
<button>Deny</button>
{% else %}
tell me if that works or I misunderstood

Categories

Resources