Thursday, August 23, 2018

Re: template tag help please?

Yes - but you still are doing it strange - you shouldn't use kwargs if you are sending the parameters and have the parameters in the function - use the parameters.

In other words: 

    avn = kwargs['avn']

    avd = kwargs['avd']

    name = kwargs['name']


That is unnecessary....


Regards,

Andréas


Den ons 22 aug. 2018 kl 16:55 skrev Mike Jones <mike.jones@paston.co.uk>:

Ah I was just doing that to prove it was working

 

This is the actual tag

 

from django import template

from django.conf import settings

from django.utils.html import escape

from band.models import Data

 

register = template.Library()

 

@register.simple_tag

def do_cost( name, avn, avd, *args, **kwargs ):

    avn = kwargs['avn']

    avd = kwargs['avd']

    name = kwargs['name']

    val_set = Data.objects.filter(prop = name).filter(date_period=avd)

    avn = 7  #this line is just for testing

    if avn >= 7:

        for x in val_set:

            val = x.full

    else:

        for x in val_set:

            val = x.short

    return val

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Andréas Kühne
Sent: 22 August 2018 15:20
To: django-users@googlegroups.com
Subject: Re: template tag help please?

 

Wait a secound, you are doing some very strange things....

 

I didn't see this before.

 

You have defined the following:

@register.simple_tag
def do_cost( name, avn, avd, *args, **kwargs ):
    avn = kwargs['avn']
    avd = kwargs['avd']
    name = kwargs['name']
    return name

 

The method has 3 parameters that you are sending to it - name, avn and avd. They correspond to the values sent to the method in order. So you don't need to use the kwargs dictionary - it's unnecessary...

You could just write:

@register.simple_tag
def do_cost( name, avn, avd, *args, **kwargs ):
    return name


Beacause you have sent the name to the method?

 

Regards,

 

Andréas

 

 

Den ons 22 aug. 2018 kl 16:09 skrev MikeKJ <mike.jones@paston.co.uk>:

Thank you Andreas Kuhne, progress

 

So this {% load get_cost %}{% do_cost b.name avn avd %} was working quite happily but for some reason is now complaining

 

Exception Type:

TemplateSyntaxError

 Exception Value:

Caught KeyError while rendering: 'avn'

 

Exception Location claims to be    get_cost.py in do_cost, line 10  which is avn = kwargs['avn']

 

but the exception is in the template line {% load get_cost %}{% do_cost b.name avn avd %}

 

 

 

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8d9d18dd-c8cb-494c-a2b8-8b06b64c40d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/zG3S_9BQKMY/unsubscribe.
To unsubscribe from this group and all its topics, 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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCcnJzyrWXGjTyF9NZAc0SE6zxQ1gbrd1ZM2Oc%2B3ZL_tyQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5b7d7953.1c69fb81.85c08.097dSMTPIN_ADDED_BROKEN%40gmr-mx.google.com.
For more options, visit https://groups.google.com/d/optout.

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCcbeyp%2B33YCNmK6w053X14-4Noqxm2KWPj58A-rs8EJrA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment