4

In an attempt to try to generalize a django app, so it can be run with different configurations defined in yaml files, I wanted to extend the runserver command to take the locations of these configuration files as arguments.

I followed a guide by Chase Seibert, but when I try to run the command with my "added" arguments, it doesn't seem to work.

I have a feeling it is because of the location of the management/commands directory, but moving that around (with the __init__.py isn't helping).

Any suggestions? Thanks!

My app structure

my_app/
    __init__.py
    static/
    templates/
    manage.py
    app1/
    app2/
    my_app/
        __init__.py
        management/
            __init__.py
            commands/
                __init__.py
                runserver.py
        models.py
        views.py
        settings.py
        urls.py

My extension of runserver

from django.core.management import call_command
from django.core.management.base import BaseCommand
from django.core.management.commands.runserver import BaseRunserverCommand

from .env import PATH_CFG_SIM, PATH_CFG_PARAM


class Command(BaseCommand):
    ''' Runs the built-in Django runserver with initial schema and fixtures
    Note: cannot use auto-reloading because that casues resetdb to be called
    twice.
    '''

    # remove this option from the --help for this command
    option_list = (o for o in BaseRunserverCommand.option_list if o.dest != 'use_reloader')
    help = 'Starts a lightweight web server testing.'
    args = '[optional port number, or ipaddr:port] appname appname'

    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)
        parser.add_argument('--cfg-sim', action='store',
                            dest='path_cfg_sim',
                            help='Set path to simulation.yml configurations')
        parser.add_argument('--cfg-param', action='store',
                            dest='path_cfg_param',
                            help='Set path to parameters.yml configurations')


    def handle(self, addrport='', *args, **options):

        # Set path parameters if passed
        if options['path_cfg_sim']:
            PATH_CFG_SIM = options['path_cfg_sim']
        if options['path_cfg_param']:
            PATH_CFG_PARAM = options['path_cfg_param']

        call_command('syncdb', *args, **options)
        # BUG: runserver gets called twice if --noreload is False
        # https://code.djangoproject.com/ticket/8085
        options['use_reloader'] = False
        call_command('runserver', addrport, *args, **options)

        return None
6
  • what's the error? Commented Jun 9, 2017 at 13:17
  • 2
    Make sure you have aded myapp to INSTALLED_APPS. Commented Jun 9, 2017 at 13:28
  • also I didn't see the __init__.py in your my_app folder. Commented Jun 9, 2017 at 13:46
  • @DiegoPuente That was a mistake. There is an __init__.py there. Updating. Commented Jun 12, 2017 at 11:06
  • 1
    Django will only check for management commands (and models, template tags and so on) for apps in the INSTALLED_APPS setting. If you do not want to add myapp to INSTALLED_APPS because it's your project directory, then you could create a new app and move the management directory there. Commented Jun 12, 2017 at 11:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.