<?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>Web-In-Sight &#187; form</title>
	<atom:link href="http://web-in-sight.nl/tag/form/feed/" rel="self" type="application/rss+xml" />
	<link>http://web-in-sight.nl</link>
	<description>Inzicht in internet en werken</description>
	<lastBuildDate>Mon, 30 Jan 2012 09:00:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Django FormWizard best .. well .. useful practices</title>
		<link>http://web-in-sight.nl/2010/02/24/django-formwizard-best-well-useful-practices/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=django-formwizard-best-well-useful-practices</link>
		<comments>http://web-in-sight.nl/2010/02/24/django-formwizard-best-well-useful-practices/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 11:10:56 +0000</pubDate>
		<dc:creator>Gerard</dc:creator>
				<category><![CDATA[All ENGLISH articles]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[wizard]]></category>

		<guid isPermaLink="false">http://www.gerardjp.com/?p=1231</guid>
		<description><![CDATA[Django FormWizard usefulness: emulating decorators .. more to come <a href="http://web-in-sight.nl/2010/02/24/django-formwizard-best-well-useful-practices/">Lees verder <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>Edit</em>: After some re-writes of this post it is finally setup in a manor that is clean and failsafe.</p>
<p>After researching and setting up two FormWizards in Django I concluded some things that might be useful to others. Hence this post.</p>
<h2>Having userdata available throughout the wizard</h2>
<p>I&#8217;ve seen some solutions on the net for this but when you handle data from several models over different steps the best way to handle this is using Django&#8217;s session framework. In parse_params() in the beginning and end we repectively get/set data and store our data.</p>
<pre>def parse_params(self, request, *args, **kwargs):
    current_step = self.determine_step(request, *args, **kwargs)
    form = self.get_form(current_step, request.POST)

    # Pickup the dict from the users session or create a new one
    wizdata = request.session.get('wizdata') or {}

    # Your code goes here. E.g. set a value in the dict
    wizdata['some_key'] = 'Some value'

    # Store the possibly changed dict back in the users session.
    request.session['wizdata'] = wizdata
</pre>
<p>So whatever you do with the user data, it&#8217;s safe, and we can access it in any step throughout the wizardry.</p>
<pre>def render_template(self, request, form, previous_fields, step, context=None):
    wizdata = request.session.get('wizdata')

    # Your other code goes here. E.g. a conditional on previously set data.
    if wizdata.get('some_key'):
        # Do something with your data
        wizdata['some_key'] = 'Some other value'

    # And again, simply store the dict in the users session.
    request.session['wizdata'] = wizdata
</pre>
<p>You can imagine that in the parse_params() and the render_template() method you respectively have the &#8216;if current_step == &#8216; or &#8216;if step == &#8216; statements along the way. All having the wizdata dict available.</p>
<p>This also works in the done() method. Simply get the dict one last time from the user session.</p>
<pre>def done(self, request, form_list):
    wizdata = request.session.get('wizdata')

    # Other code here

    # Cleanup
    del request.session['wizdata']
</pre>
<p>Then, for tidyness, at the end of done() we remove the dict from the user session.</p>
<h3>Gotchas</h3>
<p>There can be stale data, in the form of our stored dicts, left behind when wizard sessions are not finished by the user. This also is true for the sessions itself. As stated in the <a title="The Django docs about sessions" href="http://docs.djangoproject.com/en/dev/topics/http/sessions/#clearing-the-session-table" target="_blank">The Django docs about sessions</a> you should clean up the sessions from time to time.</p>
<p>Anybody that has improvements, questions or compliments please drop me a line <img src='http://web-in-sight.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>GrtzG</p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fweb-in-sight.nl%2F2010%2F02%2F24%2Fdjango-formwizard-best-well-useful-practices%2F&amp;send=false&amp;layout=button_count&amp;width=&amp;show_faces=false&amp;action=recommend&amp;colorscheme=light&amp;font=arial&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:21px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://web-in-sight.nl/2010/02/24/django-formwizard-best-well-useful-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

