Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, December 21, 2015

Building a list in Django templates

Building a list in Django templates


With this code:

{% for o in [1,2,3] %}      
{% cycle 'row1' 'row2' %}
{% endfor %}

I get a TemplateSyntaxError:

Could not parse the remainder: '[1,2,3]' from '[1,2,3]'  

Is there a way of building a list in a template?

Answer by Peter Rowell for Building a list in Django templates


drodger is correct, you can't do that in the deliberately-crippled Django template lanuage. Either pass in the list as a context variable when you invoke the template or try a template tag like expr. Then you can say {% expr [1,2,3] as my_list %} and then use my_list in your for loop.

Answer by Dominic Rodger for Building a list in Django templates


You can do it via cunning use of the make_list filter, but it's probably a bad idea:

{% for o in "123"|make_list %}      
{% cycle 'row1' 'row2' %}
{% endfor %}

p.s. You don't seem to be using o anywhere, so I'm not sure what you're trying to do.

Answer by Zulu for Building a list in Django templates


We can use split method on str object :

page.html :

{% with '1 2 3' as list %}    {% for i in list.split %}      {{ i }}
{% endfor %} {% endwith %}

Results :

1  2  3  

Answer by Alex Whittemore for Building a list in Django templates


The other answers here look like the ticket (at least for what I wanted), so I'll provide an answer as to WHY you might want to do something like this (and perhaps there's a better answer for my case than what's been provided):

I came across this question looking for a way to build 3 very similar, but not identical buttons using Bootstrap. One button might look like

  

where the difference between buttons is limited to the text of the button (Modality, on its own line above) and the contents of the pertaining to the button, which we'll assume is filled dynamically by JS (referencing id="Modality").

If I need to make 10 of these, copy/pasting the HTML seems dumb and tedious, especially if I want to change anything about my button after the fact (like making all of them split-drop-downs) and it goes against DRY.

So, instead, in the template I could do something like

{% with 'Modality Otherbutton Thirdbutton' as list %}    {% for i in list.split %}          {% endfor %}  {% endwith %}  

Now, granted, in this particular case the buttons add functionality to some related data grid, so the button names could be dynamically filled from django model-sourced data as well, but I'm not at that stage in my design right now, and you can see where this sort of functionality is desirable to maintain DRY.

Answer by Jonathan Liuti for Building a list in Django templates


The simplest is to do

{% for x in "123" %}  

Answer by rabbit.aaron for Building a list in Django templates


It's probably a bit too late now. I made this template tag to achieve this goal.

from django import template  register = template.Library()    # you might want to use simple_tag if you are on 1.9 or higher version  @register.assignment_tag  def to_list(*args):      return args  

to use it in template:

{% load your_template_tag_file %}  {% to_list 1 2 3 4 5 "yes" as my_list %}  {% for i in my_list %}      {{ i }}  {% endfor %}  

Reference here: Django assignment tags


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.