[Answer]-Django server stops during for loop

1👍

To make debugging easy on django apps, you can setup a basic logging like this (if you haven’t):

# settings.py
import logging

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(name)-12s %(module)-20s %(funcName)-15s %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'logging.NullHandler',
        },
        'console':{
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'log_file':{
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(BASE_DIR, 'myapp.log'),
            'maxBytes': '16777216', # 16megabytes (to keep the file max. 16MB big)
            'formatter': 'verbose'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'formatter': 'verbose',
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['log_file'],
            'level': 'ERROR',
            'propagate': True,
        },
        'myapp': { # this will catch any log-calls inside your app 'myapp'
            'handlers': ['log_file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

# somefile_to_debug.py

# ... use pformat to output rather complex data structures as pretty
#       strings (perfect for debugging)

from pprint import pformat
import logging

# Create an instance of a logger which will include the name of this module
logger = logging.getLogger(__name__)

def my_function(bla, somedict):
    logger.debug(pformat({'bla': bla, 'somedict': somedict}))

Restart your app, and you can use tail to watch the output on your log-file on a terminal (sorry, don’t know how to do this in windows, but in linux you’d do):

tail -f myapp.log

👤andzep

Leave a comment