How to: Variable assignment in django template.

8,655 views
Skip to first unread message

Blessed Geek

unread,
Nov 11, 2008, 5:06:27 AM11/11/08
to Google App Engine
I need to perform variable assignment in a django template.
Just to try the viability, I tested it out with the simple django
template version of the guestbook example. But python/eclipse gave me
an error:

<html>
<body>
{% for greeting in greetings %}
{% if greeting.author %}
<b>{{ greeting.author.nickname }}</b> wrote:
{% else %}
An anonymous person wrote:
{% endif %}
{% x = greeting.content %}
{% endfor %}

</body>
</html>


Could someone tell me (I have lots of experience and love for java and
JSP) where I have gone wrong in performing the assignment of x?

How to perform var assignment in django template?

Cannot be done? Then django is lousy. If so, could someone point me to
a better python template technology?

Alexander Kojevnikov

unread,
Nov 11, 2008, 6:00:08 AM11/11/08
to Google App Engine
> How to perform var assignment in django template?

Django templates don't support variable assignments...

> Cannot be done? Then django is lousy. If so, could someone point me to
> a better python template technology?

...but it does not mean it's "lousy". Could you elaborate what you are
trying to do? There's a good chance there's an alternative solution.

Blessed Geek

unread,
Nov 11, 2008, 7:59:25 AM11/11/08
to Google App Engine

> ...but it does not mean it's "lousy". Could you elaborate what you are
> trying to do? There's a good chance there's an alternative solution.

"Please tell us what you want to do and may be there's a better
solution" is a familiar answer we give to newbies in java.

Please accept my apologies for being short in politeness and words.

I know what I want to do. Without going into details just tell me:
How can I perform variable assignment in django template?

If django is incapable, recommend me an alternate python template
(which works with google apps) that would allow me to do so.

Blessed Geek

unread,
Nov 11, 2008, 8:10:17 AM11/11/08
to Google App Engine
Rather, what I meant is

I know what I want to do. Without going into details of what I need to
do just tell me in sufficient details:
How can I perform variable assignment in django template?

Your help and your tolerance of my short attitude is greatly
appreciated. I really need an answer precise to the question.

WalterJJ

unread,
Nov 11, 2008, 9:19:39 AM11/11/08
to Google App Engine
Just brainstorming:
maybe writing a custom tag? http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#a-quick-overview

Best regards,
Walter

WalterJJ

unread,
Nov 11, 2008, 9:55:53 AM11/11/08
to Google App Engine
Setting a context variable in a custom template tag:
http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#setting-a-variable-in-the-context

On 11 nov, 11:19, WalterJJ <walte...@gmail.com> wrote:
> Just brainstorming:
> maybe writing a custom tag?  http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#a-qu...

Mahmoud

unread,
Nov 11, 2008, 11:11:04 AM11/11/08
to Google App Engine
Here are some things you can do:
1. Django templates can tell you the value of the current counter/
index in the 'for' loop. Maybe you can use that somehow.
2. If you really want to assign a variable, then use a javascript tag:
<script type="text/javascript">
var x = {{greeting.content}};
</script>

-Mahmoud

yejun

unread,
Nov 11, 2008, 2:07:35 PM11/11/08
to Google App Engine


> Cannot be done? Then django is lousy. If so, could someone point me to
> a better python template technology?

Mako template can include any arbitrary python code.

Reply all
Reply to author
Forward
0 new messages