Answer the question
In order to leave comments, you need to log in
How to pass parameters in a link?
There is a button with a link to a view that exports data in excel format and gives us an xls file.
<button type="button" class="btn btn-default"><a href="{% url 'export_company_orders' %}">Excel</a></button>
def export_company_profit(request):
book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('Прибыль компании')
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_LEFT
alignment.vert = xlwt.Alignment.VERT_TOP
style = xlwt.XFStyle()
style.alignment = alignment
# write the header
row_num = 0
columns = [
(u"Дата", 4000),
(u"Заказы", 2000),
(u"Сумма", 2000),
]
for col_num in xrange(len(columns)):
sheet.write(row_num, col_num, columns[col_num][0], style=xlwt.Style.default_style)
# set column width
sheet.col(col_num).width = columns[col_num][1]
# write your data
items = CompanyRevenue.objects.all()
for item in items:
try:
row = [
item.date.strftime("%Y-%m-%d"),
item.order_count,
item.order_revenue,
]
row_num += 1
for col_num in xrange(len(row)):
sheet.write(row_num, col_num, row[col_num], style=xlwt.Style.default_style)
except User.DoesNotExist:
continue
response = HttpResponse(mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=Прибыль компании.xls'
book.save(response)
return response
Answer the question
In order to leave comments, you need to log in
Here is the solution:
{% url 'myapp:export_company_profit' start_date=start_date end_date='end_date' as the_url %}
<button type="button" class="btn btn-default"><a href="{{ the_url }}">Excel</a></button>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question