<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def display_stats(request, stats, queries):
 """ Generate a HttpResponse of functions for a profiling run. _stats_ should contain a pstats.Stats of a hotshot session. _queries_ should contain a list of SQL queries. """

    sort = [
        request.REQUEST.get('sort_first', 'time'),
        request.REQUEST.get('sort_second', 'calls')
    ]
    fmt = request.REQUEST.get('format', 'print_stats')
    sort_first_buttons = RadioButtons('sort_first', sort[0], sort_categories)
    sort_second_buttons = RadioButtons('sort_second', sort[1], sort_categories)
    format_buttons = RadioButtons('format', fmt, (
        ('print_stats', 'by function'),
        ('print_callers', 'by callers'),
        ('print_callees', 'by callees')
    ))
    output = render_stats(stats, sort, fmt)
    output.reset()
    output = [html.escape(unicode(line)) for line in output.readlines()]
    response = HttpResponse(content_type='text/html; charset=utf-8')
    response.content = (stats_template % {
        'format_buttons': format_buttons,
        'sort_first_buttons': sort_first_buttons,
        'sort_second_buttons': sort_second_buttons,
        'rawqueries' : b64encode(cPickle.dumps(queries)),
        'rawstats': b64encode(pickle_stats(stats)),
        'stats': "".join(output),
        'url': request.path
    })
    return response