<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Managing files — Django v1.2 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="top" title="Django v1.2 documentation" href="../index.html" /> <link rel="up" title="Using Django" href="index.html" /> <link rel="next" title="Testing Django applications" href="testing.html" /> <link rel="prev" title="Generic views" href="generic-views.html" /> <script type="text/javascript" src="../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../index.html">Django v1.2 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../index.html">Home</a> | <a title="Table of contents" href="../contents.html">Table of contents</a> | <a title="Global index" href="../genindex.html">Index</a> | <a title="Module index" href="../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="generic-views.html" title="Generic views">previous</a> | <a href="index.html" title="Using Django" accesskey="U">up</a> | <a href="testing.html" title="Testing Django applications">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="topics-files"> <div class="section" id="s-managing-files"> <span id="managing-files"></span><h1>Managing files<a class="headerlink" href="#managing-files" title="Permalink to this headline">¶</a></h1> <div class="versionadded"> <span class="title">New in Django 1.0:</span> <a class="reference internal" href="../releases/1.0.html"><em>Please, see the release notes</em></a></div> <p>This document describes Django’s file access APIs.</p> <p>By default, Django stores files locally, using the <a class="reference internal" href="../ref/settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a> and <a class="reference internal" href="../ref/settings.html#std:setting-MEDIA_URL"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_URL</span></tt></a> settings. The examples below assume that you’re using these defaults.</p> <p>However, Django provides ways to write custom <a class="reference internal" href="#file-storage">file storage systems</a> that allow you to completely customize where and how Django stores files. The second half of this document describes how these storage systems work.</p> <div class="section" id="s-using-files-in-models"> <span id="using-files-in-models"></span><h2>Using files in models<a class="headerlink" href="#using-files-in-models" title="Permalink to this headline">¶</a></h2> <p>When you use a <a class="reference internal" href="../ref/models/fields.html#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> or <a class="reference internal" href="../ref/models/fields.html#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a>, Django provides a set of APIs you can use to deal with that file.</p> <p>Consider the following model, using an <tt class="docutils literal"><span class="pre">ImageField</span></tt> to store a photo:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">)</span> <span class="n">price</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DecimalField</span><span class="p">(</span><span class="n">max_digits</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">decimal_places</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="n">photo</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ImageField</span><span class="p">(</span><span class="n">upload_to</span><span class="o">=</span><span class="s">'cars'</span><span class="p">)</span> </pre></div> </div> <p>Any <tt class="docutils literal"><span class="pre">Car</span></tt> instance will have a <tt class="docutils literal"><span class="pre">photo</span></tt> attribute that you can use to get at the details of the attached photo:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">car</span> <span class="o">=</span> <span class="n">Car</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"57 Chevy"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">car</span><span class="o">.</span><span class="n">photo</span> <span class="go"><ImageFieldFile: chevy.jpg></span> <span class="gp">>>> </span><span class="n">car</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">name</span> <span class="go">u'cars/chevy.jpg'</span> <span class="gp">>>> </span><span class="n">car</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path</span> <span class="go">u'/media/cars/chevy.jpg'</span> <span class="gp">>>> </span><span class="n">car</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">url</span> <span class="go">u'http://media.example.com/cars/chevy.jpg'</span> </pre></div> </div> <p>This object -- <tt class="docutils literal"><span class="pre">car.photo</span></tt> in the example -- is a <tt class="docutils literal"><span class="pre">File</span></tt> object, which means it has all the methods and attributes described below.</p> </div> <div class="section" id="s-the-file-object"> <span id="the-file-object"></span><h2>The <tt class="docutils literal"><span class="pre">File</span></tt> object<a class="headerlink" href="#the-file-object" title="Permalink to this headline">¶</a></h2> <p>Internally, Django uses a <tt class="docutils literal"><span class="pre">django.core.files.File</span></tt> any time it needs to represent a file. This object is a thin wrapper around Python's <a class="reference external" href="http://docs.python.org/library/stdtypes.html#bltin-file-objects">built-in file object</a> with some Django-specific additions.</p> <p>Most of the time you'll simply use a <tt class="docutils literal"><span class="pre">File</span></tt> that Django's given you (i.e. a file attached to a model as above, or perhaps an uploaded file).</p> <p>If you need to construct a <tt class="docutils literal"><span class="pre">File</span></tt> yourself, the easiest way is to create one using a Python built-in <tt class="docutils literal"><span class="pre">file</span></tt> object:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.core.files</span> <span class="kn">import</span> <span class="n">File</span> <span class="go"># Create a Python file object using open()</span> <span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">'/tmp/hello.world'</span><span class="p">,</span> <span class="s">'w'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">myfile</span> <span class="o">=</span> <span class="n">File</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> </pre></div> </div> <p>Now you can use any of the <tt class="docutils literal"><span class="pre">File</span></tt> attributes and methods documented in <a class="reference internal" href="../ref/files/file.html"><em>The File object</em></a>.</p> </div> <div class="section" id="s-file-storage"> <span id="file-storage"></span><h2>File storage<a class="headerlink" href="#file-storage" title="Permalink to this headline">¶</a></h2> <p>Behind the scenes, Django delegates decisions about how and where to store files to a file storage system. This is the object that actually understands things like file systems, opening and reading files, etc.</p> <p>Django's default file storage is given by the <a class="reference internal" href="../ref/settings.html#std:setting-DEFAULT_FILE_STORAGE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_FILE_STORAGE</span></tt></a> setting; if you don't explicitly provide a storage system, this is the one that will be used.</p> <p>See below for details of the built-in default file storage system, and see <a class="reference internal" href="../howto/custom-file-storage.html"><em>Writing a custom storage system</em></a> for information on writing your own file storage system.</p> <div class="section" id="s-storage-objects"> <span id="storage-objects"></span><h3>Storage objects<a class="headerlink" href="#storage-objects" title="Permalink to this headline">¶</a></h3> <p>Though most of the time you'll want to use a <tt class="docutils literal"><span class="pre">File</span></tt> object (which delegates to the proper storage for that file), you can use file storage systems directly. You can create an instance of some custom file storage class, or -- often more useful -- you can use the global default storage system:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.core.files.storage</span> <span class="kn">import</span> <span class="n">default_storage</span> <span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.core.files.base</span> <span class="kn">import</span> <span class="n">ContentFile</span> <span class="gp">>>> </span><span class="n">path</span> <span class="o">=</span> <span class="n">default_storage</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s">'/path/to/file'</span><span class="p">,</span> <span class="n">ContentFile</span><span class="p">(</span><span class="s">'new content'</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">path</span> <span class="go">u'/path/to/file'</span> <span class="gp">>>> </span><span class="n">default_storage</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="go">11</span> <span class="gp">>>> </span><span class="n">default_storage</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="go">'new content'</span> <span class="gp">>>> </span><span class="n">default_storage</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">default_storage</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="go">False</span> </pre></div> </div> <p>See <a class="reference internal" href="../ref/files/storage.html"><em>File storage API</em></a> for the file storage API.</p> </div> <div class="section" id="s-the-built-in-filesystem-storage-class"> <span id="the-built-in-filesystem-storage-class"></span><h3>The built-in filesystem storage class<a class="headerlink" href="#the-built-in-filesystem-storage-class" title="Permalink to this headline">¶</a></h3> <p>Django ships with a built-in <tt class="docutils literal"><span class="pre">FileSystemStorage</span></tt> class (defined in <tt class="docutils literal"><span class="pre">django.core.files.storage</span></tt>) which implements basic local filesystem file storage. Its initializer takes two arguments:</p> <table class="docutils"> <colgroup> <col width="30%" /> <col width="70%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Argument</th> <th class="head">Description</th> </tr> </thead> <tbody valign="top"> <tr><td><tt class="docutils literal"><span class="pre">location</span></tt></td> <td>Optional. Absolute path to the directory that will hold the files. If omitted, it will be set to the value of your <tt class="docutils literal"><span class="pre">MEDIA_ROOT</span></tt> setting.</td> </tr> <tr><td><tt class="docutils literal"><span class="pre">base_url</span></tt></td> <td>Optional. URL that serves the files stored at this location. If omitted, it will default to the value of your <tt class="docutils literal"><span class="pre">MEDIA_URL</span></tt> setting.</td> </tr> </tbody> </table> <p>For example, the following code will store uploaded files under <tt class="docutils literal"><span class="pre">/media/photos</span></tt> regardless of what your <tt class="docutils literal"><span class="pre">MEDIA_ROOT</span></tt> setting is:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span> <span class="kn">from</span> <span class="nn">django.core.files.storage</span> <span class="kn">import</span> <span class="n">FileSystemStorage</span> <span class="n">fs</span> <span class="o">=</span> <span class="n">FileSystemStorage</span><span class="p">(</span><span class="n">location</span><span class="o">=</span><span class="s">'/media/photos'</span><span class="p">)</span> <span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="o">...</span> <span class="n">photo</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ImageField</span><span class="p">(</span><span class="n">storage</span><span class="o">=</span><span class="n">fs</span><span class="p">)</span> </pre></div> </div> <p><a class="reference internal" href="../howto/custom-file-storage.html"><em>Custom storage systems</em></a> work the same way: you can pass them in as the <tt class="docutils literal"><span class="pre">storage</span></tt> argument to a <tt class="docutils literal"><span class="pre">FileField</span></tt>.</p> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Managing files</a><ul> <li><a class="reference internal" href="#using-files-in-models">Using files in models</a></li> <li><a class="reference internal" href="#the-file-object">The <tt class="docutils literal"><span class="pre">File</span></tt> object</a></li> <li><a class="reference internal" href="#file-storage">File storage</a><ul> <li><a class="reference internal" href="#storage-objects">Storage objects</a></li> <li><a class="reference internal" href="#the-built-in-filesystem-storage-class">The built-in filesystem storage class</a></li> </ul> </li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="generic-views.html">Generic views</a></li> <li>Next: <a href="testing.html">Testing Django applications</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../index.html">Django v1.2 documentation</a> <ul><li><a href="index.html">Using Django</a> <ul><li>Managing files</li></ul> </li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/topics/files.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Oct 20, 2010</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="generic-views.html" title="Generic views">previous</a> | <a href="index.html" title="Using Django" accesskey="U">up</a> | <a href="testing.html" title="Testing Django applications">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>