<?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>code.random() &#187; newforms</title>
	<atom:link href="http://code.pui.ch/category/newforms/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.pui.ch</link>
	<description>Random code-snippets and tips</description>
	<lastBuildDate>Thu, 19 Jan 2012 07:09:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using djangos newforms</title>
		<link>http://code.pui.ch/2007/01/07/using-djangos-newforms/</link>
		<comments>http://code.pui.ch/2007/01/07/using-djangos-newforms/#comments</comments>
		<pubDate>Sun, 07 Jan 2007 20:34:31 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[newforms]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/01/07/using-djangos-newforms/</guid>
		<description><![CDATA[If you start a django-app just now (January 2007) you are in a dilemma since using the old form engine is discouraged:

If you're starting from scratch, we strongly encourage you not to waste your time learning this

But then, the documentation of new form engine, called "newforms", isn't complete at all.
You may argue &#171;take a framework [...]]]></description>
			<content:encoded><![CDATA[<p>If you start a django-app just now (January 2007) you are in a dilemma since <a href="http://www.djangoproject.com/documentation/forms/#forwards-compatibility-note">using the old form engine is discouraged</a>:</p>
<blockquote><p>
If you're starting from scratch, we strongly encourage you not to waste your time learning this
</p></blockquote>
<p>But then, the documentation of new form engine, called "newforms", <a href="http://www.djangoproject.com/documentation/newforms/#more-coming-soon">isn't complete at all</a>.</p>
<p>You may argue &laquo;take a framework above 1.0&raquo;, and you'd probably be right. As I wanted to stick with django, I implemented form handling the hard way by figuring out how to use newforms.</p>
<p>Anyway, as I finally got formhandling working with newforms, I thought I'd share this with you, my fellow readers..</p>
<p>The following code..</p>
<div style="float: right"><img src="/wp-content/upload/newforms_screenshot.png" alt="screenshot of the form" /><br />
this examples' form after validating user input</div>
<ul>
<li>..displays a form</li>
<li>..validates user input</li>
<li>..shows validation errors</li>
<li>..writes the user data into the database</li>
</ul>
<p><span id="more-4"></span><br />
<strong>Update:</strong> Newforms has been added to Django 0.96 and the api has itself proved to be quite stable. The following examples work out of the box with Django 0.96 so you don't need the patches I mentioned before.</p>
<h3>Model</h3>
<div class="igBar"><span id="lpython-6"><a href="#" onclick="javascript:showPlainTxt('python-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-6">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">class</span> Entry<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; title = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">"Titel"</span>, maxlength=<span style="color: #ff4500;color:#800000;">100</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; detail = models.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; owner = models.<span style="color: black;">ForeignKey</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">User</span>, verbose_name=<span style="color: #483d8b;">"Verfasser"</span>, editable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; added = models.<span style="color: black;">DateTimeField</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">"hinzugefügt am"</span>, editable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; changed = models.<span style="color: black;">DateTimeField</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">"geändert am"</span>, editable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; public = models.<span style="color: black;">BooleanField</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">"Öffentlich"</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; category = models.<span style="color: black;">ForeignKey</span><span style="color: black;">&#40;</span>Category, verbose_name=<span style="color: #483d8b;">"Kategorie"</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">def</span> save<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: #008000;">id</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">added</span> = <span style="color: #dc143c;">datetime</span>.<span style="color: black;">date</span>.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">self</span>.<span style="color: black;">changed</span> = <span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">today</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>Entry, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Note that with the <code>editable</code> parameter I define which fields can be edited</p>
<h3>Create</h3>
<div class="igBar"><span id="lpython-7"><a href="#" onclick="javascript:showPlainTxt('python-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-7">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">from</span> django <span style="color: #ff7700;font-weight:bold;">import</span> newforms as forms</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">newforms</span> <span style="color: #ff7700;font-weight:bold;">import</span> widgets</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> add_entry<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; EntryForm = forms.<span style="color: black;">models</span>.<span style="color: black;">form_for_model</span><span style="color: black;">&#40;</span>Entry<span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; EntryForm.<span style="color: black;">base_fields</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'detail'</span><span style="color: black;">&#93;</span>.<span style="color: black;">widget</span> = TinyMCE<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">method</span> == <span style="color: #483d8b;">'POST'</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; form = EntryForm<span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> form.<span style="color: black;">is_valid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry = form.<span style="color: black;">save</span><span style="color: black;">&#40;</span>commit=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.<span style="color: black;">owner</span> = request.<span style="color: #dc143c;">user</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseRedirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">"/"</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; form = EntryForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; t = loader.<span style="color: black;">get_template</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'add_entry.html'</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c = Context<span style="color: black;">&#40;</span><span style="color: black;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'form'</span>: form,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'html_head'</span>: <span style="color: #483d8b;">'&lt;script src=&quot;/media/tiny_mce/tiny_mce_src.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;'</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>t.<span style="color: black;">render</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This code displays the form if called by GET, otherwise its inserting the data (that is: creates an entry) in the database.</p>
<p>A few explanations:</p>
<ul>
<li>line 5: the model is converted into a form class by calling <code>forms.model.form_for_model</code>.</li>
<li>line 6: I explicitely want to turn the detail form field into a richtext field (the widget code is <a href="http://code.djangoproject.com/wiki/CustomWidgetsTinyMCE">taken from here</a>). In future version, there might be the possibility to specify the widget in the model by setting a <code>form_widget=</code> argument.</li>
<li>line 11: to alter/extend the data before writing it into the db, <del>use <code>form.clean_data</code></del> use <code>commit=False</code> and alter the returned model instance and save this instance into the db.<br />form.save() returns a model instance also without setting commit to False, this comes in handy if you want to access the just generated primary key.</li>
</ul>
<p>The rest should be very straightforward.</p>
<h3>Update</h3>
<div class="igBar"><span id="lpython-8"><a href="#" onclick="javascript:showPlainTxt('python-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-8">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> edit_entry<span style="color: black;">&#40;</span>request, <span style="color: #008000;">id</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; entry = Entry.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>=<span style="color: #008000;">id</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ;background-color: #ffd;"">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; EntryForm = forms.<span style="color: black;">models</span>.<span style="color: black;">form_for_instance</span><span style="color: black;">&#40;</span>entry<span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; EntryForm.<span style="color: black;">fields</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'detail'</span><span style="color: black;">&#93;</span>.<span style="color: black;">widget</span> = TinyMCE<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">method</span> == <span style="color: #483d8b;">'POST'</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; form = EntryForm<span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> form.<span style="color: black;">is_valid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseRedirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">"/"</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; form = EntryForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; t = loader.<span style="color: black;">get_template</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'add_entry.html'</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c = Context<span style="color: black;">&#40;</span><span style="color: black;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'form'</span>: form,</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'html_head'</span>: <span style="color: #483d8b;">'&lt;script src=&quot;/media/tiny_mce/tiny_mce_src.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>t.<span style="color: black;">render</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Notice that the only differences to the create view are in line 2/3 (form_for_model becomes form_for_instance) and in omitting the detour via commit=False (since we don't want to set the owner when updating the entry).<br />
Since that's not DRY at all, lets merge those two functions into one.</p>
<h3>Create and Update together freehand without a net</h3>
<div class="igBar"><span id="lpython-9"><a href="#" onclick="javascript:showPlainTxt('python-9'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-9">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> add_edit_entry<span style="color: black;">&#40;</span>request, <span style="color: #008000;">id</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">id</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; EntryForm = forms.<span style="color: black;">models</span>.<span style="color: black;">form_for_model</span><span style="color: black;">&#40;</span>Entry<span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; entry = Entry.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>=<span style="color: #008000;">id</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; EntryForm = forms.<span style="color: black;">models</span>.<span style="color: black;">form_for_instance</span><span style="color: black;">&#40;</span>entry<span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; EntryForm.<span style="color: black;">fields</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'detail'</span><span style="color: black;">&#93;</span>.<span style="color: black;">widget</span> = TinyMCE<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">method</span> == <span style="color: #483d8b;">'POST'</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; form = EntryForm<span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> form.<span style="color: black;">is_valid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry = form.<span style="color: black;">save</span><span style="color: black;">&#40;</span>commit=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">id</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.<span style="color: black;">owner</span> = request.<span style="color: #dc143c;">user</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseRedirect<span style="color: black;">&#40;</span><span style="color: #483d8b;">"/"</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; form = EntryForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; t = loader.<span style="color: black;">get_template</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'add_entry.html'</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c = Context<span style="color: black;">&#40;</span><span style="color: black;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'form'</span>: form,</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #483d8b;">'html_head'</span>: <span style="color: #483d8b;">'&lt;script src=&quot;/media/tiny_mce/tiny_mce_src.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;'</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>t.<span style="color: black;">render</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>That's it. This code is all you need to display an empty form, a form populated with the data from the db, validating user input, displaying an error page and writing the user input into db. And furthermore you have complete control over the whole process so you could add a bell or whistle here and there as I did with setting the owner of an entry.</p>
<p>I bet this gives an idea how powerful and yet simple newforms is. I'm looking forward to the implementation in Django 1.0!</p>
<p>P.S. For completeness, the template that displays the form:</p>
<div class="igBar"><span id="lcode-10"><a href="#" onclick="javascript:showPlainTxt('code-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-10">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span>% extends <span style="color:#CC0000;">"base.html"</span> %<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span>% block content %<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;form action=<span style="color:#CC0000;">"."</span> method=<span style="color:#CC0000;">"post"</span>&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &lt;table class=<span style="color:#CC0000;">"form"</span>&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#123;</span> form <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &lt;/table&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &lt;input type=<span style="color:#CC0000;">"submit"</span> value=<span style="color:#CC0000;">"speichern"</span> /&gt;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;/form&gt;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span>% endblock %<span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/01/07/using-djangos-newforms/feed/</wfw:commentRss>
		<slash:comments>63</slash:comments>
		</item>
	</channel>
</rss>

