<?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>Patrick Arminio&#039;s blog</title>
	<atom:link href="http://patrick.arminio.info/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://patrick.arminio.info/blog</link>
	<description></description>
	<lastBuildDate>Thu, 16 Feb 2012 10:32:21 +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>Fix &#8220;ValueError: unknown locale: UTF-8&#8243; under Mac OS X 10.7 &#8211; Lion</title>
		<link>http://patrick.arminio.info/blog/2012/02/fix-valueerror-unknown-locale-utf8/</link>
		<comments>http://patrick.arminio.info/blog/2012/02/fix-valueerror-unknown-locale-utf8/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 10:28:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://patrick.arminio.info/blog/?p=49</guid>
		<description><![CDATA[Today I was trying the new Django 1.4 Beta 1 and when I run the syncdb command I got this error: &#40;app&#41;patrick@app$ python manage.py syncdb Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_user_permissions Creating table auth_user_groups Creating table auth_user Creating table django_content_type Creating table django_session Creating table django_site Creating [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was trying the new <a href="https://docs.djangoproject.com/en/dev/releases/1.4-beta-1/" target="_blank">Django 1.4 Beta 1</a> and when I run the <em>syncdb </em>command I got this error:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span>app<span style="color: #7a0874; font-weight: bold;">&#41;</span>patrick<span style="color: #000000; font-weight: bold;">@</span>app$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
&nbsp;
You just installed Django<span style="color: #ff0000;">'s auth system, which means you don'</span>t have any superusers defined.
Would you like to create one now? <span style="color: #7a0874; font-weight: bold;">&#40;</span>yes<span style="color: #000000; font-weight: bold;">/</span>no<span style="color: #7a0874; font-weight: bold;">&#41;</span>: <span style="color: #c20cb9; font-weight: bold;">yes</span>
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:
File <span style="color: #ff0000;">&quot;manage.py&quot;</span>, line <span style="color: #000000;">9</span>, <span style="color: #000000; font-weight: bold;">in</span>
execute_from_command_line<span style="color: #7a0874; font-weight: bold;">&#40;</span>sys.argv<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/__init__.py&quot;</span>, line <span style="color: #000000;">443</span>, <span style="color: #000000; font-weight: bold;">in</span> execute_from_command_line
utility.execute<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/__init__.py&quot;</span>, line <span style="color: #000000;">382</span>, <span style="color: #000000; font-weight: bold;">in</span> execute
self.fetch_command<span style="color: #7a0874; font-weight: bold;">&#40;</span>subcommand<span style="color: #7a0874; font-weight: bold;">&#41;</span>.run_from_argv<span style="color: #7a0874; font-weight: bold;">&#40;</span>self.argv<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/base.py&quot;</span>, line <span style="color: #000000;">196</span>, <span style="color: #000000; font-weight: bold;">in</span> run_from_argv
self.execute<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>args, <span style="color: #000000; font-weight: bold;">**</span>options.__dict__<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/base.py&quot;</span>, line <span style="color: #000000;">232</span>, <span style="color: #000000; font-weight: bold;">in</span> execute
output = self.handle<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>args, <span style="color: #000000; font-weight: bold;">**</span>options<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/base.py&quot;</span>, line <span style="color: #000000;">371</span>, <span style="color: #000000; font-weight: bold;">in</span> handle
<span style="color: #7a0874; font-weight: bold;">return</span> self.handle_noargs<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">**</span>options<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/commands/syncdb.py&quot;</span>, line <span style="color: #000000;">110</span>, <span style="color: #000000; font-weight: bold;">in</span> handle_noargs
emit_post_sync_signal<span style="color: #7a0874; font-weight: bold;">&#40;</span>created_models, verbosity, interactive, db<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/sql.py&quot;</span>, line <span style="color: #000000;">189</span>, <span style="color: #000000; font-weight: bold;">in</span> emit_post_sync_signal
<span style="color: #007800;">interactive</span>=interactive, <span style="color: #007800;">db</span>=db<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/dispatch/dispatcher.py&quot;</span>, line <span style="color: #000000;">172</span>, <span style="color: #000000; font-weight: bold;">in</span> send
response = receiver<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">signal</span>=self, <span style="color: #007800;">sender</span>=sender, <span style="color: #000000; font-weight: bold;">**</span>named<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py&quot;</span>, line <span style="color: #000000;">73</span>, <span style="color: #000000; font-weight: bold;">in</span> create_superuser
call_command<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;createsuperuser&quot;</span>, <span style="color: #007800;">interactive</span>=True<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/__init__.py&quot;</span>, line <span style="color: #000000;">150</span>, <span style="color: #000000; font-weight: bold;">in</span> call_command
<span style="color: #7a0874; font-weight: bold;">return</span> klass.execute<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>args, <span style="color: #000000; font-weight: bold;">**</span>defaults<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/core/management/base.py&quot;</span>, line <span style="color: #000000;">232</span>, <span style="color: #000000; font-weight: bold;">in</span> execute
output = self.handle<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">*</span>args, <span style="color: #000000; font-weight: bold;">**</span>options<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py&quot;</span>, line <span style="color: #000000;">63</span>, <span style="color: #000000; font-weight: bold;">in</span> handle
default_username = get_default_username<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py&quot;</span>, line <span style="color: #000000;">105</span>, <span style="color: #000000; font-weight: bold;">in</span> get_default_username
default_username = get_system_username<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py&quot;</span>, line <span style="color: #000000;">85</span>, <span style="color: #000000; font-weight: bold;">in</span> get_system_username
<span style="color: #7a0874; font-weight: bold;">return</span> getpass.getuser<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.decode<span style="color: #7a0874; font-weight: bold;">&#40;</span>locale.getdefaultlocale<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/locale.py&quot;</span>, line <span style="color: #000000;">496</span>, <span style="color: #000000; font-weight: bold;">in</span> getdefaultlocale
<span style="color: #7a0874; font-weight: bold;">return</span> _parse_localename<span style="color: #7a0874; font-weight: bold;">&#40;</span>localename<span style="color: #7a0874; font-weight: bold;">&#41;</span>
File <span style="color: #ff0000;">&quot;/Users/patrick/.virtualenvs/app/lib/python2.7/locale.py&quot;</span>, line <span style="color: #000000;">428</span>, <span style="color: #000000; font-weight: bold;">in</span> _parse_localename
raise ValueError, <span style="color: #ff0000;">'unknown locale: %s'</span> <span style="color: #000000; font-weight: bold;">%</span> localename
ValueError: unknown locale: UTF-<span style="color: #000000;">8</span></pre></div></div>

<p>More precisely the error is</p>
<pre>ValueError: unknown locale: UTF-8</pre>
<p>After doing a little search on Google I found <a href="https://code.djangoproject.com/ticket/5846" target="_blank">this ticket</a> on the Django bug tracker.</p>
<p>The solution was really simple I just needed to export some variables in my bash enviroment. So I edited my .bash_profile file adding these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LANG</span>=<span style="color: #ff0000;">&quot;it_IT.UTF-8&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LC_COLLATE</span>=<span style="color: #ff0000;">&quot;it_IT.UTF-8&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LC_CTYPE</span>=<span style="color: #ff0000;">&quot;it_IT.UTF-8&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LC_MESSAGES</span>=<span style="color: #ff0000;">&quot;it_IT.UTF-8&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LC_MONETARY</span>=<span style="color: #ff0000;">&quot;it_IT.UTF-8&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LC_NUMERIC</span>=<span style="color: #ff0000;">&quot;it_IT.UTF-8&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LC_TIME</span>=<span style="color: #ff0000;">&quot;it_IT.UTF-8&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LC_ALL</span>=</pre></div></div>

<p>where <strong>it_IT.UTF-8</strong> should be your locale string. To test if it worked for you just reload the console and run this code:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'import locale; print(locale.getdefaultlocale());'</span></pre></div></div>

<p>You should get something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#40;</span><span style="color: #483d8b;">'it_IT'</span>, <span style="color: #483d8b;">'UTF8'</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://patrick.arminio.info/blog/2012/02/fix-valueerror-unknown-locale-utf8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Plugin] jQuery Holed Div</title>
		<link>http://patrick.arminio.info/blog/2011/02/plugin-jquery-holed-div/</link>
		<comments>http://patrick.arminio.info/blog/2011/02/plugin-jquery-holed-div/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 18:34:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[WebDevelopment]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[freebie]]></category>
		<category><![CDATA[holed div]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://patrick.arminio.info/blog/?p=39</guid>
		<description><![CDATA[jQuery Holed Div: a plugin that created a rounded border background with an hole.]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to create a holed div, like this:</p>
<p><img class="aligncenter size-full wp-image-40" title="Holed Div Example" src="http://patrick.arminio.info/blog/wp-content/uploads/2011/02/Schermata-2011-02-03-a-19.03.18.png" alt="" width="239" height="256" />I haven&#8217;t found any good solution to create a holed div without using images, so I tried to use canvas as a background and it worked nicely.</p>
<p>Anyway I wanted to share my result to everyone, maybe it&#8217;ll be useful to someone else <img src='http://patrick.arminio.info/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
So I created a jQuery Plugin called jquery.holedDiv.</p>
<p>Here&#8217;s a demo: <a title="demo" href="http://patrick.arminio.info/blog/resources/holed/" target="_blank">demo</a></p>
<p><a title="Latest Version" href="http://patrick.arminio.info/blog/resources/holed/js/jquery.holedDiv.js" target="_blank">Download Latest</a> This version may not work, because is used for testing, download the minified version.</p>
<p><a title="Download Min 0.1" href="http://patrick.arminio.info/blog/resources/holed/js/jquery.holedDiv-0.1.min.js" target="_blank">Download Minified (0.1)</a><br />
<a title="Download Min 0.1.1" href="http://patrick.arminio.info/blog/resources/holed/js/jquery.holedDiv-0.1.1.min.js" target="_blank">Download Minified (0.1.1)</a></p>
<p><strong>Update</strong>: code uploaded to bitbucket (see below)</p>
<p>Usage:</p>
<p><code> </code></p>
<pre><code>$('div.info').holedDiv({
      'padding': 10,
      'radius': 10, //0 if you don't want rounded borders
      'stroke': false, //if you want to add a (black) stroke to the background
      'holeRadius': 20,
      'drawInner': true, //if you want to draw an inner circle, like the demo
      'drawInnerColor': 'black' //the color of the inner circle
});</code></pre>
<p><code> </code></p>
<p>Tested On:</p>
<ul>
<li>Chrome 10</li>
<li>Firefox 4b10</li>
<li>Opera 10</li>
<li>IE 9</li>
</ul>
<p>If you find any bug, or have any question (or feature suggestions) please let me know <img src='http://patrick.arminio.info/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Update 03/02/2011 20.41</strong></p>
<p>Added version 0.1.1 that supports IE 9.0</p>
<p><strong>Update 04/02/2011 23.50</strong></p>
<p>Version 0.2 is ready, supports IE &lt; 9 using excanvas</p>
<p>You can download it on bitbucket:</p>
<p><a href="http://bitbucket.org/patrick91/jquery.holeddiv/">http://bitbucket.org/patrick91/jquery.holeddiv/</a></p>
<p><strong>Update 08/02/2011</strong></p>
<p>Relased Version 0.3 check it on <a href="http://bitbucket.org/patrick91/jquery.holeddiv/">bitbucket</a></p>
]]></content:encoded>
			<wfw:commentRss>http://patrick.arminio.info/blog/2011/02/plugin-jquery-holed-div/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Project] Arduino and Canon EOS 400d</title>
		<link>http://patrick.arminio.info/blog/2010/08/project-arduino-and-canon-eos-400d/</link>
		<comments>http://patrick.arminio.info/blog/2010/08/project-arduino-and-canon-eos-400d/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 14:37:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[canon]]></category>
		<category><![CDATA[electronic]]></category>
		<category><![CDATA[eos 400d]]></category>
		<category><![CDATA[intervalometer]]></category>
		<category><![CDATA[time lapse]]></category>

		<guid isPermaLink="false">http://patrick.arminio.info/blog/?p=37</guid>
		<description><![CDATA[This is my last project, an intervalometer for my Canon EOS 400d created with arduino.]]></description>
			<content:encoded><![CDATA[<p>This is my last project, an intervalometer for my Canon EOS 400d created with arduino.</p>
<p>I created it because i need to create some time lapse video for the coming soon <a href="http://bisaccia.info">bisaccia.info</a>&#8216;s web tv.<br />
Here are two videos:</p>
<p>The &#8220;Backstage&#8221;:<br />
<object width="601" height="338"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=14143766&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=ff9933&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=14143766&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=ff9933&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="601" height="338"></embed></object></p>
<p>A sample time lapse:<br />
<object width="601" height="338"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=14146449&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=ff9933&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=14146449&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=ff9933&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="601" height="338"></embed></object></p>
<p>I&#8217;ll upload the code and the schematic soon when I create a better version, currently you can&#8217;t change the delay between every shoots, also you can&#8217;t stop it.<br />
A better time lapse video is also coming but I should buy a battery grip for my canon, currently my battery last only 5 hours.</p>
]]></content:encoded>
			<wfw:commentRss>http://patrick.arminio.info/blog/2010/08/project-arduino-and-canon-eos-400d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tip &#8211; Dynamic Path For FileField</title>
		<link>http://patrick.arminio.info/blog/2010/06/tip-dynamic-path-for-filefield/</link>
		<comments>http://patrick.arminio.info/blog/2010/06/tip-dynamic-path-for-filefield/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 19:32:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[filefield]]></category>

		<guid isPermaLink="false">http://patrick.arminio.info/blog/?p=34</guid>
		<description><![CDATA[In a recent project I needed to save a file to a dynamic directory. My model was something like this: from django.db import models &#160; class MyDoc&#40;models.Model&#41;: name = models.CharField&#40;&#41; document = models.FileField&#40;upload_to='uploads/documents'&#41; with this code each document will be uploaded to MEDIA_ROOT/uploads/documents. In order to upload the file to a dynamic dir like this: [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I needed to save a file to a dynamic directory.</p>
<p>My model was something like this:</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: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyDoc<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    name = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    document = models.<span style="color: black;">FileField</span><span style="color: black;">&#40;</span>upload_to=<span style="color: #483d8b;">'uploads/documents'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>with this code each document will be uploaded to MEDIA_ROOT/uploads/documents.<br />
In order to upload the file to a dynamic dir like this: MEDIA_ROOT/uploads/documents/id/ I used this code:</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: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyDoc<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    upload_to = <span style="color: #483d8b;">'uploads/documents/%d/%s'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _get_upload_to<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, filename<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">upload_to</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">pk</span>, filename<span style="color: black;">&#41;</span>
&nbsp;
    name = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    document = models.<span style="color: black;">FileField</span><span style="color: black;">&#40;</span>upload_to=_get_upload_to<span style="color: black;">&#41;</span></pre></div></div>

<p>That&#8217;s all (Thats&#8217;s Django!), now this code will upload each document into a separated dir for each object.</p>
<p><strong>UPDATE</strong><br />
The code I posted before doesn&#8217;t work properly in fact the first time we save the model the pk is None so this code can&#8217;t get the correct path.</p>
<p>If you have any question, please feel free to ask.</p>
]]></content:encoded>
			<wfw:commentRss>http://patrick.arminio.info/blog/2010/06/tip-dynamic-path-for-filefield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding additional views to django admin</title>
		<link>http://patrick.arminio.info/blog/2010/05/additional-admin-views/</link>
		<comments>http://patrick.arminio.info/blog/2010/05/additional-admin-views/#comments</comments>
		<pubDate>Mon, 17 May 2010 18:37:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[django-admin]]></category>

		<guid isPermaLink="false">http://patrick.arminio.info/blog/2010/05/additional-admin-views/</guid>
		<description><![CDATA[Today I wanna show you a simple way to add an additional view to the admin site. Imagine that you want to create a view to review a model, something like send an email to the site owner with your thoughs (or your changes) about an entry. First we need to define a simple model [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wanna show you a simple way to add an additional view to the admin site.<br />
Imagine that you want to create a view to review a model, something like send an email to the site owner with your thoughs (or your changes) about an entry.</p>
<p>First we need to define a simple model (of course you can use your existing model):</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: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyEntry<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    title = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
    body = models.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">title</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
        verbose_name = <span style="color: #483d8b;">'My Entry'</span>
        verbose_name_plural = <span style="color: #483d8b;">'My Entries'</span></pre></div></div>

<p><span id="more-5"></span>Once you have defined the model you need to register it to the admin site.</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: black;">contrib</span> <span style="color: #ff7700;font-weight:bold;">import</span> admin
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> my_test.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
admin.<span style="color: #dc143c;">site</span>.<span style="color: black;">register</span><span style="color: black;">&#40;</span>MyEntry<span style="color: black;">&#41;</span></pre></div></div>

<p>Now you can see your model under the django admin site:</p>
<p><img class="aligncenter size-full wp-image-13" title="wpid-Immagine3-2010-05-17-22-37.png" src="http://patrick.arminio.info/blog/wp-content/uploads/2010/05/wpid-Immagine3-2010-05-17-22-374.png" alt="" width="517" height="64" /></p>
<p>Ok it works, let&#8217;s add a simple entry like this:</p>
<p><a href="http://patrick.arminio.info/blog/wp-content/uploads/2010/05/wpid-Immagine4-2010-05-17-22-377.png"><img class="aligncenter size-medium wp-image-26" src="http://patrick.arminio.info/blog/wp-content/uploads/2010/05/wpid-Immagine4-2010-05-17-22-377-300x85.png" alt="" width="300" height="85" /></a><br />
Just to test if it works.</p>
<p>Now it&#8217;s time to create the view and map it to the admin, simply subclassing the default ModelAdmin class:</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: black;">contrib</span> <span style="color: #ff7700;font-weight:bold;">import</span> admin
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">template</span> <span style="color: #ff7700;font-weight:bold;">import</span> RequestContext
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> patterns
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">shortcuts</span> <span style="color: #ff7700;font-weight:bold;">import</span> render_to_response
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> my_test.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyEntryAdmin<span style="color: black;">&#40;</span>admin.<span style="color: black;">ModelAdmin</span><span style="color: black;">&#41;</span>:
    review_template = <span style="color: #483d8b;">'admin/my_test/myentry/review.html'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_urls<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        urls = <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>MyEntryAdmin, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">get_urls</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        my_urls = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
            <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\d</span>+/review/$'</span>, <span style="color: #008000;">self</span>.<span style="color: black;">admin_site</span>.<span style="color: black;">admin_view</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">review</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
        <span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> my_urls + urls
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> review<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request, <span style="color: #008000;">id</span><span style="color: black;">&#41;</span>:
        entry = MyEntry.<span style="color: black;">objects</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>pk=<span style="color: #008000;">id</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> render_to_response<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">review_template</span>, <span style="color: black;">&#123;</span>
            <span style="color: #483d8b;">'title'</span>: <span style="color: #483d8b;">'Review entry: %s'</span> <span style="color: #66cc66;">%</span> entry.<span style="color: black;">title</span>,
            <span style="color: #483d8b;">'entry'</span>: entry,
            <span style="color: #483d8b;">'opts'</span>: <span style="color: #008000;">self</span>.<span style="color: black;">model</span>._meta,
            <span style="color: #483d8b;">'root_path'</span>: <span style="color: #008000;">self</span>.<span style="color: black;">admin_site</span>.<span style="color: black;">root_path</span>,
        <span style="color: black;">&#125;</span>, context_instance=RequestContext<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
admin.<span style="color: #dc143c;">site</span>.<span style="color: black;">register</span><span style="color: black;">&#40;</span>MyEntry, MyEntryAdmin<span style="color: black;">&#41;</span></pre></div></div>

<p>In this example I have added a view ovverriding the get_urls method. Notice that we need to wrap our view with self.admin_site.admin_view in order to <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls">check permission</a>.</p>
<p>One of the last thing to do is create the template for the view. Let&#8217;s do it extending the admin base site template:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">{% extends &quot;admin/base_site.html&quot; %}
{% load i18n admin_modify adminmedia %}
{% block extrahead %}{{ block.super }}
{% url admin:jsi18n as jsi18nurl %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{{ jsi18nurl|default:&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--mce:0--&gt;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
{% endblock %}
{% block extrastyle %}{{ block.super }}
{% endblock %}
{% block bodyclass %}{{ opts.app_label }}-{{ opts.object_name.lower }} change-form{% endblock %}
{% block breadcrumbs %}{% if not is_popup %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;breadcrumbs&quot;</span>&gt;</span>
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../../../&quot;</span>&gt;</span>{% trans &quot;Home&quot; %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span> ›
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../../&quot;</span>&gt;</span>{{ opts.app_label|capfirst|escape }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span> ›
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../&quot;</span>&gt;</span>{{ opts.verbose_name_plural|capfirst }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span> ›
     <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../&quot;</span>&gt;</span>{{ opts.verbose_name|capfirst }} #{{ entry.pk }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span> ›
     {% trans 'Review Entry' %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
{% endif %}{% endblock %}
{% block content %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;.&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">fieldset</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;module aligned&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form-row&quot;</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span>&gt;</span>Entry<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">textarea</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;height: 136px; width: 161px; margin: 2px;&quot;</span> <span style="color: #000066;">rows</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;10&quot;</span>&gt;</span>{{ entry.body }}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">textarea</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">fieldset</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit-row&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;default&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;{% trans 'Review' %}&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
&nbsp;
{% endblock %}</pre></div></div>

<p>Let&#8217;s go to the url of the view, in my case http://localhost:8000/admin/my_test/myentry/1/review/. We should see something like this:</p>
<p><a href="http://patrick.arminio.info/blog/wp-content/uploads/2010/05/wpid-Immagine5-2010-05-17-22-371.png"><img class="aligncenter size-medium wp-image-23" title="wpid-Immagine5-2010-05-17-22-37.png" src="http://patrick.arminio.info/blog/wp-content/uploads/2010/05/wpid-Immagine5-2010-05-17-22-371-300x131.png" alt="" width="300" height="131" /></a><br />
Ok, we got the view working, nice! But we don&#8217;t want to manually insert the url every time. So let&#8217;s add the url to the change model view, in order to get this:</p>
<p><img class="aligncenter size-full wp-image-28" title="wpid-Immagine6-2010-05-17-22-37.png" src="http://patrick.arminio.info/blog/wp-content/uploads/2010/05/wpid-Immagine6-2010-05-17-22-372.png" alt="" width="164" height="46" /></p>
<p>We need to ovveride the default change model template creating a template under template_dir/admin/my_app/my_model/change_form.html (change this based on your configuration)</p>
<p>with this markup:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">{% extends &quot;admin/change_form.html&quot; %}
{% load i18n %}
{% block object-tools %}
{% if change %}{% if not is_popup %}
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;object-tools&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;historylink&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;history/&quot;</span>&gt;</span>{% trans &quot;History&quot; %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;historylink&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;review/&quot;</span>&gt;</span>{% trans &quot;Review&quot; %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
{% if has_absolute_url %}
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;viewsitelink&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;../../../r/{{ content_type_id }}/{{ object_id }}/&quot;</span>&gt;</span>{% trans &quot;View on site&quot; %}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
{% endif%}<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
{% endif %}{% endif %}
{% endblock %}</pre></div></div>

<p>That&#8217;s all! If you have some problem with this code, add a comment or contact me.</p>
]]></content:encoded>
			<wfw:commentRss>http://patrick.arminio.info/blog/2010/05/additional-admin-views/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

