<!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="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>django-appconf — django-appconf 1.0.2 documentation</title> <link rel="stylesheet" href="_static/classic.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></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> <script type="text/javascript" src="_static/language_data.js"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="next" title="Usage" href="usage.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="usage.html" title="Usage" accesskey="N">next</a> |</li> <li class="nav-item nav-item-0"><a href="#">django-appconf 1.0.2 documentation</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="django-appconf"> <h1>django-appconf<a class="headerlink" href="#django-appconf" title="Permalink to this headline">¶</a></h1> <a class="reference external image-reference" href="http://codecov.io/github/django-compressor/django-appconf?branch=develop"><img alt="Code Coverage" src="http://codecov.io/github/django-compressor/django-appconf/coverage.svg?branch=develop" /></a> <a class="reference external image-reference" href="http://travis-ci.org/django-compressor/django-appconf"><img alt="Build Status" src="https://secure.travis-ci.org/django-compressor/django-appconf.png?branch=develop" /></a> <p>A helper class for handling configuration defaults of packaged Django apps gracefully.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This app precedes Django’s own <a class="reference external" href="https://docs.djangoproject.com/en/stable/ref/applications/#django.apps.AppConfig">AppConfig</a> classes that act as “objects [to] store metadata for an application” inside Django’s app loading mechanism. In other words, they solve a related but different use case than django-appconf and can’t easily be used as a replacement. The similarity in name is purely coincidental.</p> </div> <div class="section" id="overview"> <h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2> <p>Say you have an app called <code class="docutils literal notranslate"><span class="pre">myapp</span></code> with a few defaults, which you want to refer to in the app’s code without repeating yourself all the time. <code class="docutils literal notranslate"><span class="pre">appconf</span></code> provides a simple class to implement those defaults. Simply add something like the following code somewhere in your app files:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">appconf</span> <span class="k">import</span> <span class="n">AppConf</span> <span class="k">class</span> <span class="nc">MyAppConf</span><span class="p">(</span><span class="n">AppConf</span><span class="p">):</span> <span class="n">SETTING_1</span> <span class="o">=</span> <span class="s2">"one"</span> <span class="n">SETTING_2</span> <span class="o">=</span> <span class="p">(</span> <span class="s2">"two"</span><span class="p">,</span> <span class="p">)</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last"><code class="docutils literal notranslate"><span class="pre">AppConf</span></code> classes depend on being imported during startup of the Django process. Even though there are multiple modules loaded automatically, only the <code class="docutils literal notranslate"><span class="pre">models</span></code> modules (usually the <code class="docutils literal notranslate"><span class="pre">models.py</span></code> file of your app) are guaranteed to be loaded at startup. Therefore it’s recommended to put your <code class="docutils literal notranslate"><span class="pre">AppConf</span></code> subclass(es) there, too.</p> </div> <p>The settings are initialized with the capitalized app label of where the setting is located at. E.g. if your <code class="docutils literal notranslate"><span class="pre">models.py</span></code> with the <code class="docutils literal notranslate"><span class="pre">AppConf</span></code> class is in the <code class="docutils literal notranslate"><span class="pre">myapp</span></code> package, the prefix of the settings will be <code class="docutils literal notranslate"><span class="pre">MYAPP</span></code>.</p> <p>You can override the default prefix by specifying a <code class="docutils literal notranslate"><span class="pre">prefix</span></code> attribute of an inner <code class="docutils literal notranslate"><span class="pre">Meta</span></code> class:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">appconf</span> <span class="k">import</span> <span class="n">AppConf</span> <span class="k">class</span> <span class="nc">AcmeAppConf</span><span class="p">(</span><span class="n">AppConf</span><span class="p">):</span> <span class="n">SETTING_1</span> <span class="o">=</span> <span class="s2">"one"</span> <span class="n">SETTING_2</span> <span class="o">=</span> <span class="p">(</span> <span class="s2">"two"</span><span class="p">,</span> <span class="p">)</span> <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span> <span class="n">prefix</span> <span class="o">=</span> <span class="s1">'acme'</span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">MyAppConf</span></code> class will automatically look at Django’s global settings to determine if you’ve overridden it. For example, adding this to your site’s <code class="docutils literal notranslate"><span class="pre">settings.py</span></code> would override <code class="docutils literal notranslate"><span class="pre">SETTING_1</span></code> of the above <code class="docutils literal notranslate"><span class="pre">MyAppConf</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ACME_SETTING_1</span> <span class="o">=</span> <span class="s2">"uno"</span> </pre></div> </div> <p>In case you want to use a different settings object instead of the default <code class="docutils literal notranslate"><span class="pre">'django.conf.settings'</span></code>, set the <code class="docutils literal notranslate"><span class="pre">holder</span></code> attribute of the inner <code class="docutils literal notranslate"><span class="pre">Meta</span></code> class to a dotted import path:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">appconf</span> <span class="k">import</span> <span class="n">AppConf</span> <span class="k">class</span> <span class="nc">MyAppConf</span><span class="p">(</span><span class="n">AppConf</span><span class="p">):</span> <span class="n">SETTING_1</span> <span class="o">=</span> <span class="s2">"one"</span> <span class="n">SETTING_2</span> <span class="o">=</span> <span class="p">(</span> <span class="s2">"two"</span><span class="p">,</span> <span class="p">)</span> <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span> <span class="n">prefix</span> <span class="o">=</span> <span class="s1">'acme'</span> <span class="n">holder</span> <span class="o">=</span> <span class="s1">'acme.conf.settings'</span> </pre></div> </div> <p>If you ship an <code class="docutils literal notranslate"><span class="pre">AppConf</span></code> class with your reusable Django app, it’s recommended to put it in a <code class="docutils literal notranslate"><span class="pre">conf.py</span></code> file of your app package and import <code class="docutils literal notranslate"><span class="pre">django.conf.settings</span></code> in it, too:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span> <span class="kn">from</span> <span class="nn">appconf</span> <span class="k">import</span> <span class="n">AppConf</span> <span class="k">class</span> <span class="nc">MyAppConf</span><span class="p">(</span><span class="n">AppConf</span><span class="p">):</span> <span class="n">SETTING_1</span> <span class="o">=</span> <span class="s2">"one"</span> <span class="n">SETTING_2</span> <span class="o">=</span> <span class="p">(</span> <span class="s2">"two"</span><span class="p">,</span> <span class="p">)</span> </pre></div> </div> <p>In the other files of your app you can easily make sure the settings are correctly loaded if you import Django’s settings object from that module, e.g. in your app’s <code class="docutils literal notranslate"><span class="pre">views.py</span></code>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span> <span class="kn">from</span> <span class="nn">myapp.conf</span> <span class="k">import</span> <span class="n">settings</span> <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span> <span class="n">text</span> <span class="o">=</span> <span class="s1">'Setting 1 is: </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="n">settings</span><span class="o">.</span><span class="n">MYAPP_SETTING_1</span> <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">text</span><span class="p">)</span> </pre></div> </div> </div> </div> <div class="section" id="installation"> <h1>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h1> <p>Install django-appconf with your favorite Python package manager, e.g.:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">django</span><span class="o">-</span><span class="n">appconf</span> </pre></div> </div> </div> <div class="section" id="contents"> <h1>Contents<a class="headerlink" href="#contents" title="Permalink to this headline">¶</a></h1> <div class="toctree-wrapper compound"> <ul> <li class="toctree-l1"><a class="reference internal" href="usage.html">Usage</a></li> <li class="toctree-l1"><a class="reference internal" href="reference.html">Reference</a></li> <li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li> </ul> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="#">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#">django-appconf</a><ul> <li><a class="reference internal" href="#overview">Overview</a></li> </ul> </li> <li><a class="reference internal" href="#installation">Installation</a></li> <li><a class="reference internal" href="#contents">Contents</a></li> </ul> <h4>Next topic</h4> <p class="topless"><a href="usage.html" title="next chapter">Usage</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/index.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <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> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="usage.html" title="Usage" >next</a> |</li> <li class="nav-item nav-item-0"><a href="#">django-appconf 1.0.2 documentation</a> »</li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2011-2013, Jannis Leidel and individual contributors. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.3. </div> </body> </html>