简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'find', cache error

In my local environment the django development server works fine with no errors.

When I run the app on a production environment with nginx & gunicorn I get this error:

AttributeError: 'NoneType' object has no attribute 'find'

This is the source of the error:

if cache.get('ratings').find(name_input) == -1:
    result = food.objects.get(name = name_input)

I have memcache imported:

from django.core.cache import cache

and in my settings.py:

CACHES = {
   'default': {
       'BACKEND':   
       'django.core.cache.backends.memcached.MemcachedCache',                                                                                                                                                                                                                                                   
        'LOCATION': '127.0.0.1:11211',
    }
}

I expect the webpage to work without errors

--------EDIT:--------------

Providing more info I have a JSON script in my HTML section that sends the variable to python. Then python catches it.

I repeat that everything works like charm locally.

===================================

--------Edit new------ I have tried to change the location of memcache to point to my server in "settings.py":

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'MY_IP:11211',
}

}

but this attempt didn't work I can print normally to the syslog, the values ARE NOT NULL

I believe that the cache.get('ratings') is unable to find the string 'ratings' so it returns NoneType by default, so it's unable to use the .find attribute. You should try printing the cache to see if there is the string ratings inside it

The error was that I didn't have memcached installed in the first place; even though I used this command to install it:

pip3 install python-memcached

Then I used another command instead:

sudo apt install memcached

and everything works fine now :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM