djangosnippets.org: Latest snippets tagged with 'settings'https://djangosnippets.org/tags/settings/2021-11-13T11:34:55.810010-06:00Django Settings Assignment Expressions aka Walrus Operator example
2021-11-13T11:34:55.810010-06:00webologyhttps://djangosnippets.org/snippets/10863/<p>Python 3.8's Assignment Expressions, aka the Walrus operator, feel like an excellent pattern for checking optional environment variables that are only needed to initialize a library like Sentry.</p>
Freely redistributableConditional INSTALLED_APPS entry
2014-05-11T23:54:10.973292-05:00tgandorhttps://djangosnippets.org/snippets/3048/<p>Some INSTALLED_APPLICATIONS applications may not be critical for your website to work - for example you may only need them for development - like 'django_extensions' or 'debug_toolbar'. They needn't be installed actually for the site to work. </p>
<p>This way you can avoid discussions with other developers if some application should …</p>
Freely redistributableDjango Settings Splitter & Local Settings loader
2014-03-02T11:58:01.620032-06:00rududehttps://djangosnippets.org/snippets/3033/<p>Everybody know about long spagetty-style settings file for django :-)</p>
<p>I tried to find any cool settings loader, but have no luck.I created this one myself.</p>
<p>Ok, we forgetting about <code>settings.py</code> and creating module settings (folder named settings with file <code>__init__.py</code>).</p>
<p>This <code>__init__.py</code> file have preloader for modules placed …</p>
Freely redistributableLogging to rotating files
2013-08-14T18:59:39.139983-05:00davidfunghttps://djangosnippets.org/snippets/2980/<p>It took me some time to figure out how to set up logging in Django. What I want to do is to log to a pair of rotating files each 1MB in size.</p>
<p>What I come up with is this code segment in the <code>settings.py</code> file.</p>
Freely redistributableRun a testcase with custom INSTALLED_APPS
2012-11-08T04:40:40.371984-06:00vdboorhttps://djangosnippets.org/snippets/2843/<p>This code allows you to register a model to Django that is only used for unit testing.
It will not exist in the regular Django workflow. After the tests executed, the Django settings are restored.</p>
<p>Usage:</p>
<ol>
<li>Change <code>tests.py</code> into a <code>tests</code> package.</li>
<li>Place a <code>models.py</code> in the <code>tests</code> package.</li>
<li>Use …</li></ol>
Freely redistributableheroku postgress django settings
2012-07-16T18:32:21.326354-05:00grillermohttps://djangosnippets.org/snippets/2790/<p>These are my working settings for a postgress database on heroku, after they removed automatic inyection of the values.</p>
Freely redistributableSettings file pre
2012-04-21T08:53:31.944780-05:00beresovskiyhttps://djangosnippets.org/snippets/2733/<p>Help setting up directories</p>
Freely redistributablemap_path function for settings.py
2011-07-23T19:19:46.633666-05:00btaylordesignhttps://djangosnippets.org/snippets/2494/<p>By using map_path([directory_name]) you can avoid hard coding paths for a specific OS in your settings.py</p>
Freely redistributabletag to store a settings value as template variable
2011-07-06T06:13:46.156662-05:00pflannohttps://djangosnippets.org/snippets/2479/<p>Get any value from settings.py as a template variable. The variable can then be used in conditional tags.
E.g. to show a link to a help page only if it the help page url is defined in settings.py </p>
<pre><code>{% load get_setting %}
{% get_setting MY_HELP_URL as help_url %}
{% if …</code></pre>
Freely redistributableDecorator and context manager to override settings
2011-05-17T10:02:14.118453-05:00jezdezhttps://djangosnippets.org/snippets/2437/<h2>Overriding settings</h2>
<p>For testing purposes it's often useful to change a setting temporarily and revert to the original value after running the testing code. The following code doubles as a context manager and decorator. It's used like this:</p>
<pre><code>from django.test import TestCase
from whatever import override_settings
class LoginTestCase(TestCase):
@override_settings(LOGIN_URL='/other/login/')
def …</code></pre>
Freely redistributableTemplate Tag for Retrieving complex Settings
2011-02-07T03:05:30.388153-06:00joelbitarhttps://djangosnippets.org/snippets/2350/<p>Very much like the snippet joshua wrote (http://djangosnippets.org/snippets/67/) for the same purpose but with the addition that this lets the user get values for more complex settings like dicts and lists
</p>
Freely redistributableDev/staging/prod environment settings seperation
2011-02-06T13:44:07.396734-06:00sleepycalhttps://djangosnippets.org/snippets/2349/<p>Read code for info</p>
Freely redistributableExpose filtered settings to templates request context
2010-12-21T05:48:01.613939-06:00n1k0https://djangosnippets.org/snippets/2296/<p><strong>Warning</strong>: I'm quite sure this is <strong>not</strong> a best practice, but this snippet has proven being very useful to me. Handle with care. I also wonder about the impact on performance, while I didn't notice any slowdown on a very small app of mine.
</p>
<p>Idea is to expose project …</p>
Freely redistributablechange settings locally in an individual test
2010-08-19T04:41:55.030302-05:00akaiholahttps://djangosnippets.org/snippets/2156/<p>So you need to change some settings when running an individual test in a test case. You could just wrap the test between <code>old_value = settings.MY_SETTING</code> and <code>settings.MY_SETTING = old_value</code>. This snippet provides a helper which makes this a bit more convenient, since settings are restored to their old …</p>
Freely redistributableDecorator for enabling views only when developing
2010-08-06T18:45:03.991262-05:00damdhttps://djangosnippets.org/snippets/2138/<p>This is a decorator which essentially replaces the decorated view with a view that always raises <code>Http404</code> (File Not Found) when <code>settings.DEBUG</code> is set to True.
</p>
Freely redistributable