Saphire
Saphire

Reputation: 1930

Automatically run Django project in Debug when in PyCharm

Is it possible to run my Django project in DEBUG mode, when in PyCharm? If not in PyCharm, say in production, then of course the DEBUG mode should not be True. How can this be done most neatly?

Upvotes: 2

Views: 794

Answers (4)

algo99
algo99

Reputation: 99

Use cookiecutter-duango to generate the project template when starting a new Django project. Your project can be configured for development with PyCharm and you get the both configurations, development (with DEBUG=True) and production (with DEBUG=False) automatically.

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174622

Edit your run configuration and set an environment variable called DJANGO_DEBUG to True. The easiest way to do this is to create a custom run configuration.

  1. Run > Edit Configurations
  2. Create a new configuration for django, by clicking on + and selecting django server:

enter image description here

  1. Then, set a custom environment variable. You do this by clicking on the button next to PYTHONUNBUFFERED=1:

enter image description here

  1. Next, add a new configuration variable by clicking the +

enter image description here

  1. Click OK, and give your new configuration a name:

enter image description here

  1. Click Apply then OK to dismiss the dialogues.

  2. Edit your settings.py and add this:

    import os
    
    DEBUG = os.environ.get('DJANGO_DEBUG', False)
    

Now, simply select your new configuration from the Run menu when you want to run with debugging enabled.

Upvotes: 2

Kos
Kos

Reputation: 72271

The standard approach for this is to have multiple Django settings files:

settings/
    base.py
    production.py
    development.py
    testing.py

then you can run DJANGO_SETTINGS_MODULE=settings.development manage.py runserver.

PyCharm allows to pick the settings in run configurations too.

Upvotes: 1

AKX
AKX

Reputation: 169051

The easiest way would probably be setting up your settings.py to read the DEBUG value from the environment variables, then tell PyCharm to run with that env var set.

import os
# ...
DEBUG = (True if os.environ.get('DEBUG') else False)

and set DEBUG=1 in PyCharm's Edit Configurations... window.

Upvotes: 3

Related Questions