开发者

Making a form with variable no of checkboxes in Django

Im making a site where people can take tests in Django.

If the user mentions that he wants to take a particular test, I transfer it to the view function corresponding to that test. In that view function, I generate the list of questions using the model for that particular test,and pass it in the Httpresponse function to an html file.

What I want to do is generate a variable no of checkboxes , n*4 where n = no of questions in the test. For each checkbox, the name attribute has to be set. For example, i could set them as t1, t2 .. tz. When the user clicks submit, it goes to another page where i check the answers.

The template language in Django, is similar to python, but doesnt allow for assignment of variables. How would I assign names to checkboxes then, as I dont know 开发者_如何学编程the no of checkboxes beforehand?


If you're using a for loop to display your questions you can use the forloop.counter template filter to get access to a counter variable that will allow you to enumerate the checkboxes. For example,

{% for question in questions_list %}
    {{ question }}
    <input type="checkbox" name="t{{ forloop.counter }}"/>
{% endfor %}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜