How to concatenate two variables in a template for evaluation..

6,032 views
Skip to first unread message

Hayyan Rafiq

unread,
Aug 9, 2011, 5:19:32 PM8/9/11
to django...@googlegroups.com
hi I have been playing around with a certain scenario
Consider the following

{% for title in field %}
              <td>{{obj}}{{.title}}</td>
{% endfor %}

Here obj is an object
and title are the various fields of that object.
How can i evaluate the entire thing in a template

Example  
I want it to end up like this
{{obj.title}}
How can i do that from my previous code..
Any suggestions would be appreciated...

Shawn Milochik

unread,
Aug 9, 2011, 5:25:42 PM8/9/11
to django...@googlegroups.com
You have to write it as {{ obj.title }} in your template. Django
templates are designed to allow very little in the way of logic; that
should take place in your views.

Hayyan Rafiq

unread,
Aug 9, 2011, 5:37:25 PM8/9/11
to django...@googlegroups.com
Okay then which way do u recommend that i should follow in case
i need to displays objects with different no of properties
Example obj has propA and PropB
{{obj.propA}}
{{obj.propB}}
would work but i want it to be dynamic since the no and name of properties may vary..
I tried writing all the properties of an object in a list and passing that list obj to template
but it seems this is not just possible.. since
            {% for prop in properties %}
              <td>{{obj}}+{{.prop}}</td>
             {% endfor %}
or other things similar to above do not work

Any suggestions..

> Date: Tue, 9 Aug 2011 17:25:42 -0400
> Subject: Re: How to concatenate two variables in a template for evaluation..
> From: sh...@milochik.com
> To: django...@googlegroups.com

>
> You have to write it as {{ obj.title }} in your template. Django
> templates are designed to allow very little in the way of logic; that
> should take place in your views.
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

bruno desthuilliers

unread,
Aug 9, 2011, 6:35:24 PM8/9/11
to Django users
On 9 août, 23:37, Hayyan Rafiq <hayya...@hotmail.com> wrote:
> Okay then which way do u recommend that i should follow in case
> i need to displays objects with different no of properties
> Example obj has propA and PropB
> {{obj.propA}}
> {{obj.propB}}
> would work but i want it to be dynamic since the no and name of properties may vary..

Then you need a custom template filter. Something like this should do
the trick:

# in your templatetags module:

@register.filter
def get_attribute(obj, name):
return getattr(obj, name, u"")

# in your template

{% load youtemplatetags %}

{% for attrname in attributes %}
{{ obj|get_attribute:attrname }}
{% endor %}


rishi sijariya

unread,
May 20, 2015, 11:05:11 AM5/20/15
to django...@googlegroups.com, hayy...@hotmail.com
Hi Hayyan Rafig,

     did you find your solution of adding two variable , if yes pls share with us, or any one can help pls

Luis Zárate

unread,
May 21, 2015, 11:53:16 PM5/21/15
to django...@googlegroups.com
Mmm maybe creating a tag https://docs.djangoproject.com/en/1.8/howto/custom-template-tags/#writing-custom-template-tags


In template {% mytag obj prop %}

In tag function

def mytag(obj, prop):
     return getattr(obj, prop)
> 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/bbe5399f-15e3-43b5-ae7d-d79e5f54191b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Reply all
Reply to author
Forward
0 new messages