views:

68

answers:

2

Hello,

I am using templates with django. I am having a problem where the Context is not being rendered. The meta_k is null. The meta_description is not.

 t = get_template('projects.html')   
 html = t.render(Context({
       'completed': completed, 
       'current':current, 
       'description': sp.description, 
       'project_title':sp.name, 
       'img':images, 
       'meta_desc': sp.meta_description, 
       'meta_k:': sp.meta_keywords
 }))

I can start the server in debug mode in eclipse and So I know sp.meta_keywords is not null. Here is where I call the code in projects.html:

{% block meta_keywords %}<br>
{% if meta_k %}<br>
&nbsp;&nbsp;&nbsp;&nbsp;{{ meta_k }}<br>
{% else %}<br>
&nbsp;&nbsp;&nbsp;&nbsp;Venkat, Rao, engineer, inventor, entrepreneur, projects, blue dart, control systems, labview<br>
{% endif %}<br>
{% endblock %}

This defaults to the else when I know meta_k should not be null. The complete code can be found here on Google Code.

What am I doing wrong?

+1  A: 

Only suggestion for you is that most probably it is bug in your code, for us it will be difficult to debug without running your whole project.

So i suggest you experiment on command line and see if you can replicate the bug in simple steps, so that we can try to fix it. I am sure in the process you will find the problematic part

e.g. I see your template rendered correctly by my simple context

>>> from django.template import Context, Template
>>> s = """{% block meta_keywords %}<br>
... {% if meta_k %}<br>
... &nbsp;&nbsp;&nbsp;&nbsp;{{ meta_k }}<br>
... {% else %}<br>
... &nbsp;&nbsp;&nbsp;&nbsp;Venkat, Rao, engineer, inventor, entrepreneur, projects, blue dart, control systems, labview<br>
... {% endif %}<br>
... {% endblock %}"""
>>> t = Template(s)
>>> c = Context({'meta_k':['a','b','c']})
>>> t.render(c)
u'<br>\n<br>\n&nbsp;&nbsp;&nbsp;&nbsp;[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<br>\n<br>\n'
Anurag Uniyal
Possible reasons: As `{% if meta_k %}` branch is not getting executed means meta_k somehow evaluate to False, either meta_k is empty or a object which is evaluating to false, try passing "%s"%sp.meta_keywords instead and see what is the result
Anurag Uniyal
A: 

So I was just making stupid mistake:

in the rendering file i have: html = t.render(Context({'completed': completed, 'current':current, 'description': sp.description, 'project_title':sp.name, 'img':images, 'meta_desc': sp.meta_description, 'meta_k:': sp.meta_keywords) this refers to "meta_k:" note the semicolon

in the template I have {% if meta_k %} note no semicolon

if i remove the semicolon it works. that was stupid.

Venkat S. Rao
yes mostly it is, that is why if you try to make problem simpler and pinpoint the area where the problem is eventually you will find the reason, otherwise staring at a complex bunch of code and thinking seems like a bug in django doesn't help
Anurag Uniyal