Wednesday, October 2, 2013

Re: Custom Filter: Filter output based on a Python list

Ok, now I get the point. I STRONGLY suggest to avoid performing big computations while rendering templates, so I think it is better for you to try something like

def results_page(request):
    context = {}
    context.update(locals())
    context['periods'] = {}
    for period in ['DECEMBER 2011', 'MAY 2011', 'DECEMBER 2010']:
        context['periods'][period] = Results.objects.all().filter(\
               reg_number = 'DBA/20020/82/DU').filter(<further_filter_by_period>)
    return render_to_response('results.html', context, context_instance = RequestContext(request))


and in template

{% for period,results in periods.items %}
    <table class='table table-striped table-bordered'>
    [...]
    Here you can use {{period}}, which is in turn 'DECEMBER 2011', 'MAY 2011', and so on, and
    {{result}} that encompasses the values extracted from the DB.
    [...]
    </table>
{% endfor %}

I included a filter(<further_filter_by_period>) since I do not know the models, so I do not know how you can filter out by examination period.

Please note that I would not pass the whole locals() dict to the view, but carefully select what I need in the template.

Speaking about your filter

@register.filter
def periodofexamination(periodofexam):
    periodofexam = ['DECEMBER 2011', 'MAY 2011', 'DECEMBER 2010']
    for period in periodofexam:
        return period

I think you misunderstood the filter role, which is, indeed, to filter, things. Here, the input periodofexam variable is not filtered, but overwritten. Furthermore, you use return in a for loop, so the result of your filter is 'DECEMBER 2011' irrespective of the input value.

Please check the above code, I wrote it directly in the mail composer, so bugs are certainly lurking.

Let me know if you succeed in solving the problem

Leo




Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/10/2 +Emmanuel <elusenji@gmail.com>
Here's the view:
def results_page(request):
    queryset = Results.objects.all().filter(reg_number = 'DBA/20020/82/DU')
    return render_to_response('results.html', locals(), context_instance = RequestContext(request))

Please note that, so far, there's nothing wrong with the view. Using the custom filter at the output level is where the issue is.
What I want to do is something close to this (note the bold items):

{% for query in queryset|periodofexamination %}
    <table class='table table-striped table-bordered'>
    <thead><th>Course Code</th><th>Course Name</th><th>Course Work</th><th>Exam</th><th>Grade</th><th>GPA</th><th>Year</th><th>Exam Period</th></thead>
   
    <tr><td>{{ query.coursecode}}</td><td>{{query.coursename}}</td><td>{{query.coursework}}</td><td>{{query.exam}}</td><td>{{query.exam|grade}}</td><td>{{query.exam|gpa}}</td><td>{{query.academicyear}}</td><td>{{query.examperiod}}</td></tr>
   
</table>
{% endfor %}
{% endblock %}

On Wednesday, October 2, 2013 3:00:40 PM UTC+3, Leo wrote:
Can you perhaps paste the view you are using to render the template? So I can try and help your with some code

Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/10/2 +Emmanuel <elus...@gmail.com>
That's what I have been trying to do for quite sometime now! I can't seem to get the code right.


On Wednesday, October 2, 2013 2:13:25 PM UTC+3, Leo wrote:
I'd cycle in the template through a list of exam periods, printing a table for each cycle.
You have to pass the list of exam periods in the context of your view.

Try and let me know.

Regards,
Leo


Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/10/2 +Emmanuel <elus...@gmail.com>
Am working on a Django project that retrieves a student's details from the database, filters them based on the 'period of examination' (this is implemented as a python list in a custom filter) and displays the output.

Here is my custom filter:

@register.filter
def periodofexamination(periodofexam):
    periodofexam = ['DECEMBER 2011', 'MAY 2011', 'DECEMBER 2010']
    for period in periodofexam:
        return period

The template for displaying the output:

{% extends 'base.html' %}
{% load results_extras %}
{% block title %}My Results{% endblock %}

{% block content %}
<h3> My Tentative Examination Results</h3>
<div class="alert alert-info">The results displayed below are tentative and for information purposes only.</div>

    <table class='table table-striped table-bordered'>
    <thead><th>Course Code</th><th>Course Name</th><th>Course Work</th><th>Exam</th><th>Grade</th><th>GPA</th><th>Year</th><th>Exam Period</th></thead>
{% for query in queryset %}
        
    <tr><td>{{ query.coursecode}}</td><td>{{query.coursename}}</td><td>{{query.coursework}}</td><td>{{query.exam}}</td><td>{{query.exam|grade}}</td><td>{{query.exam|gpa}}</td><td>{{query.academicyear}}</td><td>{{query.examperiod}}</td></tr>
   
{% endfor %}
</table>
{% endblock %}

The output:
See the attached file (screenshot.png)

The challenge:
I would like to be able have the student results for a given exam period to appear each in their own table, for instance, all the results for exam period 'December 2011' should be in one table, all the results for exam period 'December 2010' should be in a different table. Currently, all the results appear in one table.
How do I implement this using a custom filter?
Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a79f2a1c-3059-4882-bc97-81c39c8ddd4d%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEhE%2BOmH3qFh%2BuFyt3zMERJO8hCuhDKabyCbkkxCJ7n%2Bwn8d2A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate