Discussion:
Development and Production environment
Alexander Lamas
2018-08-24 00:55:46 UTC
Permalink
Hi guys,

In my Django REST Web API Framework, I would like to set up files for
settings for different purposes.

For example:

settings_release.py
settings_debug.py
settings_unittest.py

Then, when I deploy to production it uses the settings_release.py, when I'm
working on it I can set to settings_debug.py an when I'm running unit tests
it looks the settings_unittests.py.

Is it possible to set this somewhere? Perhaps the app or solution
configuration?

Also, is it possible to change to release mode so that when I publish to
production it uses the settings_release.py automatically as default setting?

Thank you very much!

Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marc Chakiachvili
2018-08-24 08:22:03 UTC
Permalink
You may use environment variable instead of having multiple configuration
settings.

You can also use some package dedicated to that such as django-environ in
your settings.py Then add a sample file config.env.sample to your
repository, add config.env ins .gitignored, and setup your env this way
This solution give you chance not to have any 'secret' info on your
repository (assuming you are going to publish it somewhere)

Cheers,
Marc
Post by Alexander Lamas
Hi guys,
In my Django REST Web API Framework, I would like to set up files for
settings for different purposes.
settings_release.py
settings_debug.py
settings_unittest.py
Then, when I deploy to production it uses the settings_release.py, when
I'm working on it I can set to settings_debug.py an when I'm running unit
tests it looks the settings_unittests.py.
Is it possible to set this somewhere? Perhaps the app or solution
configuration?
Also, is it possible to change to release mode so that when I publish to
production it uses the settings_release.py automatically as default setting?
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alex Daigle
2018-08-24 15:35:19 UTC
Permalink
Another solution is to have a settings folder, with (inside) a base.py, local.py, debug.py... the base settings that are the same for all your settings. This allows you to be more specific with things like installed apps that you may want in your local development and not in a production environment and just avoid redundancy. In the top of your debug.py, you specify that you import the everything from base, and you append as you need it. You do need to change a few other things around to make it work.

Best of both worlds. I personally also add environment variables for things like the secret key, and database, celery,... other things like logging configurations are in different files.

Sent from my iPhone
You may use environment variable instead of having multiple configuration settings.
You can also use some package dedicated to that such as django-environ in your settings.py Then add a sample file config.env.sample to your repository, add config.env ins .gitignored, and setup your env this way
This solution give you chance not to have any 'secret' info on your repository (assuming you are going to publish it somewhere)
Cheers,
Marc
Post by Alexander Lamas
Hi guys,
In my Django REST Web API Framework, I would like to set up files for settings for different purposes.
settings_release.py
settings_debug.py
settings_unittest.py
Then, when I deploy to production it uses the settings_release.py, when I'm working on it I can set to settings_debug.py an when I'm running unit tests it looks the settings_unittests.py.
Is it possible to set this somewhere? Perhaps the app or solution configuration?
Also, is it possible to change to release mode so that when I publish to production it uses the settings_release.py automatically as default setting?
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alexander Lamas
2018-08-30 01:10:51 UTC
Permalink
Hi Alex,

Thank you very much for your reply.

So, you could also have a production.py and python/django is intelligent
enought to know which want to use?

Do we have to have some settings somewhere to tell which settings file to
use?

Thanks in advance!

Cheers,
Alex
Post by Alexander Lamas
Hi guys,
In my Django REST Web API Framework, I would like to set up files for
settings for different purposes.
settings_release.py
settings_debug.py
settings_unittest.py
Then, when I deploy to production it uses the settings_release.py, when
I'm working on it I can set to settings_debug.py an when I'm running unit
tests it looks the settings_unittests.py.
Is it possible to set this somewhere? Perhaps the app or solution
configuration?
Also, is it possible to change to release mode so that when I publish to
production it uses the settings_release.py automatically as default setting?
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alex Daigle
2018-08-30 02:57:43 UTC
Permalink
It’s a python program after all, minor edits but they can be confusing. Some which are not always advised by most tutorials.

In the current location of settings.py, create a folder named settings. Within this new folder, add the setting.py file, and rename that file to base.py. Create a new empty local.py file and at the top add: from .base import *.

***This is the only situation where I recommend actually using * in an import. ***

Start dividing your base file to things that are specific to local, or needed everywhere (which goes into base). EG: My database settings are in Base, certain tools to help me build my app like Django toolbar are only in local. Debug, logging options...

Append variables, and everything else. In local I have INSTALLED_APPS += [‘some app like toolbar’] I believe you also must alter the ROOT_DIR in base.py to reflect the new path. In base.py, I have INSTALLED_APPS also, but only things I use everywhere for the app (like REST) are in there. Because of the import, we can append or overwrite anything in base.

This is really the only “nasty” part, because every book and tutorial on Django always tells you not to mess with this file. You must edit manage.py. But with the naming we just chose, it’s easy. You see the line in manage.py that says os.environ.setdefault()... you see where it says your project name with a .settings at the end? Add a .local after settings. Like this: my_awesome_app.settings.local That way when you do manage.py commands, it will always use the local.py. In production, you set something like gunicorn to use settings/production.py, so your repo does not change for either cases.

You also need an __init__.py file inside your settings folder, it can be empty. I usually have 4 settings files that are not repeated. Base, Local, Production, Test.

Objectify your files! Reduce, reuse, and, recycle your code. That’s my Django philosophy.

Good luck,
Alex

Sent from my iPhone
Post by Alexander Lamas
Hi Alex,
Thank you very much for your reply.
So, you could also have a production.py and python/django is intelligent enought to know which want to use?
Do we have to have some settings somewhere to tell which settings file to use?
Thanks in advance!
Cheers,
Alex
Post by Alexander Lamas
Hi guys,
In my Django REST Web API Framework, I would like to set up files for settings for different purposes.
settings_release.py
settings_debug.py
settings_unittest.py
Then, when I deploy to production it uses the settings_release.py, when I'm working on it I can set to settings_debug.py an when I'm running unit tests it looks the settings_unittests.py.
Is it possible to set this somewhere? Perhaps the app or solution configuration?
Also, is it possible to change to release mode so that when I publish to production it uses the settings_release.py automatically as default setting?
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...