Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly runserver on different settings for Django?

Tags:

python

django

I have a basic django rest API. I want to separate some of my settings for dev and prod just for organizational purposes. I'm also just learning about separating environments. I've read a few things, but I can't seem to get it working the way I want it to.

The hierarchy looks like this:

- djangorest
  - api
    - __init__.py
    - models.py
    - views.py
    - urls.py
    - etc..
  - djangorest
    - __init__.py
    - settings.py
    - urls.py
    - wsgi.py

Right now, when I run the server, I do a simple:

python3 manage.py runserver

That command reads the settings from settings.py and runs it appropriately but I've looked around on how to separate the settings into prod vs dev and it doesn't work right.

I want to be able to have:

commonsettings.py
dev.py
prod.py

In commonsettings, it'll just have whatever's in both dev and prod. I've tried running:

python3 manage.py runserver --settings=settings.dev

But it gives me an error saying there is no module named 'setting'.

Please help. Thank you!

like image 298
chakolatemilk Avatar asked Mar 12 '18 12:03

chakolatemilk


People also ask

How do I run Python py Runserver on different ports?

How do I run Python py Runserver on different ports? If you want to use a different port than the default 8000, specify the port number on the command line, such as python manage.py runserver 5000 . Ctrl+click the http://127.0.0.1:8000/ URL in the terminal output window to open your default browser to that address.


1 Answers

For example I open SQL log in dev settings

  1. My file structure:
    create settings folder and mv original settings.py to settings/defaults.py enter image description here

  2. load defaults in init.py

# proj/proj/settings/__init__.py

from .defaults import *
  1. Edit dev.py
# proj/proj/settings/dev.py

from .defaults import *
DEBUG = True

# print sql to the console
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'DEBUG',
        }
    },
}

  1. run in the python env
./manage.py runserver --settings proj.settings.dev

enter image description here

Django 2.1.7 | Mac

like image 105
C.K. Avatar answered Oct 21 '22 14:10

C.K.