Skip to content Skip to sidebar Skip to footer

Can The Django Development Server Be Started Programmatically?

I'm trying to start the django development server from another module in my package. My module can import manage.py, and I want to execute the equivalent of manage.py runserver wit

Solution 1:

Yes. Just do what's in the manage.py:

import os
from django.core.managementimport execute_from_command_line
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'web.settings')
execute_from_command_line(list_of_args)

This should work fine. Just remember that execute_from_command_line accepts originally sys.argv as argument, so the command runserver is on the index 1:

list_of_args = ['', 'runserver']

Post a Comment for "Can The Django Development Server Be Started Programmatically?"