<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iXce's blog &#187; Django</title>
	<atom:link href="http://guillaume.segu.in/blog/category/code/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://guillaume.segu.in/blog</link>
	<description>Stuff that doesn’t matter</description>
	<lastBuildDate>Mon, 28 Feb 2011 22:28:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Tip of the day: Dynamic forms with Django newforms</title>
		<link>http://guillaume.segu.in/blog/home/116/tip-of-the-day-dynamic-forms-with-django-newforms/</link>
		<comments>http://guillaume.segu.in/blog/home/116/tip-of-the-day-dynamic-forms-with-django-newforms/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 21:03:57 +0000</pubDate>
		<dc:creator>iXce</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[~home]]></category>

		<guid isPermaLink="false">http://guillaume.segu.in/blog/?p=116</guid>
		<description><![CDATA[Building dynamic forms with Django newforms module is quite undocumented, though it&#8217;s quite easy to do. All you need to do is to hook up the __init__ function of the form, raise the __init__ to the parent forms.Form class and then add your dynamically generated fields to self.fields dict. Here is a quick snippet demonstrating [...]]]></description>
			<content:encoded><![CDATA[<p>Building dynamic forms with Django newforms module is quite undocumented, though it&#8217;s quite easy to do. All you need to do is to hook up the __init__ function of the form, raise the __init__ to the parent forms.Form class and then add your dynamically generated fields to self.fields dict.</p>
<p>Here is a quick snippet demonstrating it, which will create a form with n integer fields named from 0 to (n &#8211; 1), but you will easily be able to heavily extend it.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django <span style="color: #ff7700;font-weight:bold;">import</span> newforms <span style="color: #ff7700;font-weight:bold;">as</span> forms
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyForm <span style="color: black;">&#40;</span>forms.<span style="color: black;">Form</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, n, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        forms.<span style="color: black;">Form</span>.<span style="color: #0000cd;">__init__</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, n<span style="color: black;">&#41;</span>:
            field = forms.<span style="color: black;">IntegerField</span> <span style="color: black;">&#40;</span>label = <span style="color: #483d8b;">&quot;%d&quot;</span> <span style="color: #66cc66;">%</span> i, required = <span style="color: #008000;">True</span>,
                                        min_value = <span style="color: #ff4500;">0</span>, max_value = <span style="color: #ff4500;">200</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">fields</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;%d&quot;</span> <span style="color: #66cc66;">%</span> i<span style="color: black;">&#93;</span> = field</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://guillaume.segu.in/blog/home/116/tip-of-the-day-dynamic-forms-with-django-newforms/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

