<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Dynamic upload paths in Django</title>
	<atom:link href="http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/feed/" rel="self" type="application/rss+xml" />
	<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/</link>
	<description>Code and comments on web development, Django, Python and things (un)related.</description>
	<lastBuildDate>Tue, 13 Jul 2010 16:31:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jesse</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-756</link>
		<dc:creator>Jesse</dc:creator>
		<pubDate>Sat, 02 Jan 2010 17:24:58 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-756</guid>
		<description>Thanks very much for this tutorial ... it was a lifesaver for me. A small note to add to the comments:

- The upload_to callable technique also works just fine if you&#039;re uploading an image as part of larger form. You can save a form as normal in your view code, and the request.FILES data will be passed along to your model w/o problems, like this:

&lt;pre&gt;
    form = UploadForm(request.POST, request.FILES)
    if form.is_valid():
        form.save() # all FILES data get passed along, get_image_path callable works fine
        return HttpResponseRedirect(&#039;/some/url/&#039;)&lt;/pre&gt;

Initially I got stuck on this point, because I was trying to save the form with form.save() AND save the image separately with the view code in your tutorial. 

Again thanks. There are a lot of outdated file/image upload tutorials on the web that can be quite confusing for django newcomers. This is great.</description>
		<content:encoded><![CDATA[<p>Thanks very much for this tutorial &#8230; it was a lifesaver for me. A small note to add to the comments:</p>
<p>- The upload_to callable technique also works just fine if you&#8217;re uploading an image as part of larger form. You can save a form as normal in your view code, and the request.FILES data will be passed along to your model w/o problems, like this:</p>
<pre>
    form = UploadForm(request.POST, request.FILES)
    if form.is_valid():
        form.save() # all FILES data get passed along, get_image_path callable works fine
        return HttpResponseRedirect('/some/url/')</pre>
<p>Initially I got stuck on this point, because I was trying to save the form with form.save() AND save the image separately with the view code in your tutorial. </p>
<p>Again thanks. There are a lot of outdated file/image upload tutorials on the web that can be quite confusing for django newcomers. This is great.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-740</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sat, 21 Nov 2009 22:13:08 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-740</guid>
		<description>Thanks for the great example.</description>
		<content:encoded><![CDATA[<p>Thanks for the great example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-711</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Thu, 03 Sep 2009 09:56:36 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-711</guid>
		<description>Thanks, Fidel.  I&#039;ve updated the post to use &lt;code&gt;os.path.join&lt;/code&gt; in the example.</description>
		<content:encoded><![CDATA[<p>Thanks, Fidel.  I&#8217;ve updated the post to use <code>os.path.join</code> in the example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fidel Ramos</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-710</link>
		<dc:creator>Fidel Ramos</dc:creator>
		<pubDate>Thu, 03 Sep 2009 09:20:02 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-710</guid>
		<description>Nice example, simple and to the point. However remember to use os.path.join instead of manually writing dir paths, or it wouldn&#039;t work in (at least) Windows.

&lt;pre&gt;import os
 
def get_image_path(instance, filename):
    return os.path.join(&#039;photos&#039;, instance.id, filename)&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Nice example, simple and to the point. However remember to use os.path.join instead of manually writing dir paths, or it wouldn&#8217;t work in (at least) Windows.</p>
<pre>import os

def get_image_path(instance, filename):
    return os.path.join('photos', instance.id, filename)</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-706</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Tue, 04 Aug 2009 11:22:16 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-706</guid>
		<description>@Incinnerator.

The method gets called by Django, so you can&#039;t add parameters to it.  If you can work out the &quot;number&quot; from the instance, that&#039;s best.  Otherwise, I think you&#039;ll need to get that number from the submitted form or from the view and set it separately on your model (&lt;code&gt;Photo&lt;/code&gt; in my example).</description>
		<content:encoded><![CDATA[<p>@Incinnerator.</p>
<p>The method gets called by Django, so you can&#8217;t add parameters to it.  If you can work out the &#8220;number&#8221; from the instance, that&#8217;s best.  Otherwise, I think you&#8217;ll need to get that number from the submitted form or from the view and set it separately on your model (<code>Photo</code> in my example).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Incinnerator</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-705</link>
		<dc:creator>Incinnerator</dc:creator>
		<pubDate>Mon, 03 Aug 2009 21:02:39 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-705</guid>
		<description>Really helpful, great!

I have a question, is it possible to give the method a third argument?
something like below?

def get_image_path(instance, filename, number):
    return &#039;photos/%s/%s%s&#039; % (instance.id, filename, number)

and how would you use it in the model?

thx</description>
		<content:encoded><![CDATA[<p>Really helpful, great!</p>
<p>I have a question, is it possible to give the method a third argument?<br />
something like below?</p>
<p>def get_image_path(instance, filename, number):<br />
    return &#8216;photos/%s/%s%s&#8217; % (instance.id, filename, number)</p>
<p>and how would you use it in the model?</p>
<p>thx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Moorthy</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-675</link>
		<dc:creator>Moorthy</dc:creator>
		<pubDate>Mon, 25 May 2009 10:18:06 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-675</guid>
		<description>Yes, very helpful. Thanks a bunch!</description>
		<content:encoded><![CDATA[<p>Yes, very helpful. Thanks a bunch!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-669</link>
		<dc:creator>Tom</dc:creator>
		<pubDate>Wed, 20 May 2009 04:30:06 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-669</guid>
		<description>Needed this functionality, found your post, implemented it in 10 minutes. Super helpful, thanks!</description>
		<content:encoded><![CDATA[<p>Needed this functionality, found your post, implemented it in 10 minutes. Super helpful, thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bcline</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-626</link>
		<dc:creator>bcline</dc:creator>
		<pubDate>Thu, 15 Jan 2009 08:25:17 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-626</guid>
		<description>For completeness, the above code uses...

from django.template.defaultfilters import slugify</description>
		<content:encoded><![CDATA[<p>For completeness, the above code uses&#8230;</p>
<p>from django.template.defaultfilters import slugify</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bcline</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-625</link>
		<dc:creator>bcline</dc:creator>
		<pubDate>Wed, 14 Jan 2009 18:51:26 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-625</guid>
		<description>For multiple models that modify the upload_to path you can use a single function to create the callable to pass to the upload_to parameter.

&lt;pre&gt;def upload_to(path, attribute):
    
    def upload_callback(instance, filename):
        return &#039;%s%s/%s&#039; % (path, unicode(slugify(getattr(instance, attribute))), filename)
    
    return upload_callback

...
#Model definitions
class Photo(models.Model):
    name = models.CharField()
    image = models.ImageField(upload_to = upload_to(&#039;uploads/&#039;, &#039;name&#039;))

class Screenshot(models.Model):
    title = models.CharField()
    image = models.ImageField(upload_to = upload_to(&#039;uploads/&#039;, &#039;title&#039;))&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>For multiple models that modify the upload_to path you can use a single function to create the callable to pass to the upload_to parameter.</p>
<pre>def upload_to(path, attribute):

    def upload_callback(instance, filename):
        return '%s%s/%s' % (path, unicode(slugify(getattr(instance, attribute))), filename)

    return upload_callback

...
#Model definitions
class Photo(models.Model):
    name = models.CharField()
    image = models.ImageField(upload_to = upload_to('uploads/', 'name'))

class Screenshot(models.Model):
    title = models.CharField()
    image = models.ImageField(upload_to = upload_to('uploads/', 'title'))</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.235 seconds -->
