Django custom templating template with parser.compile_filter parameter (tokens [2]) not working

I tried to implement the solution suggested by T. Stone in my question "How-do-i-pass-a-lot-of-parameters-to-views-in-django" ([link text] [1]).
I cannot get any result. Difficult to find information about compile_filter()

, but as far as I understand, cls(queryset=parser.compile_filter(tokens[2]), template=template)

should render the template using 'variable' tokens [2]. But it doesn't work.

Here is my implementation code:
models.py:

class SalesRecord(models.Model):
    name = models.CharField(max_length=100)
    month = models.CharField(max_length=10)
    revenue = models.IntegerField()
    def __unicode__(self):
        return self.name + " - " + self.month + " - " + str(self.revenue)

      

views.py:

def test(request, *args, **kwargs):
    name = 'John'
    monthly_sales_qs = SalesRecord.objects.filter(name=name)
    print monthly_sales_qs
    return render_to_response('test.html', locals())

      

mytags.py:

class DataForTag(template.Node):
    @classmethod
    def handle_token(cls, parser, token, template):
        tokens = token.contents.split()
        if tokens[1] != 'for':
                raise template.TemplateSyntaxError("First argument in %r must be 'for'" % tokens[0])

        if len(tokens) == 3:
            return cls(queryset=parser.compile_filter(tokens[2]), template=template)
        else:
            raise template.TemplateSyntaxError("%r tag requires 2 arguments" % tokens[0])

    def __init__(self, queryset=None, template=None):
        self.queryset = queryset
        self.template = template

    def render(self, context):
        return render_to_string(self.template, {'queryset':self.queryset})

@register.tag
def render_data_table(parser, token):
    return DataForTag.handle_token(parser, token, 'testtable.html')

      

test.html:

{% load mytags %}
{% render_data_table for monthly_sales_qs %}

      

testtable.html:

<table class="tabledata">
    <tr>
    {% for m in queryset.month %}
        <td>queryset.revenue</td>
     {% endfor %}
     </tr>
</table>

      

The template just returns a blank page. It seems to me that the queryset is empty. Anyone have an idea what I am doing wrong? (maybe some nonsense beginners;)

+2


a source to share


1 answer


Mark...

A couple of things: I was in a rush the other day when I posted this code for you. Within a method, render

variables must be resolved as such ...

def render(self, context):
    qs = self.queryset.resolve(context)
    return render_to_string(self.template, { 'queryset': qs } )

      

Also, in your template, this is not correct:



{% for m in queryset.month %}
    <td>queryset.revenue</td>
 {% endfor %}

      

Firstly, variables must be wrapped in {{}} like {{ queryset.revenue }}

that, and secondly, you don't do anything with the value m

, so having a loop is for

pointless.

Finally, the sample I showed you in the answer found in the appendix django.contrib.comments

. If you want to follow some of the existing / working examples, I would recommend checking out comment template tags. There are many great ideas in this app.

+1


a source







All Articles