I try to execute the runserver inside a command with custom settings. But the settings are not loaded:
from django.core.management.base import BaseCommand
from django.core.management import call_command
from django.core.management import setup_environ, ManagementUtility
import settings_api
setup_environ(settings_api)
class Command(BaseCommand):
help = "Local API to retrieve realtime quotes"
def handle(self, *args, **options):
if not settings_api.DEBUG:
call_command('runserver', '127.0.0.1:8002')
else:
call_command('runserver', '0.0.0.0:8002')
Output is:
Used API settings
Used API settings
Validating models...
0 errors found
Django version 1.4b1, using settings 'site.settings'
Development server is running at http://0.0.0.0:8002/
Quit the server with CONTROL-C.
When you use Django, you have to tell it which settings you're using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE . The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite. settings .
Inside the commands folder open up the runserver.py script with a text editor. Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like DEFAULT_PORT = "8080"
The runserver command is a built-in subcommand of Django's manage.py file that will start up a development server for this specific Django project.
runserver
forces default settings unless passed --settings=
. Use a separate WSGI container (or even the built-in one, brought up yourself) if you want custom settings.
Ignacios answer pointed me the right direction: executing the command with custom settings param solved it:
./manage.py command --settings=settings_api
More details here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With