klautern
klautern

Reputation: 139

Django admin is loading pages incredibly slowly

I've got a django application with django suit running in a gunicorn server and it´s taking a lot of time to load every single page of the admin (both list and edit views). The list view of a table that has 1 single record with no foreign keys and no callables is taking 6-8 seconds, and the list view of a table paged to 50 elements is taking 25-30 seconds.

I've checked the executed SQLs thought the logging module with the following configuration and they seem to be OK, the total time of all the SQLs is about 2 tenths. The database is postgresql.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/opt/django/myproj/log/debug.log',
        },
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

I've also tried to disable USE_I18N and removed the @never_cache from the admin just in case, but nothing has changed.

Does anyone has any suggestion on what could be going on in my server?

Thanks!

My context processors:

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    'django.core.context_processors.request',
    'django.core.context_processors.i18n'
)

Upvotes: 0

Views: 2335

Answers (2)

klautern
klautern

Reputation: 139

Finally the problem was a process running in the server and consuming 98% of CPU. So nothing to do with django, but just in case this helps anyone.

Upvotes: 1

spookylukey
spookylukey

Reputation: 6576

I would expect the total time of SQL to be far less than 1 second. If it isn't, are there lots of SQL queries? For an admin view like the one you've described, you should be looking at fewer than 10 SQL queries.

The number of SQL queries can have a big effect, because Django has to construct them and build the result set, and this time is not included in the times given for the SQL queries.

You should add the django-debug-toolbar and see if that gives some clues.

Other sources of slowness:

  • context processors that are slow (e.g. do lots of DB queries), and are executed because they are in the global TEMPLATE_CONTEXT_PROCESSORS setting. A solution is to remove them and only add them to views that need them, or use laziness so that they don't actually get evaluated unless needed.

  • Something that is being added to the head of your HTML page and is loading slowly. Use your browser network debugger to check

  • Something else server-side. Using Python profiling to work out where the time is being spent.

Upvotes: 3

Related Questions