D
D
devrais2019-12-07 05:36:48
Django
devrais, 2019-12-07 05:36:48

How to do with debug = false?

I want to make it so that when debug = true it was sql lite 3 and when false mysql. Implemented a simple condition. I don't understand why it doesn't work.

if settings.DEBUG = False:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'djangopt_db',
            'USER': 'django',
            'PASSWORD': 'xxxxxx',
            'HOST': '',
            'PORT': '',
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

swears
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/django/.djenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 325, in execute
    settings.INSTALLED_APPS
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 57, in __getattr__
    self._setup(name)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/django/.djenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 661, in exec_module
  File "<frozen importlib._bootstrap_external>", line 767, in get_code
  File "<frozen importlib._bootstrap_external>", line 727, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/django/projects_list/r/forum/forum/settings.py", line 77
    if DEBUG = False:
             ^
SyntaxError: invalid syntax
(.djenv) [email protected]:~/projects_list/r/forum$ python manage.py createsuperuser
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/django/.djenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 325, in execute
    settings.INSTALLED_APPS
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 57, in __getattr__
    self._setup(name)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/django/.djenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 661, in exec_module
  File "<frozen importlib._bootstrap_external>", line 767, in get_code
  File "<frozen importlib._bootstrap_external>", line 727, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/django/projects_list/r/forum/forum/settings.py", line 77
    if settings.DEBUG = False:
                      ^
SyntaxError: invalid syntax
(.djenv) [email protected]:~/projects_list/r/forum$ python manage.py createsuperuser
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/django/.djenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 325, in execute
    settings.INSTALLED_APPS
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 57, in __getattr__
    self._setup(name)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/django/.djenv/lib/python3.5/site-packages/django/conf/__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/django/.djenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 661, in exec_module
  File "<frozen importlib._bootstrap_external>", line 767, in get_code
  File "<frozen importlib._bootstrap_external>", line 727, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/django/projects_list/r/forum/forum/settings.py", line 77
    if settings.DEBUG = False:
                      ^
SyntaxError: invalid syntax

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
antonksa, 2019-12-07
@devrais

As you wrote above, you have a classic novice syntax error, single = is an assignment operator, double == is a Boolean equivalence operator. Therefore, you must use == instead of =, it is not clear why your IDE did not show you an error. What are you developing in? Your best bet is to use PyCharm. And as noted above, Python is an object-oriented language in which almost everything is an object. And it has two special objects built into it - True and False. Therefore, it is considered good practice for boolean operations to check whether the result object of a boolean expression is a True or False object. Because to do this, Python only compares the internal references of its object id, this operation is slightly faster. It is written as follows:

if DEBUG is False:
    ....
else:
    ....

But for the sake of complete perfectionism, the branches need to be swapped, and since if under the hood does something like bool (condition) with the condition, you can write it like this:
if DEBUG:
    # some code if DEBUG is True
else:
    # some code if DEBUG is False

S
Sergey Gornostaev, 2019-12-07
@sergey-gornostaev

First, you need to use the equals sign in the conditional expression ==, not the assignment sign =. Secondly, inside the settings module, you don't need to write settings.DEBUG. Thirdly, such a task is usually solved by connecting an additional settings file local to each working environment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question