Local Settings in Django 2

1487 views django
7

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?

answered question

What do you mean, "isn't working"? What exactly did you try, and what happens? (Note the point of that local_setting structure 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.)

1 Answer

11

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.

posted this

Have an answer?

JD

Please login first before posting an answer.