简体   繁体   English

Django模板无法呈现完整的上下文

[英]django template does not render complete context

I am using templates with django. 我正在使用带有django的模板。 I am having a problem where the Context is not being rendered. 我有一个问题,其中不呈现上下文。 The meta_k is null. meta_k为空。 The meta_description is not. meta_description不是。

 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. 我可以在Eclipse中以调试模式启动服务器,因此我知道sp.meta_keywords不为null。 Here is where I call the code in projects.html: 这是我在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. 当我知道meta_k不应该为null时,默认为else。 The complete code can be found here on Google Code. 完整代码可在此处在Google Code上找到。

What am I doing wrong? 我究竟做错了什么?

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 我相信在此过程中您会发现有问题的部分

eg 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'

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 这是指“ meta_k:” 注意分号

in the template I have 在我有的模板中

{% if meta_k %}

note no semicolon 注意没有分号

If I remove the semicolon it works. 如果我删除分号,它将起作用。 That was stupid. 那真是愚蠢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM