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! 😀
Why not just do it explicitely?
If you want the table to grow horizontally just move
<tr></tr>tags outside the outerfor.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.countervariable: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.