开发者

Why can't iterate through this list in a Django template

I have a variable from my view that is the output of a Model.objects.all() call on that Model. I'm passing it to my template in my view, and I'm trying to iterate开发者_开发百科 over it in the template. I can access the first element of it simply by this line of code. 'code' is the name of a field in my django model. This line does print the first element's 'code' attribute correctly.

{{ var_name.0.code }}

However, when I try to iterate over var_name in a template for loop, nothing shows up. I tried the following code:

{% for single_var in var_name %}
    {{ single_var.code }}
{% endfor %}

This isn't actually what I want to do in the for loop, but getting this to work will let me do what I need in the template. It may be noteworthy to add that at the moment this list has only one element in it.

This is for a work project, so that's why I changed the variable names to something generic.


I found that changing the name of single_var to something without an underscore seemed to fix it. This doesn't make a lot of sense to me because the Django template language documentation states the following:

Variable names consist of any combination of alphanumeric characters and the underscore ("_").

Does anyone know why this seemed to fix the problem?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜