snipt

Ctrl+h for KB shortcuts
#4322

Python

how to generate a dynamic at runtime form in Django

"""
----------------------------------------------------------------
how to generate a dynamic at runtime form in Django
----------------------------------------------------------------
idea taken from http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/
"""

def make_contact_form(user):
    fields = { 'name': forms.CharField(max_length=50),
               'email': forms.EmailField(),
               'message': forms.CharField(widget=forms.Textarea) }
    if not user.is_authenticated:
        fields['captcha'] = CaptchaField()
    return type('ContactForm', [forms.BaseForm], { 'base_fields': fields })

"""
That’s much nicer; type just wants to know the name of our new class (ContactForm), what classes, if any, it should inherit from (when doing this, use django.forms.BaseForm instead of django.forms.Form; the reasons are a bit obscure and technical, but any time you’re not declaring the fields directly on the class in the normal fashion you should use BaseForm) and a dictionary of attributes. Recall from our earlier look at fiddling with a form’s fields that base_fields is the attribute which holds the baseline set of fields for the form class, so our attribute dictionary simply supplies base_fields with a collection of form fields, and adds the CAPTCHA only when needed.

This is a bit more advanced in terms of Python programming, but is an extremelyuseful technique; Django uses it all over the place to build classes on the fly, and you can too. 
"""
https://snipt.net/embed/c471c7604f95c61d990d2d9b618bab41/
https://snipt.net/raw/c471c7604f95c61d990d2d9b618bab41/
c471c7604f95c61d990d2d9b618bab41
python
Python
20
2014-04-19T23:13:13
True
False
/api/public/snipt/4322/
how-to-generate-a-dynamic-at-runtime-form-in-django
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a> <a href="#L-19">19</a> <a href="#L-20">20</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="sd">&quot;&quot;&quot;</span> </span><span id="L-2"><a name="L-2"></a><span class="sd">----------------------------------------------------------------</span> </span><span id="L-3"><a name="L-3"></a><span class="sd">how to generate a dynamic at runtime form in Django</span> </span><span id="L-4"><a name="L-4"></a><span class="sd">----------------------------------------------------------------</span> </span><span id="L-5"><a name="L-5"></a><span class="sd">idea taken from http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/</span> </span><span id="L-6"><a name="L-6"></a><span class="sd">&quot;&quot;&quot;</span> </span><span id="L-7"><a name="L-7"></a> </span><span id="L-8"><a name="L-8"></a><span class="k">def</span> <span class="nf">make_contact_form</span><span class="p">(</span><span class="n">user</span><span class="p">):</span> </span><span id="L-9"><a name="L-9"></a> <span class="n">fields</span> <span class="o">=</span> <span class="p">{</span> <span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">),</span> </span><span id="L-10"><a name="L-10"></a> <span class="s">&#39;email&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">EmailField</span><span class="p">(),</span> </span><span id="L-11"><a name="L-11"></a> <span class="s">&#39;message&#39;</span><span class="p">:</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">widget</span><span class="o">=</span><span class="n">forms</span><span class="o">.</span><span class="n">Textarea</span><span class="p">)</span> <span class="p">}</span> </span><span id="L-12"><a name="L-12"></a> <span class="k">if</span> <span class="ow">not</span> <span class="n">user</span><span class="o">.</span><span class="n">is_authenticated</span><span class="p">:</span> </span><span id="L-13"><a name="L-13"></a> <span class="n">fields</span><span class="p">[</span><span class="s">&#39;captcha&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">CaptchaField</span><span class="p">()</span> </span><span id="L-14"><a name="L-14"></a> <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="s">&#39;ContactForm&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">forms</span><span class="o">.</span><span class="n">BaseForm</span><span class="p">],</span> <span class="p">{</span> <span class="s">&#39;base_fields&#39;</span><span class="p">:</span> <span class="n">fields</span> <span class="p">})</span> </span><span id="L-15"><a name="L-15"></a> </span><span id="L-16"><a name="L-16"></a><span class="sd">&quot;&quot;&quot;</span> </span><span id="L-17"><a name="L-17"></a><span class="sd">That’s much nicer; type just wants to know the name of our new class (ContactForm), what classes, if any, it should inherit from (when doing this, use django.forms.BaseForm instead of django.forms.Form; the reasons are a bit obscure and technical, but any time you’re not declaring the fields directly on the class in the normal fashion you should use BaseForm) and a dictionary of attributes. Recall from our earlier look at fiddling with a form’s fields that base_fields is the attribute which holds the baseline set of fields for the form class, so our attribute dictionary simply supplies base_fields with a collection of form fields, and adds the CAPTCHA only when needed.</span> </span><span id="L-18"><a name="L-18"></a> </span><span id="L-19"><a name="L-19"></a><span class="sd">This is a bit more advanced in terms of Python programming, but is an extremelyuseful technique; Django uses it all over the place to build classes on the fly, and you can too. </span> </span><span id="L-20"><a name="L-20"></a><span class="sd">&quot;&quot;&quot;</span> </span></pre></div> </td></tr></table>
django, forms