I asked about this same issue a while back because this is a feature
that I think would be really handy. Not sure how feasible it would be
however.

I put something together after reviewing of the html source of the
admin pages. I did *not* look at the python source (too complicated
for me :). It worked but it is not exactly pretty :) I cut out a bunch
of stuff from my own application that you don't need. I left in the
qstring stuff which can be useful if you have links on your page and
you want to remember the sorting order and stuff when you return to
the page.

Best,

Vincent

========= VIEW ===========
def view_data(request):
        # defining the fields you want to work with in a list of dictionaries
        fields = [
                        
{'field':'id','label':'ID','order':'asc','class':'','area':'All',},
                        {'field':'first_name','label':'First
name','order':'asc','class':'','area':'All',},
                        {'field':'last_name','label':'Last
name','order':'asc','class':'','area':'All',},
        ]

        # fields for filters
        filter_fields = [
        
{'field':'All','label':'All','sort_by':'','order':'asc','class':'',},
        
{'field':'type1','label':'type1','sort_by':'','order':'asc','class':'',},
        
{'field':'type2','label':'type2','sort_by':'','order':'asc','class':'',},
        ]

        # fitering the area of interest
        filter = request.GET.get('area__exact','All')
        if filter == 'All':
                applicants = Application.objects.all()
        else:
                applicants = Application.objects.filter(area=filter)
                # adding the correct filter elements to each sort field
                for i in fields:
                        i['area'] = filter
                # adding the correct elements to each filter field
                for i in filter_fields:
                        if i['field'] == filter:
                                i['class'] = 'class=\"selected\"'

        # sorting the applicant dictionary
        sort_by = int(request.GET.get('o','1'))
        sort_asc = request.GET.get('ot','asc') == 'asc'
        if sort_asc:
                fields[sort_by]['order'] = 'desc'
                fields[sort_by]['class'] = 'class=\"sorted ascending\"'
                applicants = dictsort(applicants,fields[sort_by]['field'])
                # adding the correct elements to each filter field
                for i in filter_fields:
                        i['sort_by'] = sort_by
                        i['order'] = 'asc'
        else:
                fields[sort_by]['order'] = 'asc'
                fields[sort_by]['class'] = 'class=\"sorted descending\"'
                applicants = 
dictsortreversed(applicants,fields[sort_by]['field'])
                # adding the correct elements to each filter field
                for i in filter_fields:
                        i['sort_by'] = sort_by
                        i['order'] = 'desc'

        return render_to_response('view/view.html',
{'applicants':applicants,'nr_applicants':len(applicants),'fields':fields,'filter_fields':filter_fields,'sort_by':fields[sort_by]
['field'],'sort_asc':sort_asc,'qstring':request.META["QUERY_STRING"],'eval_fn':request.user.first_name,'eval_ln':request.user.last_name,},)

======== TEMPLATE ===========
{% extends "admin/change_list.html" %}
{% block title %}List of Applicants{% endblock %}

{% block content %}
<div id="content-main">
        <h3>Number of Applicants: {{ nr_applicants }}</h3>

        <table>
                <tr>
                        <td valign="top">Filter by Area:</td>
                        {% for i in filter_fields %}
                                <td {{ i.class }}><a href="?
area__exact={{ i.field }}&amp;ot={{ i.order }}&amp;o={{ i.sort_by }}">{{ 
i.label }}</
a></td>
                        {% endfor %}
                </tr>
        </table>

        <table>
        <thead>
        <tr>
                {% for i in fields %}
                        <th {{ i.class }}><a href="?
area__exact={{ i.area }}&amp;ot={{ i.order }}&amp;o={{ forloop.counter0 }}">{{ 
i.label }}</
a></th>
                {% endfor %}
        </tr>
        </thead>

        <tbody>
        {% for applicant in applicants %}
        <tr>
                {% if qstring %}
                        <td><a href="/view/eval/applicant{{ applicant.id }}/
{{ qstring }}/">Evaluate</a></td>
                {% else %}
                        <td><a href="/view/eval/applicant{{ applicant.id 
}}//">Evaluate</
a></td>
                {% endif %}
                <td><a href="/admin/application/application/
{{ applicant.id }}/">{{ applicant.id }}</a></td>
                <td>{{ applicant.first_name }}</td>
                <td>{{ applicant.last_name }}</td>
        </tr>
        {% endfor %}

        </tbody>
        </table>

</div>

{% endblock %}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to