Wednesday, October 2, 2013

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

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8bb1abd1-7cc4-44a4-aba4-2087b88d554d%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/a79f2a1c-3059-4882-bc97-81c39c8ddd4d%40googlegroups.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