Is there a way to determine if Django is running on localhost and setting the DEBUG
variable in settings.py
accordingly.
So that if I run the server locally it will set DEBUG
to True
and otherwise set it to False
.
Localhost: python manage.py runserver
Not localhost: python manage.py runserver 0.0.0.0:8000
To verify the Django project, make sure your virtual environment is activated, then start Django's development server using the command python manage.py runserver . The server runs on the default port 8000, and you see output like the following output in the terminal window: Performing system checks...
For Django this will usually be the Gunicorn web application server (with a . wsgi script). wsgi.py: WSGI configuration to call our Django application in the Railway environment.
The built-in development server is part of what the manage.py file offers in any Django Project. To run the development server you'll want to navigate to the project directory that holds your Django project. This can be done right from Visual Studio code if you like by clicking Terminal-> New Terminal like so.
Django is an extremely popular and fully featured server-side web framework, written in Python. This module shows you why Django is one of the most popular web server frameworks, how to set up a development environment, and how to start using it to create your own web applications.
This is not the best approach, but it works :)
For something better you can use django-configurations
import sys
# Determine if in Production or Development
if (len(sys.argv) >= 2 and sys.argv[1] == 'runserver'):
DEBUG = True
#...
else:
DEBUG = False
#...
Or you can use it as one-liner as mentioned by little_birdie in the comments:DEBUG = (len(sys.argv) > 1 and sys.argv[1] == 'runserver')
As suggested by Bernhard Vallant, you can just check for runserver
in sys.argv
.
You can just replace your DEBUG
assignment in settings.py
with this:
DEBUG = (sys.argv[1] == 'runserver')
You should also import sys
somewhere in settings.py
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With