0

i have the following piece of code:

                <h3 style="margin: 0px; margin-bottom: 20px;">Click the checkboxes for more subscriptions</h3>
                {% for keyword in keyword_list %}
                    {% if keyword.keyword_name == userprofile.keywords_subscribed  %}
                        <input type="checkbox" disabled="disabled" name="keywords" value="keywords"/>
                        {{keyword.keyword_name}}
                        <br />
                    {% else %}
                        <input type="checkbox" name="cb" value="keywords"  />
                        {{keyword.keyword_name}}
                        <br />
                    {% endif %}
                {% endfor %}

Right now it just displays a checkbox of keywords one by one. I was wondering if there is anyway which i can turn this into a table form.

I need the table to be dynamic because the number of keywords in the list would keep on expanding.

I've tried to come up with some solutions with using the keyword id that is stored in the database but that's a tedious method.

Any other efficient method that i may have missed out?

This is a mixture of django and html so don't be alarmed by the unique terms. :p

Thanks for helping! :D

1

1 Answer 1

3

Why not just do it explicitely?

            <h3 style="margin: 0px; margin-bottom: 20px;">Click the checkboxes for more subscriptions</h3>
            <table>
              <tbody>
                {% for keyword in keyword_list %}
                  <tr>
                    <td>
                      {% if keyword.keyword_name == userprofile.keywords_subscribed  %}
                        <input type="checkbox" disabled="disabled" name="keywords" value="keywords"/>
                      {% else %}
                        <input type="checkbox" name="cb" value="keywords"  />
                      {% endif %}
                      {{keyword.keyword_name}}
                    </td>
                  </tr>
                {% endfor %}
               </tbody>
            </table>

If you want the table to grow horizontally just move <tr></tr> tags outside the outer for.

For further deployment you may want to use Django Forms, with custom templates packed into a FormsSet.

EDIT:

If you want a N-column layout (3-columns for example), you can access a forloop.counter variable:

            <h3 style="margin: 0px; margin-bottom: 20px;">Click the checkboxes for more subscriptions</h3>
            <table>
              <tbody>
                {% for keyword in keyword_list %}
                  {% if forloop.counter|divisibleby:"3" %}
                    <tr>
                  {% endif}
                    <td>
                      {% if keyword.keyword_name == userprofile.keywords_subscribed  %}
                        <input type="checkbox" disabled="disabled" name="keywords" value="keywords"/>
                      {% else %}
                        <input type="checkbox" name="cb" value="keywords"  />
                      {% endif %}
                      {{keyword.keyword_name}}
                    </td>
                  {% if forloop.counter|add:"1"|divisibleby:"3" %}
                    </tr>
                  {% endif}
                {% endfor %}
               </tbody>
            </table>

Code above works only when the length of a list of keywords is divisible by three, but it illustrates the general idea. To fix thath you can for example append empty items to the list, to make it satisfy that condition. A custom filter which do so may be a good idea.

If you want something more sophisticated it's again a play with if conditions and forloop variables.

For something very sophisticated it may be better to write a custom filter or a template tag, in order to make your template file clearer.

Sign up to request clarification or add additional context in comments.

3 Comments

thatnks zoltak, but what i really want is to display the list into let's say three columns. coding it explicitly will work if i want my list to be displayed in one column only.
to add on, like if the program displays out 10 keywords in the first column, it will automatically display the list of keywords in the next column. is that even possible?
thank you so much. I've now got a better understanding of how tables work. Thanks again! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.