开发者

Remember radio button selection in Django

开发者 https://www.devze.com 2023-02-27 06:53 出处:网络
Without using javascript, is there a better way to remember a user\'s radio button selection? I feel I have a pretty naive(?) way of doing it?

Without using javascript, is there a better way to remember a user's radio button selection? I feel I have a pretty naive(?) way of doing it?

How can I re-write this to make it more DRY? usersegment parem gets returned on the same page upon submit.

{% if usersegment == "non-paying" %}
<input type="radio" name="usersegment" value="non-paying" ch开发者_运维知识库ecked="yes"/> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" /> Paying Users <br />
<input type="radio" name="usersegment" value="all" /> All Users<br />
{% endif %}

{% if usersegment == "paying" %}
<input type="radio" name="usersegment" value="non-paying" /> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" checked="yes"/> Paying Users <br />
<input type="radio" name="usersegment" value="all" /> All Users<br />
{% endif %}

{% if usersegment == "all" or not usersegment %}
<input type="radio" name="usersegment" value="non-paying" checked="yes"/> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" /> Paying Users <br />
<input type="radio" name="usersegment" value="all" checked="yes"/> All Users<br />
{% endif %} 


How about this:

<input type="radio" name="usersegment" value="non-paying" {% if usersegment == "non-paying" %}checked="checked"{% endif %}/> Non-paying Users <br />
<input type="radio" name="usersegment" value="paying" {% if usersegment == "paying" %}checked="checked"{% endif %}/> Paying Users <br />
<input type="radio" name="usersegment" value="all" {% if usersegment == "all" or not usersegment %}checked="checked"{% endif %}/> All Users<br />

Also, consider using django forms. Trivial problems like those are solved in a very elegant way there, you don't even need to think about it.

0

精彩评论

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