Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Abstract 

3-------- 

4 

5There are lots of ways of dealing with configuration data that should not be hard-coded (and potentially) revealed in 

6the ``settings.py`` file. Using environment variables, though recommended by lots of people, is not always possible; 

7they do not work in Apache and may not work with Nginx. 

8 

9Two notable solutions are `django-configurations`_ and `django-environ`_, and developers are encouraged to check these 

10out. SuperDjango offers the ``env`` app as a relatively straight-forward means of dealing with configuration data. 

11 

12.. _django-configurations: https://github.com/jazzband/django-configurations 

13.. _django-environ: https://github.com/joke2k/django-environ 

14 

15Install 

16------- 

17 

18No installation steps are required to use this package, but see Usage below. 

19 

20Usage 

21----- 

22 

23Create an ``env.ini`` file in your source directory. This is typically the same directory as the ``manage.py`` file. 

24 

25.. important:: 

26 Be sure to exclude this file from your repo. 

27 

28Here is an example: 

29 

30.. code-block:: ini 

31 

32 [environment] 

33 name = live 

34 debug = True 

35 

36 [branding] 

37 copyright = ACME, Inc. 

38 logo = images/logo.png 

39 site_title = ACME Technologies 

40 tagline = Rocket skates, explosive birdseed, and more since 1955. 

41 

42 [secret] 

43 key = secret_key 

44 

45 [database] 

46 host = localhost 

47 name = example_com 

48 password = None 

49 user = postgres 

50 

51 [log] 

52 level = DEBUG 

53 path = /path/to/work/example_com/logs 

54 

55Each section becomes available as an instance variable on the Env instance, meaning (for example) that the database host 

56may be referred to as ``env.database.host``. 

57 

58.. tip:: 

59 Any variable added to the ``environment`` section is available *without* the use of the environment section name. 

60 For example, ``env.debug`` instead of ``env.environment.debug``. See example below. 

61 

62In your ``settings.py`` file, import :py:class:`Env` and load the INI file: 

63 

64.. code-block:: python 

65 

66 # settings.py 

67 from superdjang.env import Env 

68 

69 # assuming the file is located at the same level as manage.py ... 

70 env = Env("env.ini") 

71 

72 DEBUG = env.debug 

73 SECRET_KEY = env.secret.key 

74 

75 DATABASES = { 

76 'default': { 

77 'ENGINE': 'django.db.backends.postgresql', 

78 'HOST': env.database.host, 

79 'NAME': env.database.name, 

80 'PASSWORD': env.database.password, 

81 'USER': env.database.user, 

82 } 

83 } 

84 

85 BRANDING = env.branding 

86 

87 # ... and so on ... 

88 

89""" 

90__author__ = "Shawn Davis <shawn@superdjango.com>" 

91__maintainer__ = "Shawn Davis <shawn@superdjango.com>" 

92__version__ = "0.4.0-x" 

93 

94from .library import * 

95 

96__all__ = ( 

97 "Env", 

98)