20👍
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
.
2👍
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')
- Filter on django-import-export
- Django backup strategy with dumpdata and migrations
- Django: Change the DOM id at form field level
- Merge/join lists of dictionaries based on a common value in Python
- Is a varchar 2 more efficient than a varchar 255?
0👍
Could not have a permalink to this accepted and related answer to your question. So, just pasting it:-
server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
print 'inside dev'
Of course, wsgi.file_wrapper might be set on META, and have a class from a module named django.core.servers.basehttp by extreme coincidence on another server environment, but I hope this will have you covered.
PS: Please refer to How can I tell whether my Django application is running on development server or not? for more details
- Using Django auth User model as a Foreignkey and reverse relations
- Django REST Framework different depth for POST/PUT?
- Passing a variable in redirect in Django
- How to get django queryset results with formatted datetime field
0👍
- Simpler code.
- you really should log it so you can know for sure
- this works even if it’s run in an environment that starts it a completely different way other than calling
python
. There may not beargv
at position 1.
import sys
DEBUG = 'runserver' in sys.argv
print(f'DEBUG = {DEBUG}')