-1

I'm using Django 2 with Python 3.7

I want to import some settings from a file local_settings.py so I can include that file in a .gitignore so I don't share my secret keys on github.

I have the folder tree set up like this: folder tree

settings.py has this at the end:

try:
       from local_settings import *
    except ImportError:
        pass

    ORDERS = '@catsinuniform.myshopify.com/admin/orders.json'

    PRODUCTS = '@catsinuniform.myshopify.com/admin/products.json'

    SHOPIFY_SECRET_KEY = ''

    SHOPIFY_PWORD = ''

    ORDERS_URL = f"https://{SHOPIFY_SECRET_KEY}:{SHOPIFY_PWORD}{ORDERS}"

    PRODUCTS_URL = f"https://{SHOPIFY_SECRET_KEY}:{SHOPIFY_PWORD}{PRODUCTS}"

I would also like to put my SECRET_KEY in local_settings.py

This isn't working and I can't find why not? Is it my Python version or Django?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Davtho1983
  • 3,827
  • 8
  • 54
  • 105
  • What do you mean, "isn't working"? What exactly did you try, and what happens? (Note the point of that local_settings pattern is that the import goes at the very end of the file, otherwise you won't be able to overwrite any of the subsequent settings. But this doesn't affect your SECRET_KEY anyway.) – Daniel Roseman Nov 07 '18 at 13:59

2 Answers2

0

Because your local_settings.py is getting imported first, then overrriden by your settings. IF you want to use this pattern, do your import at the end of the file.

A better way would be to strore your secret keys in an environment variable set on the server which is then imported into your settings file using something like:

SECRET_KEY = os.environ.get('SECRET_KEY', 'some_sort_of_sane_default_for_your_dev')

Sina Khelil
  • 2,001
  • 1
  • 18
  • 27
  • ORDERS_URL and PRODUCTS_URL are still blank so I'm getting a key error when I import the urls into my views? – Davtho1983 Nov 07 '18 at 14:10
0

Another way would be to import it from your __init__.py file

from .settings import *
try:
    from .local_settings import *
except ImportError as exc:
    exc.args = tuple(
        ['%s (local_settings.py missing)' % exc.args[0]])
raise exc
Alex
  • 5,759
  • 1
  • 32
  • 47