9๐
โ
Here is my solution. Add the code below to the bottom of your settings file.
# Process --set command line option
import sys
# This module can be imported several times,
# check if the option has been retrieved already.
if not hasattr(sys, 'arg_set'):
# Search for the option.
args = filter(lambda arg: arg[:6] == '--set=', sys.argv[1:])
if len(args) > 0:
expr = args[0][6:]
# Remove the option from argument list, because the actual command
# knows nothing about it.
sys.argv.remove(args[0])
else:
# --set is not provided.
expr = ''
# Save option value for future use.
sys.arg_set = expr
# Execute the option value.
exec sys.arg_set
Then just pass any code to any management command:
./manage.py runserver --set="DEBUG=True ; TEMPLATE_DEBUG=True"
๐คraacer
2๐
You can add custom option (ex. log level) to your command. Docs
Example:
from optparse import make_option
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--delete',
action='store_true',
dest='delete',
default=False,
help='Delete poll instead of closing it'),
)
# ...
๐คDenis Kabalkin
- Django CommandError: App 'polls' has migrations
- How to limit queryset/the records to view in Django admin site?
2๐
In settings.py you can check for command line arguments, like this:
import sys
# for testing
if "--enable-wiki" in sys.argv:
ENABLE_WIKI = True
sys.argv.remove("--enable-wiki")
Usage:
./manage.py test --enable-wiki MyApp.tests
๐คRamon
- In MVC (eg. Django) what's the best place to put your heavy logic?
- Django migrate error : TypeError expected string or bytes-like object
- Django 1.8, makemigrations not detecting newly added app
- Moving django apps into subfolder and url.py error
- Django Generic Views: When to use ListView vs. DetailView
-1๐
You can make your settings.py more aware of itโs current environment:
DEBUG = socket.gethostname().find( 'example.com' ) == -1
Hereโs an option for different databases when testing:
'ENGINE': 'sqlite3' if 'test_coverage' in sys.argv else 'django.db.backends.postgresql_psycopg2',
๐คgdonald
- Django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty
- How to set initial values for a ModelForm when instance is also given
- Is there a way to render a html page without view model?
- Make browser submit additional HTTP-Header if click on hyperlink
Source:stackexchange.com