<!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>FAQ: Using Django — Django 1.5.8 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.5.8', 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 1.5.8 documentation" href="../index.html" /> <link rel="up" title="Django FAQ" href="index.html" /> <link rel="next" title="FAQ: Getting Help" href="help.html" /> <link rel="prev" title="FAQ: Installation" href="install.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 1.5.8 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="install.html" title="FAQ: Installation">previous</a> | <a href="index.html" title="Django FAQ" accesskey="U">up</a> | <a href="help.html" title="FAQ: Getting Help">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="faq-usage"> <div class="section" id="s-faq-using-django"> <span id="faq-using-django"></span><h1>FAQ: Using Django<a class="headerlink" href="#faq-using-django" title="Permalink to this headline">¶</a></h1> <div class="section" id="s-why-do-i-get-an-error-about-importing-django-settings-module"> <span id="why-do-i-get-an-error-about-importing-django-settings-module"></span><h2>Why do I get an error about importing DJANGO_SETTINGS_MODULE?<a class="headerlink" href="#why-do-i-get-an-error-about-importing-django-settings-module" title="Permalink to this headline">¶</a></h2> <p>Make sure that:</p> <ul class="simple"> <li>The environment variable DJANGO_SETTINGS_MODULE is set to a fully-qualified Python module (i.e. “mysite.settings”).</li> <li>Said module is on <tt class="docutils literal"><span class="pre">sys.path</span></tt> (<tt class="docutils literal"><span class="pre">import</span> <span class="pre">mysite.settings</span></tt> should work).</li> <li>The module doesn’t contain syntax errors (of course).</li> </ul> </div> <div class="section" id="s-i-can-t-stand-your-template-language-do-i-have-to-use-it"> <span id="i-can-t-stand-your-template-language-do-i-have-to-use-it"></span><h2>I can’t stand your template language. Do I have to use it?<a class="headerlink" href="#i-can-t-stand-your-template-language-do-i-have-to-use-it" title="Permalink to this headline">¶</a></h2> <p>We happen to think our template engine is the best thing since chunky bacon, but we recognize that choosing a template language runs close to religion. There’s nothing about Django that requires using the template language, so if you’re attached to ZPT, Cheetah, or whatever, feel free to use those.</p> </div> <div class="section" id="s-do-i-have-to-use-your-model-database-layer"> <span id="do-i-have-to-use-your-model-database-layer"></span><h2>Do I have to use your model/database layer?<a class="headerlink" href="#do-i-have-to-use-your-model-database-layer" title="Permalink to this headline">¶</a></h2> <p>Nope. Just like the template system, the model/database layer is decoupled from the rest of the framework.</p> <p>The one exception is: If you use a different database library, you won’t get to use Django’s automatically-generated admin site. That app is coupled to the Django database layer.</p> </div> <div class="section" id="s-how-do-i-use-image-and-file-fields"> <span id="how-do-i-use-image-and-file-fields"></span><h2>How do I use image and file fields?<a class="headerlink" href="#how-do-i-use-image-and-file-fields" title="Permalink to this headline">¶</a></h2> <p>Using 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 an <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> in a model takes a few steps:</p> <ol class="arabic simple"> <li>In your settings file, you’ll need to define <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> as the full path to a directory where you’d like Django to store uploaded files. (For performance, these files are not stored in the database.) Define <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> as the base public URL of that directory. Make sure that this directory is writable by the Web server’s user account.</li> <li>Add the <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> to your model, making sure to define the <a class="reference internal" href="../ref/models/fields.html#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><tt class="xref py py-attr docutils literal"><span class="pre">upload_to</span></tt></a> option to tell Django to which subdirectory of <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> it should upload files.</li> <li>All that will be stored in your database is a path to the file (relative to <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>). You’ll most likely want to use the convenience <a class="reference internal" href="../ref/models/fields.html#django.db.models.fields.files.FieldFile.url" title="django.db.models.fields.files.FieldFile.url"><tt class="xref py py-attr docutils literal"><span class="pre">url</span></tt></a> attribute provided by Django. For example, if your <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> is called <tt class="docutils literal"><span class="pre">mug_shot</span></tt>, you can get the absolute path to your image in a template with <tt class="docutils literal"><span class="pre">{{</span> <span class="pre">object.mug_shot.url</span> <span class="pre">}}</span></tt>.</li> </ol> </div> <div class="section" id="s-how-do-i-make-a-variable-available-to-all-my-templates"> <span id="how-do-i-make-a-variable-available-to-all-my-templates"></span><h2>How do I make a variable available to all my templates?<a class="headerlink" href="#how-do-i-make-a-variable-available-to-all-my-templates" title="Permalink to this headline">¶</a></h2> <p>Sometimes your templates just all need the same thing. A common example would be dynamically-generated menus. At first glance, it seems logical to simply add a common dictionary to the template context.</p> <p>The correct solution is to use a <tt class="docutils literal"><span class="pre">RequestContext</span></tt>. Details on how to do this are here: <a class="reference internal" href="../ref/templates/api.html#subclassing-context-requestcontext"><em>Subclassing Context: RequestContext</em></a>.</p> </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="#">FAQ: Using Django</a><ul> <li><a class="reference internal" href="#why-do-i-get-an-error-about-importing-django-settings-module">Why do I get an error about importing DJANGO_SETTINGS_MODULE?</a></li> <li><a class="reference internal" href="#i-can-t-stand-your-template-language-do-i-have-to-use-it">I can’t stand your template language. Do I have to use it?</a></li> <li><a class="reference internal" href="#do-i-have-to-use-your-model-database-layer">Do I have to use your model/database layer?</a></li> <li><a class="reference internal" href="#how-do-i-use-image-and-file-fields">How do I use image and file fields?</a></li> <li><a class="reference internal" href="#how-do-i-make-a-variable-available-to-all-my-templates">How do I make a variable available to all my templates?</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="install.html">FAQ: Installation</a></li> <li>Next: <a href="help.html">FAQ: Getting Help</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../index.html">Django 1.5.8 documentation</a> <ul><li><a href="index.html">Django FAQ</a> <ul><li>FAQ: Using Django</li></ul> </li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/faq/usage.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" /> <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">May 15, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="install.html" title="FAQ: Installation">previous</a> | <a href="index.html" title="Django FAQ" accesskey="U">up</a> | <a href="help.html" title="FAQ: Getting Help">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>