template tag help please?

57 views
Skip to first unread message

MikeKJ

unread,
Aug 17, 2018, 7:53:54 AM8/17/18
to Django users
I want to pass 2 variables to a templatetag from within a template, a name and a date (eg, John 29/09/2018) to output the result of the templatetag def.

The reason I want to do this is:

From a list of all names:

The name comes from a model that has a foreign key to another model that has actions performed by that name by date and I want to output the action taken on that date so something like:

from django import template
from django.conf import settings
from django.utils.html import escape
from user.models import person
from action.models import data

def (get_action):
    action = Data.objects.get.filter(person=name).filter(date=date)
    return action

but how do I pass the variables in the 1st place please?


Mikhailo Keda

unread,
Aug 17, 2018, 9:42:35 AM8/17/18
to Django users

Mike Jones

unread,
Aug 17, 2018, 10:01:22 AM8/17/18
to django...@googlegroups.com

Yes

 

For example, in the filter {{ var|foo:"bar" }}, the filter foo would be passed the variable var and the argument "bar".

 

I want to pass 2 (maybe 3) variables and correct me if I am wrong but

Custom filters are just Python functions that take one or two arguments:

 

So could do  {{ var|foo:”bar, bar2” }}

or {{ var|foo:”bar” “bar2” }}   and pass 2 arguments?   The doc doesn’t specify

 

I assume if 2 args

def cut(value, arg1, arg2):

 

?

--
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...@googlegroups.com.
To post to this group, send email to django...@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/553d2ac6-cac1-4b9a-b3bd-2a4f5934ae47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

MikeKJ

unread,
Aug 17, 2018, 10:02:51 AM8/17/18
to Django users

Mikhailo Keda

unread,
Aug 17, 2018, 10:48:49 AM8/17/18
to Django users
"Then in the template any number of arguments, separated by spaces, may be passed to the template tag." from the Documentation

MikeKJ

unread,
Aug 22, 2018, 6:16:58 AM8/22/18
to Django users

I am so rusty I’m having trouble writing a simple_tag

 

In the template

 

{% for b in list %}

{% load get_cost %}{% do_cost {{ b.name }} avn avd %}

 

Also tried (per docs) {% do_cost name={{ b.name }} avn=avn avd=avd %}

I definitely have values in the template passed from the view for avn and avd and of course b.name

 

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 = Data.objects.filter(property=name).filter(date_period=date)

    return val

 

I just keep getting do_cost takes 3 arguments

*** I know I want it to take 3 arguments!!!!***

Is it telling me that I can’t use {{ b.name }} as an arg?



Andréas Kühne

unread,
Aug 22, 2018, 6:44:55 AM8/22/18
to django...@googlegroups.com
Hi,

You don't need the {{}} when sending a variable to the template tag - try removing them.

Regards,

Andréas


--
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 https://groups.google.com/group/django-users.
Message has been deleted

MikeKJ

unread,
Aug 22, 2018, 10:09:28 AM8/22/18
to Django users

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 %}



Andréas Kühne

unread,
Aug 22, 2018, 10:20:45 AM8/22/18
to django...@googlegroups.com
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


--
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 https://groups.google.com/group/django-users.

Mike Jones

unread,
Aug 22, 2018, 10:55:49 AM8/22/18
to django...@googlegroups.com

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...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Andréas Kühne
Sent: 22 August 2018 15:20
To: django...@googlegroups.com
Subject: Re: template tag help please?

 

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

--
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...@googlegroups.com.


To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Message has been deleted

Andréas Kühne

unread,
Aug 23, 2018, 3:15:41 AM8/23/18
to django...@googlegroups.com
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


Mike Jones

unread,
Aug 23, 2018, 5:44:08 AM8/23/18
to django...@googlegroups.com

Andreas, 

Thank you for your help and patience I have got the damned thing working!! 

It helped that I remembered that one of the filter arguments was actually a foreign key in the model and had to be an integer id not a string,  that was being obfuscated by being a templatetag call.

 

Thank you

Tosin Ayoola

unread,
Aug 23, 2018, 9:03:34 AM8/23/18
to django...@googlegroups.com
good day please i'm having issues, attached below is one of the error i'm getting also ive been trying to add bootstrap glyphicon to my project but it just display the text without the glyphicon,

--
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 https://groups.google.com/group/django-users.
Screenshot from 2018-08-23 13-50-27.png

ireoluwa fakeye

unread,
Aug 23, 2018, 9:07:26 AM8/23/18
to django...@googlegroups.com
You Are not properly routing your  urls

Tosin Ayoola

unread,
Aug 23, 2018, 9:10:14 AM8/23/18
to django...@googlegroups.com
not properly routing my urls as in, i don't understand

ireoluwa fakeye

unread,
Aug 23, 2018, 9:18:10 AM8/23/18
to django...@googlegroups.com
Sorry didn't look at it well ,can I see a screen shot of your code

Tosin Ayoola

unread,
Aug 23, 2018, 9:31:54 AM8/23/18
to django...@googlegroups.com
i have the code on git actually because i'm trying to get the add a file at the admin page but i'm getting the error


Tosin Ayoola

unread,
Aug 23, 2018, 9:40:49 AM8/23/18
to django...@googlegroups.com
this is the link https://github.com/olaneat/ask_the_schools will rili b glad if u can help

Tosin Ayoola

unread,
Aug 26, 2018, 12:38:26 PM8/26/18
to django...@googlegroups.com
good day guys I'm working on a project but currently I'm having
issues, which is I created a model to save some images files & I
created a template to display the images but the issue now is for loop
isn't displaying the files at the template
>>>>>>> <https://groups.google.com/d/msgid/django-users/8d7b9f60-9f88-41c6-8fec-cf858d7e6574%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>> 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...@googlegroups.com.
>>>>>> To post to this group, send email to django...@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/CAHLKn714iMAzyE%3DaXn_tyJQos-xLWPrWS6BEfFq013%3DS-rmShA%40mail.gmail.com
>>>>>> <https://groups.google.com/d/msgid/django-users/CAHLKn714iMAzyE%3DaXn_tyJQos-xLWPrWS6BEfFq013%3DS-rmShA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>> 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...@googlegroups.com.
>>>>> To post to this group, send email to django...@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/CAO87g13Nmssbp_VPv6ktJmpRrSOasusm%2BvPnDhKGZe_o9yBZ%2BQ%40mail.gmail.com
>>>>> <https://groups.google.com/d/msgid/django-users/CAO87g13Nmssbp_VPv6ktJmpRrSOasusm%2BvPnDhKGZe_o9yBZ%2BQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> 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...@googlegroups.com.
>>>> To post to this group, send email to django...@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/CAHLKn70NnEcJLNH5-8nnc%2B90TaTx3iqCiXuiVAdd6xop_teX7g%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/django-users/CAHLKn70NnEcJLNH5-8nnc%2B90TaTx3iqCiXuiVAdd6xop_teX7g%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> 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...@googlegroups.com.
>>> To post to this group, send email to django...@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/CAO87g10CtF%3Dj7C5ZbGYSKX1J7DFRNPDgfbLBAwNVXhWYPmdS0w%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-users/CAO87g10CtF%3Dj7C5ZbGYSKX1J7DFRNPDgfbLBAwNVXhWYPmdS0w%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
Reply all
Reply to author
Forward
0 new messages