title: Conditional field in form
body: <p>I need to make a Form class that may or not have a ReCaptcha field depending on whether the user is logged in or not.</p>
  
  <p>Because this is a CommentForm, I have no access to the <code>request</code> object on form creation/definition, so I can't rely on that.</p>
  
  <p>For the <code>POST</code> request the solution is easy: I've got this:</p>
  
  <pre><code>class ReCaptchaCommentForm(CommentForm):
      def __init__(self, data=None, *args, **kwargs):
          super(ReCaptchaCommentForm, self).__init__(data, *args, **kwargs)
          if data and 'recaptcha_challenge_field' in data:
              self.fields['captcha'] = ReCaptchaField()
  </code></pre>
  
  <p>Having done this, form validation should work as intended. The problem now is on the template side. I need the template to be like this:</p>
  
  <pre><code>&lt;form action={% comment_form_target %} method="post"&gt;
  {# usual form stuff #}
  {% if not user.is_authenticated %}
  &lt;script  type="text/javascript"
           src="http://www.google.com/recaptcha/api/js/recaptcha_ajax.js"&gt;&lt;/script&gt;
  &lt;div id="recaptcha-div"&gt;&lt;/div&gt;
  &lt;script type="text/javascript"&gt;
    Recaptcha.create({{ public_key }}, "recaptcha-div",
                     { theme: 'white',
                       callback: Recaptcha.focus_response_field });
  &lt;/script&gt;
  {% endif %}
  &lt;/form&gt;
  </code></pre>
  
  <p>But I'd like not to have to repeat that code on every <code>comments/*/form.html</code> template. I gather there should be some way of adding equivalent code from a widget's <code>render</code> method and <code>Media</code> definition.</p>
  
  <p>Can anyone think of a nice way to do this?</p>