Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django runserver in command with custom settings

Tags:

python

django

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.
like image 350
patroqueeet Avatar asked Jul 25 '12 07:07

patroqueeet


People also ask

How do I change Django settings?

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 .

How do I run python py Runserver on different ports?

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"

What is Runserver command in Django?

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.


2 Answers

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.

like image 57
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 14:10

Ignacio Vazquez-Abrams


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

like image 22
patroqueeet Avatar answered Oct 07 '22 13:10

patroqueeet