{"id":95,"date":"2010-03-25T19:56:50","date_gmt":"2010-03-25T23:56:50","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=95"},"modified":"2010-03-25T19:56:50","modified_gmt":"2010-03-25T23:56:50","slug":"accessing-the-value-of-a-form-field-in-a-template","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2010\/03\/accessing-the-value-of-a-form-field-in-a-template\/","title":{"rendered":"Accessing the Value of a Form Field in a Template"},"content":{"rendered":"<p>(This is a &#8216;Note to Self&#8217; post)<\/p>\n<p>I&#8217;ve been trying for half the day to get radio buttons in a custom template to work.  The buttons are generated in a for loop that builds a table row for each choice (iterating over a queryset called <code>cls<\/code>), including this code for the row&#8217;s radio button:<\/p>\n<pre>input type=\"radio\" name=\"reg_choice_1\" value=\"{{ cls.id }}\"\r\n {% ifequal srform.reg_choice_1 cls.id %}CHECKED{% endifequal %}<\/pre>\n<p>The goal is to have the <code>ifequal<\/code> test tell me if the row&#8217;s <code>cls.id<\/code> matches the value set in the form.  However, I didn&#8217;t know how to access that value.  After many Google searches, perusal of the Django documentation, and even a failed attempt to follow the template rendering code, I was stumped.  As a wild guess, I tried the <code>.data<\/code> attribute, and it worked! Here&#8217;s the updated (and functional) code:<\/p>\n<pre>input type=\"radio\" name=\"reg_choice_1\" value=\"{{ cls.id }}\" \r\n{% ifequal srform.reg_choice_1.data cls.id %}CHECKED{% endifequal %}<\/pre>\n<p>I hope that I&#8217;ve put enough keywords in this post so that future stumped Djangonauts can find some help.<\/p>\n<p>As always, leave your comments below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(This is a &#8216;Note to Self&#8217; post) I&#8217;ve been trying for half the day to get radio buttons in a custom template to work. The buttons are generated in a for loop that builds a table row for each choice (iterating over a queryset called cls), including this code for the row&#8217;s radio button: input &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2010\/03\/accessing-the-value-of-a-form-field-in-a-template\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Accessing the Value of a Form Field in a Template&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":7,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}