G
G
Gennady Karev2014-06-05 13:24:31
Django
Gennady Karev, 2014-06-05 13:24:31

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>

The "export_company_orders" view itself
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

Now the export is happening all the time. I need to add a period, but I don’t know how to correctly pass the start and end parameters of the date from the form to the link. Or how to do the correct export with filtering?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gennady Karev, 2014-06-05
@Warlegend

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 question

Ask a Question

731 491 924 answers to any question